赞
踩
阅读文本大概需要3分钟。
Elasticsearch官方为Java提供了三种客户端API:
TransportClient:这种方式通过TCP与Elasticsearch服务进行交互。
Java Low Level REST Client: 低级别的REST客户端,通过http与集群交互,用户需自己编组请求JSON串,及解析响应JSON串。兼容所有ES版本。
Java High Level REST Client: 高级别的REST客户端,基于低级别的REST客户端,增加了编组请求JSON串、解析响应JSON串等相关api。使用的版本需要保持和ES服务端的版本一致,否则会有版本问题。
另外Spring框架也提供了spring-data-elasticsearch对Elasticsearch进行CURD操作,但是最底层也是基于Elasticsearch官方提供的API。
对于TransportClientAPI官方有如下一段话
https://www.elastic.co/guide/en/elasticsearch/client/java-api/7.0/java-api.html
The TransportClient
is deprecated infavour of the Java High Level RESTClient and will be removed in Elasticsearch 8.0. The migration guide describes all thesteps needed to migrate.
TransportClient API已经被标识为过期,并在Elasticsearch 8.0.版本后被移除。但是TransportClient API在一些陈年老项目还是使用到的,今天就从TransportClient API开始介绍一下Elasticsearch的几个Java客户端API。
1、在pom.xml文件中引入
- <dependency>
- <groupId>org.elasticsearch.client</groupId>
- <artifactId>transport</artifactId>
- <version>7.0.0</version>
- </dependency>
2、构建TransportClient对象
- public static TransportClient getTransportClient(){
-
- TransportClient client = null;
-
- try {
-
- client = new PreBuiltTransportClient(Settings.EMPTY);
-
- //根据主机名获取InetAddress对象
-
- // InetAddress remAdd=InetAddress.getByName("es.server.com");
-
- //获取本地InetAddress对象
-
- // InetAddress locAdd=InetAddress.getLocalHost();
-
- //创建客户端clients
-
- InetAddress locAdd = InetAddress.getByName("127.0.0.1"); // ip
-
- client.addTransportAddress(new TransportAddress(locAdd, 9300));
-
- System.out.println(client);
-
- //client.close();
-
- }catch (Exception e){
-
- e.printStackTrace();
-
- }
-
- return client;
-
- }
3、document操作,document分为单文档和多文档操作
https://www.elastic.co/guide/en/elasticsearch/client/java-api/7.0/java-docs.html
4、创建Document
Index API 允许我们添加某种类型的JSON文档到特定的index ,并使之可搜索。
使用json字符串创建Index
- public static IndexResponse getIndexResponseWithString(TransportClient client){
-
- String json = "{" +
-
- "\"user\":\"kimchy\"," +
-
- "\"postDate\":\"2013-01-30\"," +
-
- "\"message\":\"trying out Elasticsearch\"" +
-
- "}";
-
-
-
- IndexResponse response = client.prepareIndex
-
- ("twitter", "tweet", "1")
-
- .setSource(json, XContentType.JSON)
-
- .get();
-
- return response;
-
- }
使用map创建Index
- public static IndexResponse getIndexResponseWithMap(TransportClient client) {
-
- Map<String, Object> json = new HashMap<String, Object>();
-
- json.put("user","kimchy");
-
- json.put("postDate",new Date());
-
- json.put("message","trying out Elasticsearch");
-
- IndexResponse response = client.prepareIndex
-
- ("twitter2", "tweet2", "2")
-
- .setSource(json, XContentType.JSON)
-
- .get();
-
- return response;
-
- }
生成JSON文档的方式如下:
手动使用native byte[] or as a String
使用一个可以自动转换为对应JSON 的Map
使用第三方库如 Jackson序列化 beans
使用内置的 XContentFactory.jsonBuilder()
备注:
1、pom.xml文件
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
-
- <groupId>es.transport</groupId>
- <artifactId>es-transport</artifactId>
- <version>1.0-SNAPSHOT</version>
-
- <dependencies>
-
- <!--<dependency>
- <groupId>org.elasticsearch.client</groupId>
- <artifactId>transport</artifactId>
- <version>7.0.0</version>
- </dependency>-->
- <dependency>
- <groupId>org.elasticsearch.client</groupId>
- <artifactId>transport</artifactId>
- <version>6.7.0</version>
- </dependency>
- <dependency>
- <groupId>org.apache.logging.log4j</groupId>
- <artifactId>log4j-core</artifactId>
- <version>2.11.2</version>
- </dependency>
-
- </dependencies>
-
- </project>
2、使用elasticsearch-5.6.13版本elasticsearch
☆
往期精彩
☆
03 精讲Spring Boot—入门+进阶+实例
关注我
每天进步一点点
很干!在看吗?☟
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。