使用java操作ES
一、Java连接ES1.1、引入依赖包<dependency><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId><version>7.10.2</version></dependency>
·
文章目录
一、Java连接ES
连接客户端主要有 Rest Low Level Client 和 Rest High Level Client 两种可以使用,两者的主要区别在于:
-
Rest Low Level Client:低级别的 REST 客户端,通过 http 与集群交互,用户需自己编组请求 JSON 串,及解析响应 JSON 串。兼容所有ES版本。最小 Java 版本要求为 1.7。
-
Rest High Level Client:高级别的 REST 客户端,基于低级别的 REST 客户端,增加了编组请求 JSON 串、解析响应 JSON 串等相关 api。使用的版本需要保持和 ES 服务端的版本一致,否则会有版本问题
1.1、引入依赖包
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.10.2</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.10.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
<scope>provided</scope>
</dependency>
1.2、创建ES客户端
package com.chb.utils;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
public class ESClient {
public static RestHighLevelClient getClient() {
// 创建HttpHost
HttpHost httpHost = new HttpHost("s203", 9200);
// 创建 RestClientBuilder
RestClientBuilder restClientBuilder = RestClient.builder(httpHost);
// 创建RestHighLevelClient
RestHighLevelClient client = new RestHighLevelClient(restClientBuilder);
return client;
}
}
1.3、测试连接
package com.chb.test;
import com.chb.utils.ESClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.junit.Test;
public class Demo1 {
@Test
public void testConnect() {
RestHighLevelClient client = ESClient.getClient();
System.out.println("connect ok...");
}
}
二、操作索引
2.1、创建索引
package com.chb.test;
import com.chb.utils.ESClient;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.junit.Test;
import java.io.IOException;
public class Demo2 {
String index = "person";
RestHighLevelClient client = ESClient.getClient();
@Test
public void createIndex() throws IOException {
// 1、创建setting
Settings.Builder settings = Settings.builder()
.put("number_of_shards", 3)
.put("number_of_replicas", 1);
// 2、设置mappings
XContentBuilder mappings = JsonXContent.contentBuilder()
.startObject()
.startObject("properties")
.startObject("name")
.field("type", "text")
.endObject()
.startObject("age")
.field("type", "integer")
.endObject()
.startObject("birthday")
.field("type", "date")
.field("format", "yyyy-MM-dd")
.endObject()
.endObject()
.endObject();
// 3、将settings和mappings封装到Request对象
CreateIndexRequest request = new CreateIndexRequest(index).settings(settings).mapping(mappings);
// 4、通过client对象连接ES并创建索引
CreateIndexResponse resp = client.indices().create(request, RequestOptions.DEFAULT);
System.out.println(resp);
}
}
2.2、exists & delete
2.2.1、exists
@Test
public void existIndex() throws IOException {
// 1、创建Request对象
GetIndexRequest request = new GetIndexRequest(index);
// 2、通过client对象执行
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
//3、打印返回结果
System.out.println(exists);
}
2.2.2、delete
@Test
public void deleteIndex() throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest(index);
// 删除
client.indices().delete(request, RequestOptions.DEFAULT);
}
三、操作文档
3.1、添加文档
3.1.1、创建person实体类
使用到lombok, 注意idea中需要安装lombok插件
package com.chb.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Person {
private Integer id;
private String name;
private Integer age;
private Date birthday;
}
3.1.2、为了能够将person对象转为json字符串,引入依赖
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.3.5</version>
</dependency>
3.1.3、逻辑实现
3.1.3.1、id不用序列化,birthday字段是yyyy-MM-dd
格式,不能序列化为Date
3.1.3.2、执行
package com.chb.test;
import com.chb.bean.Person;
import com.chb.utils.ESClient;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.Test;
import java.io.IOException;
import java.util.Date;
public class DocDemo {
String index = "person";
RestHighLevelClient client = ESClient.getClient();
ObjectMapper mapper = new ObjectMapper();
@Test
public void createDoc() throws IOException {
// 1、准备json数据
Person person = new Person(1, "chb", 23, new Date());
String json = mapper.writeValueAsString(person);
// 准备request对象用于添加数据
IndexRequest request = new IndexRequest(index);
request.source(json, XContentType.JSON); // 添加数据
// 3、通过client对象执行, 注意此处与操作Index的区别 client.indices().create
IndexResponse resp = client.index(request, RequestOptions.DEFAULT);
// 4、打印结果
System.out.println(resp);
}
}
由于忽略了id , 所以_id
是ES自动生成
3.1.3.3、java.lang.NoSuchFieldError: FAIL_ON_SYMBOL_HASH_OVERFLOW
jackson版本过低, 引入2.11.4后解决。
3.2、修改文档,基于doc
@Test
public void updateDoc() throws IOException {
// 1、创建一个Map, 指定需要修改的内容
Map<String, Object> doc = new HashMap<String, Object>();
doc.put("name", "张三");
String docId = "N67mgXgB_tiW03WV73UZ";
// 2、创建request对象,封装数据
UpdateRequest updateRequest = new UpdateRequest(index, docId);
updateRequest.doc(doc);
// 3、执行
UpdateResponse resp = client.update(updateRequest, RequestOptions.DEFAULT);
// 结果
System.out.println(resp.getResult().toString());
}
3.3、删除文档
@Test
public void deleteDoc() throws IOException {
DeleteRequest request = new DeleteRequest(index, "N67mgXgB_tiW03WV73UZ");
client.delete(request, RequestOptions.DEFAULT);
}
3.4、批量操作
3.4.1、批量增加
注意可以通过id()
设置_doc
@Test
public void bulkCreateDoc() throws IOException {
// 1、准备json数据
Person person1 = new Person(3, "张三", 33, new Date());
Person person2 = new Person(4, "李四", 44, new Date());
Person person3 = new Person(5, "王五", 55, new Date());
String json1 = mapper.writeValueAsString(person1);
String json2 = mapper.writeValueAsString(person2);
String json3 = mapper.writeValueAsString(person3);
// 准备request对象用于添加数据
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.add(new IndexRequest(index).id(person1.getId().toString()).source(json1, XContentType.JSON));
bulkRequest.add(new IndexRequest(index).id(person2.getId().toString()).source(json2, XContentType.JSON));
bulkRequest.add(new IndexRequest(index).id(person3.getId().toString()).source(json3, XContentType.JSON));
// 3、通过client对象执行, 注意此处与操作Index的区别 client.indices().create
BulkResponse resp = client.bulk(bulkRequest, RequestOptions.DEFAULT);
// 4、打印结果
System.out.println(resp);
}
3.4.2、批量删除
@Test
public void bulkDeleteDoc() throws IOException {
// DeleteRequest request = new DeleteRequest(index, "N67mgXgB_tiW03WV73UZ");
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.add(new DeleteRequest(index, "3"));
bulkRequest.add(new DeleteRequest(index, "4"));
bulkRequest.add(new DeleteRequest(index, "5"));
client.bulk(bulkRequest, RequestOptions.DEFAULT);
}
更多推荐
已为社区贡献15条内容
所有评论(0)