最后
由于文案过于长,在此就不一一介绍了,这份Java后端架构进阶笔记内容包括:Java集合,JVM、Java并发、微服务、SpringNetty与 RPC 、网络、日志 、Zookeeper 、Kafka 、RabbitMQ 、Hbase 、MongoDB、Cassandra 、Java基础、负载均衡、数据库、一致性算法、Java算法、数据结构、分布式缓存等等知识详解。
本知识体系适合于所有Java程序员学习,关于以上目录中的知识点都有详细的讲解及介绍,掌握该知识点的所有内容对你会有一个质的提升,其中也总结了很多面试过程中遇到的题目以及有对应的视频解析总结。
跟操作数据库一样,写入地址、账号密码,获取一个客户端,有兴趣的可以看org.springframework.data.elasticsearch.client.ClusterNodesspring boot是怎么解析集群的
private TransportClient client;
private String[] nodes = new String[]{“127.0.0.1:9200”};
@Before
public void initClint() {
Settings settings = Settings.builder()
// es 集群的名称
.put(“cluster.name”, “elasticsearch”)
.put(“client.transport.sniff”, “true”)
//账号密码
.put(“xpack.security.user”, “elastic:123456”)
.build();
client = new PreBuiltXPackTransportClient(settings)
//添加集群节点
.addTransportAddresses(parseAddress());
}
创建索引
可以理解为创建一个mysql数据库表
@Test
public void createIndex() {
client.admin()
.indices()
.prepareCreate(“elastic”).get();
client.close();
}
设置mappings
mappings可以理解为mysql的表字段(json数据)
对应的json格式:
{
“properties”: {
“id”: {
“type”: “long”,
“store”: true
},
“name”: {
“type”: “text”,
“store”: true
},
“age”: {
“type”: “integer”,
“store”: true
}
}
}
对应的Java格式:
XContentBuilder builder = XContentFactory.jsonBuilder()
.startObject() // 相当于json的’{’
.startObject(“properties”)
.startObject(“id”)
.field(“type”, “long”) //字段类型
.field(“store”, true) //是否存储
.endObject() //相当于json的’}’
.startObject(“name”)
.field(“type”, “text”)
.field(“store”, true)
.field(“analyzer”, “ik_smart”) //采用ik_smart分词 “search_analyzer”: “ik_smart”
.endObject()
.startObject(“age”)
.field(“type”, “integer”)
.field(“store”, true)
.endObject()
.startObject(“desc”)
.field(“type”, “text”)
.field(“store”, true)
.field(“analyzer”, “ik_max_word”)
.endObject()
.startObject(“registerTime”)
.field(“type”, “date”)
.field(“store”, true)
.field(“format”, “yyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis”)
.endObject()
.endObject()
.endObject();
client.admin().indices()
.preparePutMapping(“elastic”)
.setType(“user”) //对应数据库的表名称
.setSource(builder)
.get();
client.close();
1.1常用查询
1、查询全部:match_all
2、模糊匹配: match (类似sql 的 like)
3、全句匹配: match_phrase (类似sql 的 = )
4、多字段匹配:muti_match (多属性查询)
5、语法查询:query_string (直接写需要配置的 关键字 )
6、字段查询 : term (针对某个属性的查询,这里注意 term 不会进行分词,比如 在 es 中 存了 “火锅” 会被分成 “火/锅” 当你用 term 去查询 “火时能查到”,但是查询 “火锅” 时,就什么都没有,而 match 就会将词语分成 “火/锅”去查)
7、范围查询:range ()
1.2 增删改查
1.2.1.插入操作
Java插入操作代码:
示例一:
String index = “test1”;
String type = “_doc”;
// 唯一编号
String id = “1”;
IndexRequest request = new IndexRequest(index, type, id);
String jsonString = “{” + ““uid”:“1234”,”+ ““phone”:“12345678909”,”+ ““msgcode”:“1”,” + ““sendtime”:“2019-03-14 01:57:04”,”
- ““message”:“xuwujing study Elasticsearch”” + “}”;
request.source(jsonString, XContentType.JSON);
IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
示例二:
String index = “test1”;
String type = “_doc”;
// 唯一编号
String id = “1”;
IndexRequest request = new IndexRequest(index, type, id);
Map<String, Object> jsonMap = new HashMap<>();
jsonMap.put(“uid”, 1234);
jsonMap.put(“phone”, 12345678909L);
jsonMap.put(“msgcode”, 1);
jsonMap.put(“sendtime”, “2019-03-14 01:57:04”);
jsonMap.put(“message”, “xuwujing study Elasticsearch”);
request.source(jsonMap);
IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
示例三:
String index = “test1”;
String type = “_doc”;
// 唯一编号
String id = “1”;
IndexRequest request = new IndexRequest(index, type, id);
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
{
builder.field(“uid”, 1234);
builder.field(“phone”, 12345678909L);
builder.field(“msgcode”, 1);
builder.timeField(“sendtime”, “2019-03-14 01:57:04”);
builder.field(“message”, “xuwujing study Elasticsearch”);
}
builder.endObject();
request.source(builder);
IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
1> 使用自定义的id
使用put方式,并自己提供id
类似于下面的格式
PUT /{index}/{type}/{id}
{
“field”: “value”,
…
}
请求
PUT /website/blog/123
{
“title”: “My first blog entry”,
“text”: “Just trying this out…”,
“date”: “2014/01/01”
}
响应
{
“_index”: “website”,
“_type”: “blog”,
“_id”: “123”,
“_version”: 1,
“created”: true
}
自动生成的 ID 是 URL-safe、 基于 Base64 编码且长度为20个字符的 GUID 字符串。 这些 GUID 字符串由可修改的 FlakeID 模式生成,这种模式允许多个节点并行生成唯一 ID ,且互相之间的冲突概率几乎为零。
1.2.2.更改操作
Java示例代码:
private static void update() throws IOException {
String type = “_doc”;
String index = “test1”;
// 唯一编号
String id = “1”;
UpdateRequest upateRequest = new UpdateRequest();
upateRequest.id(id);
upateRequest.index(index);
upateRequest.type(type);
// 依旧可以使用Map这种集合作为更新条件
Map<String, Object> jsonMap = new HashMap<>();
jsonMap.put(“uid”, 12345);
jsonMap.put(“phone”, 123456789019L);
jsonMap.put(“msgcode”, 2);
jsonMap.put(“sendtime”, “2019-03-14 01:57:04”);
jsonMap.put(“message”, “xuwujing study Elasticsearch”);
upateRequest.doc(jsonMap);
// upsert 方法表示如果数据不存在,那么就新增一条
upateRequest.docAsUpsert(true);
client.update(upateRequest, RequestOptions.DEFAULT);
System.out.println(“更新成功!”);
}
控制台输入
PUT /website/blog/123
{
“title”: “My first blog entry”,
“text”: “Just trying this out…”,
“date”: “2014/01/01”
}
在响应体中,我们能看到 Elasticsearch 已经增加了 _version 字段值,created 标志设置成 false ,是因为相同的索引、类型和 ID 的文档已经存在。
{
“_index”: “website”,
“_type”: “blog”,
“_id”: “123”,
“_version”: 2,
“created”: false
}
1.2.3.删除操作
Java示例代码:
示例一(正常根据ID删除):
private static void delete() throws IOException {
String type = “_doc”;
String index = “test1”;
// 唯一编号
String id = “1”;
DeleteRequest deleteRequest = new DeleteRequest();
deleteRequest.id(id);
deleteRequest.index(index);
deleteRequest.type(type);
// 设置超时时间
deleteRequest.timeout(TimeValue.timeValueMinutes(2));
// 设置刷新策略"wait_for"
// 保持此请求打开,直到刷新使此请求的内容可以搜索为止。此刷新策略与高索引和搜索吞吐量兼容,但它会导致请求等待响应,直到发生刷新
deleteRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);
// 同步删除
DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT);
}
示例二(条件删除):
private static void deleteByQuery() throws IOException {
String type = “_doc”;
String index = “test1”;
DeleteByQueryRequest request = new DeleteByQueryRequest(index,type);
// 设置查询条件
request.setQuery(QueryBuilders.termsQuery(“uid”,1234));
// 同步执行
BulkByScrollResponse bulkResponse = client.deleteByQuery(request, RequestOptions.DEFAULT);
}
DELETE /website/blog/123
如果找到该文档,Elasticsearch 将要返回一个 200 ok 的 HTTP 响应码,和一个类似以下结构的响应体。注意,字段 _version 值已经增加:
{
“found” : true,
“_index” : “website”,
“_type” : “blog”,
“_id” : “123”,
“_version” : 3
}
如果文档没有 找到,我们将得到 404 Not Found 的响应码和类似这样的响应体:
{
“found” : false,
“_index” : “website”,
“_type” : “blog”,
“_id” : “123”,
“_version” : 4
}
1.2.4.查询操作
查询API
等值(term查询:QueryBuilders.termQuery(name,value);
多值(terms)查询:QueryBuilders.termsQuery(name,value,value2,value3…);
范围(range)查询:QueryBuilders.rangeQuery(name).gte(value).lte(value);
存在(exists)查询:QueryBuilders.existsQuery(name);
模糊(wildcard)查询:QueryBuilders.wildcardQuery(name,+value+);
组合(bool)查询: BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
1.2.4.1.查询所有代码示例
在这里插入代码片 private static void allSearch() throws IOException {
SearchRequest searchRequestAll = new SearchRequest();
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchAllQuery());
searchRequestAll.source(searchSourceBuilder);
// 同步查询
SearchResponse searchResponseAll = client.search(searchRequestAll, RequestOptions.DEFAULT);
System.out.println(“所有查询总数:” + searchResponseAll.getHits().getTotalHits());
}
1.2.4.2.一般查询代码示例
private static void genSearch() throws IOException {
String type = “_doc”;
String index = “test1”;
// 查询指定的索引库
SearchRequest searchRequest = new SearchRequest(index);
searchRequest.types(type);
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
// 设置查询条件
sourceBuilder.query(QueryBuilders.termQuery(“uid”, “1234”));
// 设置起止和结束
sourceBuilder.from(0);
sourceBuilder.size(5);
sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
// 设置路由
// searchRequest.routing(“routing”);
// 设置索引库表达式
searchRequest.indicesOptions(IndicesOptions.lenientExpandOpen());
// 查询选择本地分片,默认是集群分片
searchRequest.preference(“_local”);
// 排序
// 根据默认值进行降序排序
// sourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC));
// 根据字段进行升序排序
// sourceBuilder.sort(new FieldSortBuilder(“id”).order(SortOrder.ASC));
// 关闭suorce查询
// sourceBuilder.fetchSource(false);
String[] includeFields = new String[]{“title”, “user”, “innerObject.*”};
String[] excludeFields = new String[]{“_type”};
// 包含或排除字段
// sourceBuilder.fetchSource(includeFields, excludeFields);
searchRequest.source(sourceBuilder);
System.out.println(“普通查询的DSL语句:”+sourceBuilder.toString());
// 同步查询
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
// HTTP状态代码、执行时间或请求是否提前终止或超时
RestStatus status = searchResponse.status();
TimeValue took = searchResponse.getTook();
Boolean terminatedEarly = searchResponse.isTerminatedEarly();
boolean timedOut = searchResponse.isTimedOut();
// 供关于受搜索影响的切分总数的统计信息,以及成功和失败的切分
int totalShards = searchResponse.getTotalShards();
int successfulShards = searchResponse.getSuccessfulShards();
int failedShards = searchResponse.getFailedShards();
// 失败的原因
for (ShardSearchFailure failure : searchResponse.getShardFailures()) {
// failures should be handled here
笔者福利
以下是小编自己针对马上即将到来的金九银十准备的一套“面试宝典”,不管是技术还是HR的问题都有针对性的回答。
有了这个,面试踩雷?不存在的!
回馈粉丝,诚意满满!!!
ut = searchResponse.isTimedOut();
// 供关于受搜索影响的切分总数的统计信息,以及成功和失败的切分
int totalShards = searchResponse.getTotalShards();
int successfulShards = searchResponse.getSuccessfulShards();
int failedShards = searchResponse.getFailedShards();
// 失败的原因
for (ShardSearchFailure failure : searchResponse.getShardFailures()) {
// failures should be handled here
笔者福利
以下是小编自己针对马上即将到来的金九银十准备的一套“面试宝典”,不管是技术还是HR的问题都有针对性的回答。
有了这个,面试踩雷?不存在的!
回馈粉丝,诚意满满!!!
[外链图片转存中…(img-SaVrCSKH-1715637782137)]
[外链图片转存中…(img-pzk3NlPJ-1715637782137)]
[外链图片转存中…(img-g6o4B6my-1715637782137)]
[外链图片转存中…(img-xIqi2jIG-1715637782138)]