ES CRUD

原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/12097679.html

 

Project Directory

 

Maven Dependency

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <modelVersion>4.0.0</modelVersion>
 6 
 7     <groupId>org.example</groupId>
 8     <artifactId>hello-es</artifactId>
 9     <version>1.0-SNAPSHOT</version>
10 
11     <properties>
12         <elasticsearch.version>7.5.0</elasticsearch.version>
13     </properties>
14 
15     <dependencies>
16         <dependency>
17             <groupId>org.elasticsearch</groupId>
18             <artifactId>elasticsearch</artifactId>
19             <version>${elasticsearch.version}</version>
20         </dependency>
21         <dependency>
22             <groupId>org.elasticsearch.client</groupId>
23             <artifactId>transport</artifactId>
24             <version>${elasticsearch.version}</version>
25         </dependency>
26         <dependency>
27             <groupId>org.elasticsearch.client</groupId>
28             <artifactId>elasticsearch-rest-high-level-client</artifactId>
29             <version>${elasticsearch.version}</version>
30         </dependency>
31 
32         <dependency>
33             <groupId>org.apache.logging.log4j</groupId>
34             <artifactId>log4j-core</artifactId>
35             <version>2.12.1</version>
36         </dependency>
37         <dependency>
38             <groupId>org.apache.logging.log4j</groupId>
39             <artifactId>log4j-api</artifactId>
40             <version>2.12.1</version>
41         </dependency>
42         <dependency>
43             <groupId>org.apache.logging.log4j</groupId>
44             <artifactId>log4j-slf4j-impl</artifactId>
45             <version>2.12.1</version>
46         </dependency>
47 
48         <dependency>
49             <groupId>com.fasterxml.jackson.core</groupId>
50             <artifactId>jackson-databind</artifactId>
51             <version>2.10.1</version>
52         </dependency>
53 
54         <dependency>
55             <groupId>org.projectlombok</groupId>
56             <artifactId>lombok</artifactId>
57             <version>1.18.10</version>
58         </dependency>
59 
60         <dependency>
61             <groupId>junit</groupId>
62             <artifactId>junit</artifactId>
63             <version>4.12</version>
64         </dependency>
65     </dependencies>
66 
67     <build>
68         <plugins>
69             <plugin>
70                 <groupId>org.apache.maven.plugins</groupId>
71                 <artifactId>maven-compiler-plugin</artifactId>
72                 <version>3.8.1</version>
73                 <configuration>
74                     <source>1.8</source>
75                     <target>1.8</target>
76                 </configuration>
77             </plugin>
78         </plugins>
79     </build>
80 </project>
View Code

 

log4j

1 log4j.rootLogger=info,stdout
2 log4j.appender.stdout=org.apache.log4j.ConsoleAppender
3 log4j.appender.stdout.Target=System.out
4 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
5 log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c] [%p] - %m%n
View Code

 

Source Code

Book.java

 1 package org.fool.es.model;
 2 
 3 import lombok.Builder;
 4 import lombok.Data;
 5 
 6 import java.util.Date;
 7 
 8 @Data
 9 @Builder
10 public class Book {
11     private String name;
12     private Date publishDate;
13     private Double price;
14 }

 

ES7TransportClientTest.java

 1 package org.fool.es;
 2 
 3 import org.elasticsearch.action.delete.DeleteResponse;
 4 import org.elasticsearch.action.get.GetResponse;
 5 import org.elasticsearch.action.index.IndexResponse;
 6 import org.elasticsearch.action.update.UpdateResponse;
 7 import org.elasticsearch.client.transport.TransportClient;
 8 import org.elasticsearch.common.settings.Settings;
 9 import org.elasticsearch.common.transport.TransportAddress;
10 import org.elasticsearch.common.xcontent.XContentType;
11 import org.elasticsearch.transport.client.PreBuiltTransportClient;
12 import org.fool.es.model.Book;
13 import org.fool.es.util.JsonUtils;
14 import org.junit.After;
15 import org.junit.Before;
16 import org.junit.Test;
17 
18 import java.net.InetAddress;
19 import java.util.Date;
20 
21 public class ES7TransportClientTest {
22     private static final String HOST = "127.0.0.1";
23     private static final int PORT = 9300;
24 
25     private TransportClient client;
26 
27     @Before
28     public void getClient() throws Exception {
29         Settings settings = Settings.builder().put("cluster.name", "haha").build();
30 
31         client = new PreBuiltTransportClient(settings)
32                 .addTransportAddress(new TransportAddress(InetAddress.getByName(HOST), PORT));
33     }
34 
35     @After
36     public void close() {
37         if (client != null) {
38             client.close();
39         }
40     }
41 
42     @Test
43     public void testGet() {
44         GetResponse response = client.prepareGet("book", "_doc", "1").get();
45         System.out.println(response.getSourceAsString());
46     }
47 
48     @Test
49     public void testIndex() {
50         Book book = Book.builder().name("Hello ES").publishDate(new Date()).price(99.0).build();
51 
52         IndexResponse indexResponse = client.prepareIndex("book", "_doc", "1")
53                 .setSource(JsonUtils.objectToJson(book), XContentType.JSON).get();
54 
55         System.out.println(indexResponse.getIndex());
56         System.out.println(indexResponse.getType());
57         System.out.println(indexResponse.getId());
58         System.out.println(indexResponse.status());
59     }
60 
61     @Test
62     public void testUpdate() {
63         Book book = Book.builder().name("Hello ES Plus").publishDate(new Date()).price(88.0).build();
64 
65         UpdateResponse response = client.prepareUpdate("book", "_doc", "1")
66                 .setDoc(JsonUtils.objectToJson(book), XContentType.JSON).get();
67 
68         System.out.println(response.getIndex());
69         System.out.println(response.getType());
70         System.out.println(response.getId());
71         System.out.println(response.status());
72     }
73 
74     @Test
75     public void testDelete() {
76         DeleteResponse response = client.prepareDelete("book", "_doc", "1").get();
77 
78         System.out.println(response.getIndex());
79         System.out.println(response.getType());
80         System.out.println(response.getId());
81         System.out.println(response.status());
82     }
83 }

