return httpHosts;
}
}
2:查询所有索引
//省略连接…
final GetIndexResponse all = client.indices().get(query -> query.index(“_all”));
System.out.println(all.toString());
//省略关闭…
3:查询某个索引
//查询某个索引
final GetIndexResponse products = client.indices().get(query -> query.index(“products”));
System.err.println(products.toString());
4:创建索引
//查询某个索引是否存在
boolean exists = client.indices().exists(query -> query.index(“products”)).value();
System.out.println(exists);
if (exists) {
System.err.println(“索引已存在”);
} else {
final CreateIndexResponse products = client.indices().create(builder -> builder.index(“products”));
System.err.println(products.acknowledged());
}
5:删除指定索引
//删除指定索引
boolean exists = client.indices().exists(query -> query.index(“products”)).value();
System.out.println(exists);
if (exists) {
DeleteIndexResponse response = client.indices().delete(query -> query.index(“products”));
System.err.println(response.acknowledged());
} else {
System.err.println(“索引不存在”);
}
6:查询索引的映射
//查询映射信息
final GetIndexResponse response = client.indices().get(builder -> builder.index(“produces”));
System.err.println(response.result().get(“produces”).mappings());
7:创建索引指定映射
numberOfReplicas(“1”):设置副本
numberOfShards(“1”):设置分片
//创建索引指定映射,分片和副本信息
final CreateIndexResponse response = client.indices().create(builder ->
builder.settings(indexSetting -> indexSetting.numberOfReplicas(“1”).numberOfShards(“1”)).mappings(
map -> map
.properties(“name”, propertyBuilder -> propertyBuilder.keyword(keywordProperty -> keywordProperty))
.properties(“price”, propertyBuilder -> propertyBuilder.double_(doubleNumProperty -> doubleNumProperty))
.properties(“des”, propertyBuilder -> propertyBuilder.text(textProperty -> textProperty.analyzer(“ik_smart”).searchAnalyzer(“ik_smart”)))
).index(“produces”)
);
8:创建文档
使用HashMap作为数据存储容器
//创建文档
//1.创建HashMap进行存储数据,文档要对应映射
final HashMap<String, Object> doc = new HashMap<>();
doc.put(“name”,“辣条”);
doc.put(“age”,12);
doc.put(“id”,“11111”);
//2.将文档存入索引中
final IndexResponse response = client.index(builder -> builder.index(“produces”).id(doc.get(“id”)).document(doc));
System.err.println(response.version());
使用自定义类作为数据存储容器
实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Produce {
private String id;
private String name;
private double age;
}
//创建文档
final Produce produce = new Produce(“123”, “小明”, 18);
final IndexResponse response = client.index(builder -> builder.index(“produces”).id(produce.getId()).document(produce));
System.err.println(response.version());
使用外部JSON数据创建
这里要注意我们需要使用StringReader进行读取时使用replace函数将设置的’改为",当然这在真实的业务中肯定不会有,因为真实业务中一定是标准的JSON数据,无需使用replace进行替换了
//创建文档
final StringReader input = new StringReader(
“{‘name’:‘农夫三拳’,‘price’:3.00,‘des’:‘农夫三拳有点甜’}”.replace(‘’', ‘"’)
);
final IndexResponse response = client.index(builder -> builder.index(“produces”).id(“44514”).withJson(input));
System.err.println(response.version());
9: 查询所有文档
final SearchResponse response = client.search(builder -> builder.index(“produces”), Object.class);
final List<Hit> hits = response.hits().hits();
hits.forEach(
x-> System.err.println(x)
);
10:根据ID查询文档
使用HashMap对应查询
//查询文档
final GetResponse
使用自定义类对应查询
final GetResponse response1 = client.get(builder -> builder.index(“produces”).id(“aabbcc123”), Produce.class);
final Produce source1 = response1.source();
System.err.println(source1.toString());
11:删除文档
final GetResponse response1 = client.get(builder -> builder.index(“produces”).id(“aabbcc123”), Produce.class);
final Produce source1 = response1.source();
System.err.println(source1.toString());
12:修改文档
全覆盖
//修改文档(覆盖)
final Produce produce = new Produce(“ccaabb123”, “旺仔摇滚洞”, “旺仔摇滚洞乱摇乱滚”, 10.23D);
final UpdateResponse response = client.update(builder -> builder.index(“produces”).id(“aabbcc123”).doc(produce), Produce.class);
System.err.println(response.shards().successful());
修改部分文档
区别在于我们需要设置.docAsUpsert(true)表明是修改部分而不是覆盖
//修改文档(部分修改)
// final Produce produce = new Produce(“ccaabb123”, “旺仔摇滚洞”, “旺仔摇滚洞乱摇乱滚”, 10.23D);
final Produce produce = new Produce();
produce.setName(“旺仔摇不动”);
final UpdateResponse response = client.update(builder -> builder.index(“produces”).id(“aabbcc123”).doc(produce).docAsUpsert(true), Produce.class);
System.err.println(response.shards().successful());
13:批量操作
批量新增
produceList.add(produce1);
produceList.add(produce2);
produceList.add(produce3);
//构建BulkRequest
final BulkRequest.Builder br = new BulkRequest.Builder();
for (Produce produce : produceList) {
br.operations(op->op.index(idx->idx.index(“produces”).id(produce.getSku()).document(produce)));
}
final BulkResponse response = client.bulk(br.build());
批量删除
List bulkOperations = new ArrayList<>();
// 向集合中添加需要删除的文档id信息
for (int i = 0; i < dto.getIds().size(); i++) {
int finalI = i;
bulkOperations.add(BulkOperation.of(b -> b
.delete((d -> d
.index(dto.getIndex())
.id(dto.getIds().get(finalI))
))
));
}
// 调用客户端的bulk方法,并获取批量操作响应结果
BulkResponse response = client
.bulk(e -> e
.index(dto.getIndex())
.operations(bulkOperations));
批量更新
JSONObject jsonObject = new JSONObject();
jsonObject.put(“id”, deleteIds);
jsonObject.put(“status”, 1);
BulkRequest.Builder br = new BulkRequest.Builder();
for (String deleteId : deleteIds) {
br.operations(op -> op
.update(idx ->
idx.index(EsIndexConstants.opinion_information)
.id(deleteId)
.action(a -> a
.doc(jsonObject)//局部修改
.docAsUpsert(true)//局部修改
)
)).refresh(Refresh.True);
}
BulkRequest bulkRequest = br.build();
BulkResponse result = null;
try {
result = elasticsearchClient.bulk(bulkRequest);
} catch (IOException e) {
throw new RuntimeException(e);
}
DSL查询
1:matchAll查询所有文档
//matchAll
final SearchResponse response = client.search(builder ->
builder.index(“produces”)
.query(q ->
q.matchAll(
v->v
)), Produce.class);
System.err.println(response.hits().hits());
2:match 根据字段查询
//简单query方式查询
final SearchResponse response = client.search(builder ->
builder.index(“produces”)
.query(q ->
q.match(t ->
t.field(“name”)
.query(“龙虎万精油”))), Produce.class);
System.err.println(response.hits().hits());
3:多id查询
//多ID查询
final SearchResponse response = client.search(builder ->
builder.index(“produces”)
.query(q ->
q.ids(sid->sid.values(“1000”,“1001”))), Produce.class);
System.err.println(response.hits().hits());
4:term 不分词查询
//term不分词条件查询
final SearchResponse response = client.search(builder -> builder.index(“produces”)
.query(q -> q.term(t -> t.field(“name”).value(“风油精”))), Produce.class);
System.err.println(response.hits().hits());
5:范围查询
//范围查询
final SearchResponse response = client.search(builder ->
builder.index(“produces”).query(q ->
q.range(r ->
r.field(“price”).gt(JsonData.of(5D)).lt(JsonData.of(15D)))),
Produce.class);
System.err.println(response.hits().hits());
6: 前缀查询
final SearchResponse response = client.search(builder ->
builder.index(“produces”).query(q ->q.prefix(p->p.field(“name”).value(“六”))),
Produce.class);
System.err.println(response.hits().hits());
7:匹配查询
//匹配查询
final SearchResponse response = client.search(builder ->
builder.index(“produces”).query(q ->q.wildcard(w->w.field(“name”).value(“风*”))),
Produce.class);
System.err.println(response.hits().hits());
?单字符匹配
//匹配查询
final SearchResponse response = client.search(builder ->
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数大数据工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年大数据全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上大数据开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以添加VX:vip204888 (备注大数据获取)
帮助到想自学提升又不知道该从何学起的朋友。**
[外链图片转存中…(img-0q44HX2k-1712516082949)]
[外链图片转存中…(img-unQRmqXI-1712516082950)]
[外链图片转存中…(img-RKpYbVPQ-1712516082951)]
[外链图片转存中…(img-93vMoFfn-1712516082951)]
[外链图片转存中…(img-eeORaoV6-1712516082952)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上大数据开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以添加VX:vip204888 (备注大数据获取)
[外链图片转存中…(img-3VzNdgIN-1712516082952)]