Elasticsearch-Elasticsearch-Rest-Client(三)

1)TCP

spring-data-elasticsearch:transport-api.jar;

springboot版本不同,transport-api.jar不同,不能适配es版本, 7.x已经不建议使用,8以后就要废弃

2)、9200:HTTP

JestClient:非官方,更新慢

RestTemplate:模拟发HTTP请求,ES很多操作需要自己封装,麻烦

HttpClient:同上

Elasticsearch-Rest-Client:官方RestClient,封装了ES操作,API层次分明,上手简单

最终选择Elasticsearch-Rest-Client(elasticsearch-rest-high-level-client) https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high.html

1、SpringBoot整合

复制代码
<dependency>
     <groupId>org.elasticsearch.client</groupId>
     <artifactId>elasticsearch-rest-high-level-client</artifactId>
     <version>7.4.2</version>
 </dependency>

2、配置

复制代码
@Bean RestHighLevelClientclient(){ 
    RestClientBuilder builder = RestClient.builder(newHttpHost("192.168.56.10", 9200, "http")); 
    return new RestHighLevelClient(builder); 
}

3、使用 参照官方文档

复制代码
@Test
 void test1() throwsIOException{
     Product product=new Product();
     product.setSpuName("华为");
     product.setId(10L);
     IndexRequest request=new IndexRequest("product").id("20").source("spuName","华为","id",20L);
try{
     IndexResponse response=client.index(request,RequestOptions.DEFAULT);
     System.out.println(request.toString());
    IndexResponse response2 = client.index(request, RequestOptions.DEFAULT);
 } catch (ElasticsearchException e) {
     if (e.status() == RestStatus.CONFLICT) {
     }
   }
 }
相关推荐
大刘讲IT12 分钟前
数据治理体系的“三驾马车”:质量、安全与价值挖掘
大数据·运维·经验分享·学习·安全·制造·零售
悻运1 小时前
Spark论述及其作用
大数据·分布式·spark
szxinmai主板定制专家2 小时前
国产RK3568+FPGA以 ‌“实时控制+高精度采集+灵活扩展”‌ 为核心的解决方案
大数据·运维·网络·人工智能·fpga开发·机器人
怒放吧德德2 小时前
MySQL篇:MySQL如何实时同步到ES
mysql·elasticsearch·面试
细心的莽夫3 小时前
Elasticsearch复习笔记
java·大数据·spring boot·笔记·后端·elasticsearch·docker
痕5174 小时前
spark和Hadoop之间的对比和联系
大数据·hadoop·spark
CXH7284 小时前
hadoop分布式部署
大数据·hadoop·分布式
码农周5 小时前
Elasticsearch 报错 Limit of total fields [1000] has been exceeded
大数据·elasticsearch
1momomo汉堡包5 小时前
spark和hadoop之间的对比和联系
大数据·hadoop·spark
Elastic 中国社区官方博客6 小时前
Elasticsearch 堆内存使用情况和 JVM 垃圾回收
大数据·jvm·数据库·elasticsearch·搜索引擎·全文检索