当前位置:   article > 正文

elk部署springboot

elk部署springboot
1.设置es版本:
  1. <properties>
  2. <java.version>1.8</java.version>
  3. <elasticsearch.version>6.8.13</elasticsearch.version>
  4. </properties>
2.导入ES依赖,JSON依赖:
  1. <dependency>
  2. <groupId>com.alibaba</groupId>
  3. <artifactId>fastjson</artifactId>
  4. <version>1.2.54</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.elasticsearch.client</groupId>
  8. <artifactId>elasticsearch-rest-high-level-client</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.elasticsearch.client</groupId>
  12. <artifactId>elasticsearch-rest-client</artifactId>
  13. </dependency>
  14. <dependency>
  15. <groupId>org.elasticsearch</groupId>
  16. <artifactId>elasticsearch</artifactId>
  17. </dependency>
3.创建一个实体类
  1. package cn.com.galaxy.elkTest;
  2. import ch.qos.logback.core.joran.spi.NoAutoStart;
  3. import lombok.Data;
  4. import lombok.NoArgsConstructor;
  5. @Data
  6. public class Employee {
  7. private String name;
  8. private Integer age;
  9. private String text;
  10. private String num;
  11. public Employee() {
  12. }
  13. public Employee(String name, Integer age, String text, String num) {
  14. this.name = name;
  15. this.age = age;
  16. this.text = text;
  17. this.num = num;
  18. }
  19. }
