API操作

新建工程并导入依赖:

  1. <dependency>
  2. <groupId>org.apache.httpcomponents</groupId>
  3. <artifactId>httpclient</artifactId>
  4. <version>4.5.5</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.apache.httpcomponents</groupId>
  8. <artifactId>httpmime</artifactId>
  9. <version>4.3.6</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>io.searchbox</groupId>
  13. <artifactId>jest</artifactId>
  14. <version>5.3.3</version>
  15. </dependency>
  16. <dependency>
  17. <groupId>net.java.dev.jna</groupId>
  18. <artifactId>jna</artifactId>
  19. <version>4.5.2</version>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.codehaus.janino</groupId>
  23. <artifactId>commons-compiler</artifactId>
  24. <version>2.7.8</version>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.elasticsearch</groupId>
  28. <artifactId>elasticsearch</artifactId>
  29. <version>6.6.0</version>
  30. </dependency>

写数据

1)单条数据写入

  1. //1.创建ES客户端构建器
  2. JestClientFactory factory = new JestClientFactory();
  3. //2.创建ES客户端连接地址
  4. HttpClientConfig httpClientConfig = new HttpClientConfig.Builder("http://hadoop102:9200").build();
  5. //3.设置ES连接地址
  6. factory.setHttpClientConfig(httpClientConfig);
  7. //4.获取ES客户端连接
  8. JestClient jestClient = factory.getObject();
  9. //5.构建ES插入数据对象
  10. Index index = new Index.Builder("{\n" +
  11. " \"id\":\"1002\",\n" +
  12. " \"movie_name\":\”姜子牙\”\n" +
  13. "}").index("movie_test1").type("_doc").id("1002").build();
  14. //6.执行插入数据操作
  15. jestClient.execute(index);
  16. //7.关闭连接
  17. jestClient.shutdownClient();
  18. 2)批量数据写入
  19. //1.创建客户端对象
  20. JestClientFactory jestClientFactory = new JestClientFactory();
  21. //2.设置连接参数
  22. HttpClientConfig httpClientConfig = new HttpClientConfig
  23. .Builder("http://hadoop102:9200")
  24. .build();
  25. jestClientFactory.setHttpClientConfig(httpClientConfig);
  26. //3.获取客户端对象
  27. JestClient jestClient = jestClientFactory.getObject();
  28. Movie movie1 = new Movie("1005", "二十不惑");
  29. Movie movie2 = new Movie("1006", "三十而立");
  30. Movie movie3 = new Movie("1007", "寄生虫");
  31. Index index1 = new Index.Builder(movie1).id("1005").build();
  32. Index index2 = new Index.Builder(movie2).id("1006").build();
  33. Index index3 = new Index.Builder(movie3).id("1007").build();
  34. Bulk bulk = new Bulk.Builder()
  35. .defaultIndex("movie_test1")
  36. .defaultType("_doc")
  37. .addAction(index1)
  38. .addAction(index2)
  39. .addAction(index3)
  40. .build();
  41. jestClient.execute(bulk);
  42. jestClient.shutdownClient();

读数据

  1. //1.创建ES客户端连接池
  2. JestClientFactory factory = new JestClientFactory();
  3. //2.创建ES客户端连接地址
  4. HttpClientConfig httpClientConfig = new HttpClientConfig.Builder("http://hadoop102:9200").build();
  5. //3.设置ES连接地址
  6. factory.setHttpClientConfig(httpClientConfig);
  7. //4.获取ES客户端连接
  8. JestClient jestClient = factory.getObject();
  9. //5.构建查询数据对象
  10. Search search = new Search.Builder("{\n" +
  11. " \"query\": {\n" +
  12. " \"match\": {\n" +
  13. " \"name\": \"zhangsan\"\n" +
  14. " }\n" +
  15. " }\n" +
  16. "}").addIndex("test5").addType("_doc").build();
  17. //6.执行查询操作
  18. SearchResult searchResult = jestClient.execute(search);
  19. //7.解析查询结果
  20. System.out.println(searchResult.getTotal());
  21. List<SearchResult.Hit<Map, Void>> hits = searchResult.getHits(Map.class);
  22. for (SearchResult.Hit<Map, Void> hit : hits) {
  23. System.out.println(hit.index + "--" + hit.id);
  24. }
  25. //8.关闭连接
  26. jestClient.shutdownClient();