소스 검색

es版本修改,使用restHigHLevelClient直接实现,后续还要搞配置
1.新增根据classCode和condition查询设备数据
2.根据设备id查询设备数据

易涛 3 년 전
부모
커밋
0413d42adb

+ 14 - 6
pom.xml

@@ -96,17 +96,25 @@
     </dependency>
 
     <dependency>
-        <groupId>org.springframework.data</groupId>
-        <artifactId>spring-data-elasticsearch</artifactId>
-        <version>3.2.13.RELEASE</version>
+        <groupId>org.elasticsearch</groupId>
+        <artifactId>elasticsearch</artifactId>
+        <version>7.13.2</version>
     </dependency>
 
-    <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-elasticsearch -->
     <dependency>
-        <groupId>org.springframework.boot</groupId>
-        <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
+        <groupId>org.elasticsearch.client</groupId>
+        <artifactId>elasticsearch-rest-high-level-client</artifactId>
+        <version>7.13.2</version>
+    </dependency>
+    <dependency>
+        <groupId>org.elasticsearch.client</groupId>
+        <artifactId>elasticsearch-rest-client</artifactId>
+        <version>7.13.2</version>
     </dependency>
 
+
+    <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-elasticsearch -->
+
     <dependency>
         <groupId>com.persagy</groupId>
         <artifactId>apm-mybatis</artifactId>

+ 11 - 0
src/main/java/com/persagy/apm/constants/ElasticStrConstant.java

@@ -0,0 +1,11 @@
+package com.persagy.apm.constants;
+
+/**
+ * @author 易涛
+ * @version 1.0
+ * @date 2021/9/9 15:59
+ */
+public interface ElasticStrConstant {
+
+    String INDEX_NAME  = "rwd_object";
+}

+ 22 - 3
src/main/java/com/persagy/apm/controller/GroupManagementController.java

@@ -3,14 +3,18 @@ package com.persagy.apm.controller;
 import com.alibaba.fastjson.JSONArray;
 import com.persagy.apm.common.response.CommonResult;
 import com.persagy.apm.common.utils.ResultHelper;
+import com.persagy.apm.model.dto.GroupManagementDTO;
+import com.persagy.apm.model.vo.GroupManagementVo;
 import com.persagy.apm.model.vo.GroupTreeDetailVO;
 import com.persagy.apm.model.vo.GroupTreeVO;
+import com.persagy.apm.service.GroupManagementService;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.BeanUtils;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -33,8 +37,11 @@ import java.util.Objects;
 @RequestMapping("group-management")
 public class GroupManagementController {
 
+    @Autowired
+    private GroupManagementService managementService;
+
     @ApiOperation(value = "获取分组树")
-    @PostMapping("/queryGroupTree")
+    @PostMapping("/eq_groups/tree")
     public CommonResult<List<GroupTreeVO>> queryGroupTree(){
         return ResultHelper.single(getGroupTree());
     }
@@ -59,8 +66,8 @@ public class GroupManagementController {
         }
     }
     
-    @ApiOperation(value = "根据id获取分组详情信息")
-    @PostMapping("/queryTreeById")
+    @ApiOperation(value = "获取分组名称")
+    @PostMapping("/eq_groups/get")
     public CommonResult<GroupTreeDetailVO> queryTreeById(@RequestBody String id){
         List<GroupTreeVO> groupTreeVOList = getGroupTree();
         GroupTreeDetailVO groupTreeDetailVO = new GroupTreeDetailVO();
@@ -91,4 +98,16 @@ public class GroupManagementController {
         }
         return groupTreeDetailVO;
     }
+
+    @ApiOperation(value = "分组条件(设备类+查询条件)+项目 获取设备")
+    @PostMapping("/equips/query")
+    public CommonResult<List<GroupManagementVo>> equipsQuery(@RequestBody GroupManagementDTO managementDTO){
+        return ResultHelper.single(managementService.equipsQuery(managementDTO));
+    }
+
+    @ApiOperation(value = "获取设备名称")
+    @PostMapping("/equips/get")
+    public CommonResult<GroupManagementVo> equipsQuery(@RequestBody String id) {
+        return ResultHelper.single(managementService.equipsGet(id));
+    }
 }