4.测试
  1. package cn.com.galaxy.elkTest;
  2. import com.alibaba.fastjson2.JSON;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.apache.http.HttpHost;
  5. import org.elasticsearch.action.bulk.BulkRequest;
  6. import org.elasticsearch.action.bulk.BulkResponse;
  7. import org.elasticsearch.action.delete.DeleteRequest;
  8. import org.elasticsearch.action.delete.DeleteResponse;
  9. import org.elasticsearch.action.index.IndexRequest;
  10. import org.elasticsearch.action.index.IndexResponse;
  11. import org.elasticsearch.action.search.SearchRequest;
  12. import org.elasticsearch.action.search.SearchResponse;
  13. import org.elasticsearch.action.update.UpdateRequest;
  14. import org.elasticsearch.action.update.UpdateResponse;
  15. import org.elasticsearch.client.RequestOptions;
  16. import org.elasticsearch.client.RestClient;
  17. import org.elasticsearch.client.RestClientBuilder;
  18. import org.elasticsearch.client.RestHighLevelClient;
  19. import org.elasticsearch.client.indices.CreateIndexRequest;
  20. import org.elasticsearch.common.xcontent.XContentType;
  21. import org.elasticsearch.index.query.BoolQueryBuilder;
  22. import org.elasticsearch.index.query.QueryBuilders;
  23. import org.elasticsearch.rest.RestStatus;
  24. import org.elasticsearch.search.SearchHit;
  25. import org.elasticsearch.search.SearchHits;
  26. import org.elasticsearch.search.builder.SearchSourceBuilder;
  27. import org.junit.jupiter.api.Test;
  28. import org.springframework.context.annotation.Bean;
  29. import org.springframework.context.annotation.Configuration;
  30. import java.io.IOException;
  31. import java.util.ArrayList;
  32. @Slf4j
  33. @Configuration
  34. public class EsConfig {
  35. @Bean
  36. public RestHighLevelClient restHighLevelClient() {
  37. RestHighLevelClient client = new RestHighLevelClient(
  38. RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")));
  39. return client;
  40. }
  41. @Test
  42. void testCreateIndex() throws IOException {
  43. HttpHost host = HttpHost.create("http://124.221.27.165:9200");
  44. RestClientBuilder builder = RestClient.builder(host);
  45. RestHighLevelClient client = new RestHighLevelClient(builder);
  46. CreateIndexRequest request = new CreateIndexRequest("user");
  47. client.indices().create(request, RequestOptions.DEFAULT);
  48. client.close();
  49. }
  50. /**
  51. * 批量添加
  52. *
  53. * @throws IOException
  54. */
  55. @Test
  56. void add() throws IOException {
  57. RestHighLevelClient client = restHighLevelClient();
  58. BulkRequest bulkRequest = new BulkRequest();
  59. ArrayList<Employee> users = new ArrayList<>();
  60. users.add(new Employee("李文", 23, "研发工程师", "2352"));
  61. users.add(new Employee("罗文", 17, "测试工程师", "8732"));
  62. users.add(new Employee("徐洁", 22, "算法工程师", "8791"));
  63. users.add(new Employee("罗辑", 31, "高级研发工程师", "8765"));
  64. users.add(new Employee("叶文洁", 70, "资深研发工程师", "8551"));
  65. for (int i = 0; i < users.size(); i++) {
  66. bulkRequest.add(new IndexRequest("test").id("" + (i + 1)).type("pinyin")
  67. .source(JSON.toJSONString(users.get(i)), XContentType.JSON));
  68. }
  69. BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
  70. System.out.println(!bulkResponse.hasFailures());
  71. }
  72. /**
  73. * 添加单条记录
  74. * @throws IOException
  75. */
  76. @Test
  77. void add2() throws IOException {
  78. RestHighLevelClient client = restHighLevelClient();
  79. // 1.要在指定索引下创建文档,所以要先创建索引,再创建文档
  80. IndexRequest request=new IndexRequest();
  81. // index()方法设置索引名;id()方法设置唯一id标识
  82. request.index("test").id("5").type("pinyin");
  83. // 2.创建实体类对象,填充数据
  84. Employee employee=new Employee();
  85. employee.setName("张三丰");
  86. employee.setAge(30);
  87. employee.setNum("23");
  88. employee.setText("添加");
  89. // 3.利用jackson将实体类对象转换成JSON格式字符串
  90. request.source(JSON.toJSONString(employee), XContentType.JSON);
  91. // 5.发送请求,获取响应结果
  92. IndexResponse response = client.index(request, RequestOptions.DEFAULT);
  93. System.out.println("_index: "+response.getIndex());
  94. System.out.println("_id: "+response.getId());
  95. System.out.println("_result: "+response.getResult());
  96. }
  97. /**
  98. * 修改
  99. */
  100. @Test
  101. void update() throws IOException {
  102. RestHighLevelClient client = restHighLevelClient();
  103. UpdateRequest request = new UpdateRequest("test", "pinyin", "1");
  104. Employee user = new Employee("张三", 18, "测试", "1234");
  105. request.doc(JSON.toJSONString(user), XContentType.JSON);
  106. UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);
  107. System.out.println(updateResponse.status() == RestStatus.OK);
  108. }
  109. /**
  110. * 删除
  111. */
  112. @Test
  113. void delete() throws IOException {
  114. RestHighLevelClient client = restHighLevelClient();
  115. HttpHost host = HttpHost.create("http://124.221.27.165:9200");
  116. client = new RestHighLevelClient(RestClient.builder(host));
  117. DeleteRequest request = new DeleteRequest("test", "pinyin", "5");
  118. DeleteResponse deleteResponse = client.delete(request, RequestOptions.DEFAULT);
  119. System.out.println(deleteResponse.status() == RestStatus.OK);
  120. }
  121. /**
  122. * 全查(除id)
  123. *
  124. * @throws Exception
  125. */
  126. @Test
  127. public void select() {
  128. RestHighLevelClient client = restHighLevelClient();
  129. try {
  130. // 构建查询条件
  131. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  132. // 创建查询请求对象,将查询对象配置到其中
  133. SearchRequest searchRequest = new SearchRequest("test");
  134. searchRequest.source(searchSourceBuilder);
  135. // 执行查询,然后处理响应结果
  136. SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
  137. // 根据状态和数据条数验证是否返回了数据
  138. if (RestStatus.OK.equals(searchResponse.status())) {
  139. SearchHits hits = searchResponse.getHits();
  140. for (SearchHit hit : hits) {
  141. // 将 JSON 转换成对象
  142. Employee userInfo = JSON.parseObject(hit.getSourceAsString(), Employee.class);
  143. // 输出查询信息
  144. System.out.println(userInfo.toString());
  145. }
  146. }
  147. } catch (IOException e) {
  148. log.error("", e);
  149. }
  150. }

5.ElasticSearch进阶:各种es查询在java中实现

https://www.cnblogs.com/wk-missQ1/p/16664511.html

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号