Docker启动测试写入查询Elasticsearch.

安装

  1. docker pull elasticsearch:7.5.1 # 拉镜像
  2. docker pull kibana:7.5.1 #拉kibana 镜像
  3. #创建相同的网络
  4. docker network create somenetwork
  5. docker run -d --name elasticsearch --net somenetwork -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.5.1
  6. docker run -d --name kibana --net somenetwork -p 5601:5601 kibana:7.5.1

打开 http://127.0.0.1:5601 查看 kibana 是否启动成功,如果出现 kibana not start xxx yet , 说明kibana启动没有成功,此时应该关闭重启。

  1. docker ps
  2. docker stop xxxxxxxx
  3. docker start xxxxxxxx # 重启镜像

出现kibana界面说明启动成功

Java环境

添加依赖

SpringBoot版本: 2.2.2.RELEASE

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
  4. </dependency>
  1. /**
  2. * @author chenshun00
  3. * @since 2020/1/3 3:36 PM
  4. */
  5. @Service
  6. public class ESClientFactory {
  7. //todo 换
  8. private static final String HOST = "es01";
  9. //todo 换
  10. private static final int PORT = 9200;
  11. private static final String SCHEMA = "http";
  12. private static final int CONNECT_TIME_OUT = 5000;
  13. private static final int SOCKET_TIME_OUT = 40000;
  14. private static final int CONNECTION_REQUEST_TIME_OUT = 5000;
  15. private static final int MAX_CONNECT_NUM = 100;
  16. private static final int MAX_CONNECT_PER_ROUTE = 100;
  17. private static HttpHost HTTP_HOST = new HttpHost(HOST, PORT, SCHEMA);
  18. private static RestClientBuilder builder;
  19. private static RestHighLevelClient restHighLevelClient;
  20. static {
  21. init();
  22. }
  23. private static void init() {
  24. builder = RestClient.builder(HTTP_HOST);
  25. setConnectTimeOutConfig();
  26. setMutiConnectConfig();
  27. restHighLevelClient = new RestHighLevelClient(builder);
  28. }
  29. private static void setConnectTimeOutConfig() {
  30. builder.setRequestConfigCallback(requestConfigBuilder -> {
  31. requestConfigBuilder.setConnectTimeout(CONNECT_TIME_OUT);
  32. requestConfigBuilder.setSocketTimeout(SOCKET_TIME_OUT);
  33. requestConfigBuilder.setConnectionRequestTimeout(CONNECTION_REQUEST_TIME_OUT);
  34. return requestConfigBuilder;
  35. });
  36. }
  37. private static void setMutiConnectConfig() {
  38. builder.setHttpClientConfigCallback(httpClientBuilder -> {
  39. httpClientBuilder.setMaxConnTotal(MAX_CONNECT_NUM);
  40. httpClientBuilder.setMaxConnPerRoute(MAX_CONNECT_PER_ROUTE);
  41. return httpClientBuilder;
  42. });
  43. }
  44. public static RestHighLevelClient getHighLevelClient() {
  45. return restHighLevelClient;
  46. }
  47. }

客户端中使用 RestHighLevelClient 进行查询插入操作.

bulk

  1. class Go implements Runnable {
  2. @Override
  3. public void run() {
  4. BulkRequest request = new BulkRequest();
  5. for (int j = 0; j < 500; j++) {
  6. int ii = RandomUtils.nextInt(0, 100);
  7. Map<String, Object> result = new HashMap<>();
  8. result.put("app_id", "xxxx");
  9. result.put("user_id", "xxxx-" + ii);
  10. result.put("created", new Date());
  11. result.put("updated", new Date());
  12. result.put("data_status", "normal");
  13. result.put("int_1", RandomUtils.nextInt(10000, 1000000));
  14. result.put("float_1", RandomUtils.nextFloat(10000F, 1000000F));
  15. result.put("input_1", RandomStringUtils.randomAlphabetic(100, 300));
  16. result.put("text_1", textMap.get(RandomUtils.nextInt(0, 16)));
  17. result.put("date_1", new Date());
  18. request.add(new IndexRequest(index, "_doc", RandomStringUtils.randomAlphanumeric(30)).source(result, XContentType.JSON));
  19. try {
  20. Thread.sleep(20L);
  21. } catch (InterruptedException ignore) {
  22. }
  23. }
  24. try {
  25. //批量操作
  26. BulkResponse bulk = ESClientFactory.getHighLevelClient().bulk(request, RequestOptions.DEFAULT);
  27. log.info("[{}]", JSONObject.toJSONString(bulk));
  28. } catch (IOException e) {
  29. e.printStackTrace();
  30. }
  31. }
  32. }