Note: 端口号是9300

TransportClient 是一种轻量级的方法,它通过Socket与Elasticsearch集群连接,是基于Netty 线程池的方式。

 

ES7RestClientTest.java

 1 package org.fool.es;
 2 
 3 import org.apache.http.HttpHost;
 4 import org.elasticsearch.action.delete.DeleteRequest;
 5 import org.elasticsearch.action.delete.DeleteResponse;
 6 import org.elasticsearch.action.get.GetRequest;
 7 import org.elasticsearch.action.get.GetResponse;
 8 import org.elasticsearch.action.index.IndexRequest;
 9 import org.elasticsearch.action.index.IndexResponse;
10 import org.elasticsearch.action.update.UpdateRequest;
11 import org.elasticsearch.action.update.UpdateResponse;
12 import org.elasticsearch.client.RequestOptions;
13 import org.elasticsearch.client.RestClient;
14 import org.elasticsearch.client.RestHighLevelClient;
15 import org.elasticsearch.common.xcontent.XContentType;
16 import org.fool.es.model.Book;
17 import org.fool.es.util.JsonUtils;
18 import org.junit.After;
19 import org.junit.Before;
20 import org.junit.Test;
21 
22 import java.util.Date;
23 import java.util.Objects;
24 
25 public class ES7RestClientTest {
26     private static final String HOST = "127.0.0.1";
27     private static final int PORT = 9200;
28 
29     private RestHighLevelClient client;
30 
31     @Before
32     public void getClient() {
33         client = new RestHighLevelClient(RestClient.builder(new HttpHost(HOST, PORT, "http")));
34     }
35 
36     @After
37     public void close() throws Exception {
38         if (client != null) {
39             client.close();
40         }
41     }
42 
43     @Test
44     public void testGet() throws Exception {
45         GetRequest request = new GetRequest("book", "1");
46 
47         GetResponse response = client.get(request, RequestOptions.DEFAULT);
48 
49         System.out.println(response.getSourceAsString());
50     }
51 
52     @Test
53     public void testIndex() throws Exception {
54         Book book = Book.builder().name("Hello ES").publishDate(new Date()).price(99.0).build();
55 
56         IndexRequest indexRequest = new IndexRequest("book")
57                 .source(Objects.requireNonNull(JsonUtils.objectToJson(book)), XContentType.JSON).id("1");
58 
59         IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
60 
61         System.out.println(indexResponse.getIndex());
62         System.out.println(indexResponse.getType());
63         System.out.println(indexResponse.getId());
64         System.out.println(indexResponse.status());
65     }
66 
67     @Test
68     public void testUpdate() throws Exception {
69         Book book = Book.builder().name("Hello ES Plus").publishDate(new Date()).price(88.0).build();
70 
71         UpdateRequest request = new UpdateRequest("book", "1");
72         request.doc(JsonUtils.objectToJson(book), XContentType.JSON);
73         UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
74 
75         System.out.println(response.getIndex());
76         System.out.println(response.getType());
77         System.out.println(response.getId());
78         System.out.println(response.status());
79     }
80 
81     @Test
82     public void testDelete() throws Exception {
83         DeleteRequest request = new DeleteRequest("book").id("1");
84         DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
85 
86         System.out.println(response.getIndex());
87         System.out.println(response.getType());
88         System.out.println(response.getId());
89         System.out.println(response.status());
90     }
91 }

Note: 端口号是9200

Elasticsearch 官方推出Java High Level REST Client,它是基于Java Low Level REST Client的封装,并且API接收参数和返回值和TransportClient是一样的,使得代码迁移变得容易并且支持了RESTful的风格,兼容了这两种客户端的优点。

posted @ 2019-12-25 16:58  李白与酒  阅读(1209)  评论(0编辑  收藏  举报