+ 0 - 14
src/main/java/com/persagy/apm/dao/GroupManagementMapper.java

@@ -1,14 +0,0 @@
-package com.persagy.apm.dao;
-
-import com.persagy.apm.model.GroupManagement;
-import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
-import org.springframework.stereotype.Repository;
-
-/**
- * @author 易涛
- * @version 1.0
- * @date 2021/9/7 18:10
- */
-@Repository
-public interface GroupManagementMapper extends ElasticsearchRepository<GroupManagement,String> {
-}

+ 17 - 14
src/main/java/com/persagy/apm/model/GroupManagement.java

@@ -1,9 +1,11 @@
 package com.persagy.apm.model;
 
-import org.springframework.data.annotation.Id;
+import lombok.Data;
+/*import org.springframework.data.annotation.Id;
 import org.springframework.data.elasticsearch.annotations.Document;
 import org.springframework.data.elasticsearch.annotations.Field;
 import org.springframework.data.elasticsearch.annotations.FieldType;
+import org.springframework.data.elasticsearch.annotations.Setting;*/
 
 import java.time.LocalDateTime;
 
@@ -12,38 +14,39 @@ import java.time.LocalDateTime;
  * @version 1.0
  * @date 2021/9/7 18:11
  */
-@Document(indexName = "rwd_object")
+/*@Document(indexName = "rwd_object")
+@Setting(settingPath = "commaAnalyzer.json")*/
+@Data
 public class GroupManagement {
-
-    @Id
-    @Field(type = FieldType.Text)
+    //@Id
+    //@Field(type = FieldType.Text)
     private String id;
 
-    @Field(name = "class_code",type = FieldType.Text)
+    //@Field(name = "class_code",type = FieldType.Text)
     private String classCode;
 
-    @Field(type = FieldType.Text)
+    //@Field(type = FieldType.Text,analyzer = "comma")
     private String infos;
 
-    @Field(name = "local_id",type = FieldType.Text)
+    //@Field(name = "local_id",type = FieldType.Text)
     private String localId;
 
-    @Field(name = "local_name",type = FieldType.Text)
+    //@Field(name = "local_name",type = FieldType.Text)
     private String localName;
 
-    @Field(type = FieldType.Text)
+    //@Field(type = FieldType.Text)
     private String name;
 
-    @Field(name = "obj_type",type = FieldType.Text)
+    //@Field(name = "obj_type",type = FieldType.Text)
     private String objType;
 
-    @Field(name = "project_id",type = FieldType.Text)
+    //@Field(name = "project_id",type = FieldType.Text)
     private String projectId;
 
-    @Field(name = "group_code",type = FieldType.Text)
+    //@Field(name = "group_code",type = FieldType.Text)
     private String groupCode;
 
-    @Field(name = "update_time")
+    //@Field(name = "update_time")
     private LocalDateTime updateTime;
 
     private Long valid;

+ 21 - 0
src/main/java/com/persagy/apm/model/dto/GroupManagementDTO.java

@@ -0,0 +1,21 @@
+package com.persagy.apm.model.dto;
+
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+/**
+ * @author 易涛
+ * @version 1.0
+ * @date 2021/9/8 16:46
+ */
+@ApiModel("设备信息")
+@Data
+public class GroupManagementDTO {
+
+    @ApiModelProperty("对象类编码")
+    private String classCode;
+
+    @ApiModelProperty("查询条件")
+    private String condition;
+}

+ 21 - 0
src/main/java/com/persagy/apm/model/vo/GroupManagementVo.java

@@ -0,0 +1,21 @@
+package com.persagy.apm.model.vo;
+
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+/**
+ * @author 易涛
+ * @version 1.0
+ * @date 2021/9/7 18:11
+ */
+@ApiModel("设备信息")
+@Data
+public class GroupManagementVo {
+
+    @ApiModelProperty("设备id")
+    private String id;
+
+    @ApiModelProperty("设备名称")
+    private String name;
+}

+ 23 - 0
src/main/java/com/persagy/apm/service/GroupManagementService.java

@@ -1,9 +1,32 @@
 package com.persagy.apm.service;
 
+import com.persagy.apm.model.dto.GroupManagementDTO;
+import com.persagy.apm.model.vo.GroupManagementVo;
+
+import java.io.IOException;
+import java.util.List;
+
 /**
  * @author 易涛
  * @version 1.0
  * @date 2021/9/8 14:17
  */
 public interface GroupManagementService{
+    /**
+     * 分组条件(设备类+查询条件)+项目 获取设备
+     * @param managementDTO
+     * @author 易涛
+     * @date 2021/9/8 16:54
+     * @return java.util.List<com.persagy.apm.model.vo.GroupManagementVo>
+     */
+    List<GroupManagementVo> equipsQuery(GroupManagementDTO managementDTO);
+    
+    /**
+     *
+     * @param id
+     * @author 易涛
+     * @date 2021/9/9 16:04 
+     * @return com.persagy.apm.model.vo.GroupManagementVo
+     */
+    GroupManagementVo equipsGet(String id);
 }

+ 69 - 0
src/main/java/com/persagy/apm/service/impl/GroupManagementServiceImpl.java

@@ -1,8 +1,32 @@
 package com.persagy.apm.service.impl;
 
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.persagy.apm.configuration.ElasticSearchConfig;
+import com.persagy.apm.model.dto.GroupManagementDTO;
+import com.persagy.apm.model.vo.GroupManagementVo;
 import com.persagy.apm.service.GroupManagementService;
+import org.elasticsearch.action.get.GetRequest;
+import org.elasticsearch.action.get.GetResponse;
+import org.elasticsearch.action.search.SearchRequest;
+import org.elasticsearch.action.search.SearchResponse;
+import org.elasticsearch.client.RequestOptions;
+import org.elasticsearch.client.RestHighLevelClient;
+import org.elasticsearch.index.query.BoolQueryBuilder;
+import org.elasticsearch.index.query.QueryBuilders;
+import org.elasticsearch.rest.RestStatus;
+import org.elasticsearch.search.SearchHit;
+import org.elasticsearch.search.SearchHits;
+import org.elasticsearch.search.builder.SearchSourceBuilder;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import static com.persagy.apm.constants.ElasticStrConstant.INDEX_NAME;
+
 /**
  * @author 易涛
  * @version 1.0
@@ -10,4 +34,49 @@ import org.springframework.stereotype.Service;
  */
 @Service
 public class GroupManagementServiceImpl implements GroupManagementService {
+
+    @Autowired
+    private RestHighLevelClient client;
+
+    @Override
+    public List<GroupManagementVo> equipsQuery(GroupManagementDTO managementDTO) {
+        List<GroupManagementVo> groupManagementVoList = new ArrayList<>();
+        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
+        queryBuilder.must(QueryBuilders.matchQuery("class_code",managementDTO.getClassCode()));
+        queryBuilder.must(QueryBuilders.matchPhraseQuery("infos",managementDTO.getCondition()));
+        queryBuilder.must(QueryBuilders.termQuery("valid",1));
+        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
+        sourceBuilder.query(queryBuilder);
+        SearchRequest rq = new SearchRequest(INDEX_NAME);
+        rq.source(sourceBuilder);
+        try {
+            SearchResponse searchResponse = client.search(rq, RequestOptions.DEFAULT);
+            // 根据状态和数据条数验证是否返回了数据
+            if (RestStatus.OK.equals(searchResponse.status())&&searchResponse.getHits().getTotalHits().value>0) {
+                SearchHits hits = searchResponse.getHits();
+                for (SearchHit hit : hits) {
+                    // 将 JSON 转换成对象
+                    GroupManagementVo managementVo = JSON.parseObject(hit.getSourceAsString(), GroupManagementVo.class);
+                    groupManagementVoList.add(managementVo);
+                }
+            }
+        }catch (IOException e){
+            throw new RuntimeException("es查询出错",e);
+        }
+        return groupManagementVoList;
+    }
+
+    @Override
+    public GroupManagementVo equipsGet(String id) {
+        GetRequest getRequest = new GetRequest(INDEX_NAME,id);
+        try {
+            GetResponse getResponse = client.get(getRequest,RequestOptions.DEFAULT);
+            if(getResponse.isExists()){
+                return JSONObject.parseObject(getResponse.getSourceAsString(),GroupManagementVo.class);
+            }
+        }catch (IOException e){
+            throw new RuntimeException("es查询出错",e);
+        }
+        return null;
+    }
 }