Browse Source

add manual load data

kerryzhang 5 years ago
parent
commit
4cee2dc244

+ 4 - 1
elastic-publish-service/req/main-controller.http View File

@@ -21,7 +21,7 @@ Content-Type: application/json
21 21
 
22 22
 {
23 23
   "agencyPath": "/1",
24
-  "keyword": "b楼",
24
+  "keyword": "科苑一区",
25 25
   "page": 1,
26 26
   "size": 10,
27 27
   "regulatoryLevelId": "",
@@ -38,6 +38,9 @@ GET http://{{host}}/load
38 38
 
39 39
 ### load data
40 40
 
41
+GET http://{{host}}/set-load
42
+
43
+### load data
41 44
 
42 45
 
43 46
 GET http://{{host}}/loadByAgencyIds?agencyIds=190084,196581

+ 0 - 4
elastic-publish-service/src/main/java/com/vcarecity/publish/constants/ElasticConstant.java View File

@@ -9,9 +9,6 @@ import java.time.format.DateTimeFormatter;
9 9
 public class ElasticConstant {
10 10
     public static final long CHECK_TIME_INTERVAL = 1000 * 3 * 60;
11 11
 
12
-    public static final String UNIT_AGENCY_INDEX = "unit-agency";
13
-
14
-
15 12
     public static final int QUERY_PAGE_COUNT = 2000;
16 13
 
17 14
 
@@ -22,5 +19,4 @@ public class ElasticConstant {
22 19
     public static final DateTimeFormatter ES_DT_FORMATTER = DateTimeFormatter.ofPattern(ES_DATETIME_PATTERN);
23 20
 
24 21
 
25
-
26 22
 }

+ 5 - 3
elastic-publish-service/src/main/java/com/vcarecity/publish/controller/MainController.java View File

@@ -3,7 +3,6 @@ package com.vcarecity.publish.controller;
3 3
 import com.vcarecity.publish.annotation.SkipMethod;
4 4
 import com.vcarecity.publish.api.ApiResult;
5 5
 import com.vcarecity.publish.api.ApiStatus;
6
-import com.vcarecity.publish.constants.ElasticConstant;
7 6
 import com.vcarecity.publish.elastic.service.ElasticLoadDataService;
8 7
 import com.vcarecity.publish.elastic.service.ElasticUpdateService;
9 8
 import com.vcarecity.publish.entity.UnitAgencyMergeEntity;
@@ -38,6 +37,9 @@ public class MainController implements PrepareController {
38 37
     private final ElasticUpdateService elasticUpdateService;
39 38
     private final LoadDataStatusService loadDataStatusService;
40 39
 
40
+    @Value("${app.index.name}")
41
+    private String indexName;
42
+
41 43
     public MainController(UnitService unitService,
42 44
                           ElasticLoadDataService elasticLoadDataService,
43 45
                           LoadDataStatusService loadDataStatusService,
@@ -119,7 +121,7 @@ public class MainController implements PrepareController {
119 121
     @PostMapping("/query-unit-agency")
120 122
     public ApiResult queryUnitAgency(@RequestBody @Valid UnitAgencyQuery unitAgencyQuery) throws IOException {
121 123
 
122
-        TotalResultDTO<List<UnitIdDTO>> totalResult = unitService.getUnitAgencyByQuery(unitAgencyQuery, UnitIdDTO.class, ElasticConstant.UNIT_AGENCY_INDEX);
124
+        TotalResultDTO<List<UnitIdDTO>> totalResult = unitService.getUnitAgencyByQuery(unitAgencyQuery, UnitIdDTO.class, indexName);
123 125
 
124 126
         return ApiResult.builder()
125 127
                 .status(ApiStatus.SUCCESS_CODE)
@@ -130,7 +132,7 @@ public class MainController implements PrepareController {
130 132
 
131 133
     @PostMapping("/test/query-unit-agency")
132 134
     public ApiResult testQueryUnitAgency(@RequestBody @Valid UnitAgencyQuery unitAgencyQuery) throws IOException {
133
-        TotalResultDTO<List<UnitAgencyDTO>> totalResult = unitService.getUnitAgencyByQuery(unitAgencyQuery, UnitAgencyDTO.class, ElasticConstant.UNIT_AGENCY_INDEX);
135
+        TotalResultDTO<List<UnitAgencyDTO>> totalResult = unitService.getUnitAgencyByQuery(unitAgencyQuery, UnitAgencyDTO.class, indexName);
134 136
         return ApiResult.builder()
135 137
                 .status(ApiStatus.SUCCESS_CODE)
136 138
                 .data(totalResult.getData())

+ 5 - 2
elastic-publish-service/src/main/java/com/vcarecity/publish/elastic/service/impl/ElasticLoadDataServiceImpl.java View File

@@ -7,6 +7,7 @@ import com.vcarecity.publish.elastic.service.ElasticUpdateService;
7 7
 import com.vcarecity.publish.entity.UnitAgencyMergeEntity;
8 8
 import com.vcarecity.publish.mapper.UnitMapper;
9 9
 import lombok.extern.slf4j.Slf4j;
10
+import org.springframework.beans.factory.annotation.Value;
10 11
 import org.springframework.stereotype.Service;
11 12
 
12 13
 import java.io.IOException;
@@ -25,7 +26,10 @@ public class ElasticLoadDataServiceImpl implements ElasticLoadDataService {
25 26
     private final ElasticUpdateService elasticUpdateService;
26 27
     private final ElasticIndexService elasticIndexService;
27 28
     private final int pageCount;
28
-    private final String index;
29
+
30
+
31
+    @Value("${app.index.name}")
32
+    private String index;
29 33
 
30 34
     public ElasticLoadDataServiceImpl(UnitMapper unitMapper,
31 35
                                       ElasticUpdateService elasticUpdateService,
@@ -34,7 +38,6 @@ public class ElasticLoadDataServiceImpl implements ElasticLoadDataService {
34 38
         this.elasticUpdateService = elasticUpdateService;
35 39
         this.elasticIndexService = elasticIndexService;
36 40
         this.pageCount = ElasticConstant.QUERY_PAGE_COUNT;
37
-        this.index = ElasticConstant.UNIT_AGENCY_INDEX;
38 41
     }
39 42
 
40 43
 

+ 7 - 2
elastic-publish-service/src/main/java/com/vcarecity/publish/elastic/service/impl/ElasticQueryServiceImpl.java View File

@@ -1,6 +1,5 @@
1 1
 package com.vcarecity.publish.elastic.service.impl;
2 2
 
3
-import com.vcarecity.publish.constants.ElasticConstant;
4 3
 import com.vcarecity.publish.elastic.service.ElasticQueryService;
5 4
 import lombok.extern.slf4j.Slf4j;
6 5
 import org.elasticsearch.action.search.SearchRequest;
@@ -11,6 +10,7 @@ import org.elasticsearch.index.query.QueryBuilders;
11 10
 import org.elasticsearch.index.query.TermQueryBuilder;
12 11
 import org.elasticsearch.search.SearchHit;
13 12
 import org.elasticsearch.search.builder.SearchSourceBuilder;
13
+import org.springframework.beans.factory.annotation.Value;
14 14
 import org.springframework.stereotype.Service;
15 15
 
16 16
 import java.io.IOException;
@@ -25,7 +25,12 @@ public class ElasticQueryServiceImpl implements ElasticQueryService {
25 25
 
26 26
     private final RestHighLevelClient restHighLevelClient;
27 27
 
28
+
29
+    @Value("${app.index.name}")
30
+    private String indexName;
31
+
28 32
     public ElasticQueryServiceImpl(RestHighLevelClient restHighLevelClient) {
33
+
29 34
         this.restHighLevelClient = restHighLevelClient;
30 35
     }
31 36
 
@@ -39,7 +44,7 @@ public class ElasticQueryServiceImpl implements ElasticQueryService {
39 44
         searchSourceBuilder.query(unitId);
40 45
 
41 46
 
42
-        SearchRequest request = new SearchRequest(ElasticConstant.UNIT_AGENCY_INDEX);
47
+        SearchRequest request = new SearchRequest(indexName);
43 48
         request.source(searchSourceBuilder);
44 49
 
45 50
         SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);

+ 10 - 7
elastic-publish-service/src/main/java/com/vcarecity/publish/elastic/service/impl/ElasticUpdateServiceImpl.java View File

@@ -5,8 +5,6 @@ import com.fasterxml.jackson.core.type.TypeReference;
5 5
 import com.fasterxml.jackson.databind.DeserializationFeature;
6 6
 import com.fasterxml.jackson.databind.ObjectMapper;
7 7
 import com.vcarecity.elastic.util.AgencyPathUtil;
8
-import com.vcarecity.elastic.util.SnowFlake;
9
-import com.vcarecity.publish.constants.ElasticConstant;
10 8
 import com.vcarecity.publish.elastic.entity.UnitAgencyDetailEntity;
11 9
 import com.vcarecity.publish.elastic.service.ElasticUpdateService;
12 10
 import com.vcarecity.publish.entity.UnitAgencyMergeEntity;
@@ -24,6 +22,7 @@ import org.elasticsearch.index.reindex.DeleteByQueryRequest;
24 22
 import org.elasticsearch.index.reindex.UpdateByQueryRequest;
25 23
 import org.elasticsearch.script.Script;
26 24
 import org.elasticsearch.script.ScriptType;
25
+import org.springframework.beans.factory.annotation.Value;
27 26
 import org.springframework.cglib.beans.BeanCopier;
28 27
 import org.springframework.stereotype.Service;
29 28
 
@@ -42,7 +41,8 @@ import java.util.Map;
42 41
 @Service
43 42
 public class ElasticUpdateServiceImpl implements ElasticUpdateService {
44 43
 
45
-    public final String index;
44
+    @Value("${app.index.name}")
45
+    public String index;
46 46
 
47 47
     private final RestHighLevelClient restHighLevelClient;
48 48
     private final ObjectMapper objectMapper;
@@ -58,7 +58,7 @@ public class ElasticUpdateServiceImpl implements ElasticUpdateService {
58 58
         objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
59 59
 
60 60
         this.objectMapper = objectMapper;
61
-        this.index = ElasticConstant.UNIT_AGENCY_INDEX;
61
+
62 62
     }
63 63
 
64 64
 
@@ -132,7 +132,10 @@ public class ElasticUpdateServiceImpl implements ElasticUpdateService {
132 132
     public List<UnitAgencyMergeEntity> updateDataByQuery(String key, List<UnitAgencyMergeEntity> data) throws IOException {
133 133
         List<UnitAgencyMergeEntity> addList = new ArrayList<>();
134 134
         final BeanCopier beanCopier = BeanCopier.create(UnitAgencyMergeEntity.class, UnitAgencyDetailEntity.class, false);
135
-        for (UnitAgencyMergeEntity item : data) {
135
+
136
+
137
+        for (int i = 0; i < data.size(); i++) {
138
+            UnitAgencyMergeEntity item = data.get(i);
136 139
 
137 140
             Map<String, Object> params = createElasticDataMap(beanCopier, item);
138 141
             Object value = params.get(key);
@@ -144,7 +147,7 @@ public class ElasticUpdateServiceImpl implements ElasticUpdateService {
144 147
 
145 148
             BulkByScrollResponse response = restHighLevelClient.updateByQuery(request, RequestOptions.DEFAULT);
146 149
 
147
-            logger.info("response:{}", response);
150
+            logger.info("updateDataByQuery.index: {} resp:{}", i, response);
148 151
 
149 152
             if (response.getUpdated() == 0L) {
150 153
                 addList.add(item);
@@ -205,7 +208,7 @@ public class ElasticUpdateServiceImpl implements ElasticUpdateService {
205 208
 
206 209
     private IndexRequest createIndexRequest(String index, Map<String, Object> value) {
207 210
         IndexRequest indexRequest = new IndexRequest(index);
208
-        indexRequest.id(SnowFlake.nextId() + "");
211
+        // indexRequest.id(SnowFlake.nextId() + "");
209 212
         indexRequest.source(value, XContentType.JSON);
210 213
         return indexRequest;
211 214
     }

+ 38 - 23
elastic-publish-service/src/main/java/com/vcarecity/publish/task/CheckUpdateTask.java View File

@@ -9,6 +9,7 @@ import com.vcarecity.publish.service.CheckTimeService;
9 9
 import com.vcarecity.publish.service.LoadDataStatusService;
10 10
 import com.vcarecity.publish.util.DateUtil;
11 11
 import lombok.extern.slf4j.Slf4j;
12
+import org.springframework.beans.factory.annotation.Value;
12 13
 import org.springframework.scheduling.annotation.Scheduled;
13 14
 import org.springframework.stereotype.Component;
14 15
 
@@ -32,6 +33,14 @@ public class CheckUpdateTask {
32 33
     private final UnitMapper unitMapper;
33 34
 
34 35
 
36
+    @Value("${app.index.update-unit}")
37
+    private Boolean updateUnit;
38
+
39
+
40
+    @Value("${app.index.update-agency}")
41
+    private Boolean updateAgency;
42
+
43
+
35 44
     public CheckUpdateTask(CheckTimeService checkTimeService,
36 45
                            ElasticUpdateService elasticUpdateService,
37 46
                            LoadDataStatusService loadDataStatusService, AgencyMapper agencyMapper,
@@ -60,41 +69,47 @@ public class CheckUpdateTask {
60 69
         List<UnitAgencyMergeEntity> updateUaList;
61 70
         String fromDateTime;
62 71
 
63
-        // 获取上一次的时间
64
-        fromDateTime = checkTimeService.getLastDateTime("T_UNIT");
72
+        if (updateUnit) {
73
+
74
+
75
+            // 获取上一次的时间
76
+            fromDateTime = checkTimeService.getLastDateTime("T_UNIT");
65 77
 
66
-        logger.info("start checkUpdateTask on T_UNIT from:{}. to:{}", fromDateTime, toDateTime);
78
+            logger.info("start checkUpdateTask on T_UNIT from:{}. to:{}", fromDateTime, toDateTime);
67 79
 
68
-        try {
69
-            updateUaList = unitMapper.findUpdateUaList(fromDateTime, toDateTime);
80
+            try {
81
+                updateUaList = unitMapper.findUpdateUaList(fromDateTime, toDateTime);
70 82
 
71
-            logger.debug("T_UNIT update list size = {}", updateUaList.size());
83
+                logger.debug("T_UNIT update list size = {}", updateUaList.size());
72 84
 
73
-            if (!updateUaList.isEmpty()) {
74
-                elasticUpdateService.handlerDataChange("unitId", updateUaList);
85
+                if (!updateUaList.isEmpty()) {
86
+                    elasticUpdateService.handlerDataChange("unitId", updateUaList);
87
+                }
88
+                checkTimeService.setLastDateTime("T_UNIT", curCheckTime);
89
+                updateUaList.clear();
90
+            } catch (Exception e) {
91
+                e.printStackTrace();
75 92
             }
76
-            checkTimeService.setLastDateTime("T_UNIT", curCheckTime);
77
-            updateUaList.clear();
78
-        } catch (Exception e) {
79
-            e.printStackTrace();
80 93
         }
81 94
 
82
-        fromDateTime = checkTimeService.getLastDateTime("T_AGENCY");
95
+        if (updateAgency) {
96
+            fromDateTime = checkTimeService.getLastDateTime("T_AGENCY");
83 97
 
84
-        logger.info("start checkUpdateTask on T_AGENCY from:{}. to:{}", fromDateTime, toDateTime);
98
+            logger.info("start checkUpdateTask on T_AGENCY from:{}. to:{}", fromDateTime, toDateTime);
85 99
 
86
-        try {
87
-            updateUaList = agencyMapper.findUpdateUaList(fromDateTime, toDateTime);
100
+            try {
101
+                updateUaList = agencyMapper.findUpdateUaList(fromDateTime, toDateTime);
88 102
 
89
-            logger.debug("T_AGENCY update list size = {}", updateUaList.size());
103
+                logger.debug("T_AGENCY update list size = {}", updateUaList.size());
90 104
 
91
-            if (!updateUaList.isEmpty()) {
92
-                elasticUpdateService.handlerDataChange("agencyId", updateUaList);
105
+                if (!updateUaList.isEmpty()) {
106
+                    elasticUpdateService.handlerDataChange("agencyId", updateUaList);
107
+                }
108
+                checkTimeService.setLastDateTime("T_AGENCY", curCheckTime);
109
+                updateUaList.clear();
110
+            } catch (Exception e) {
111
+                e.printStackTrace();
93 112
             }
94
-            checkTimeService.setLastDateTime("T_AGENCY", curCheckTime);
95
-            updateUaList.clear();
96
-        } catch (Exception e) {
97
-            e.printStackTrace();
98 113
         }
99 114
     }
100 115
 

+ 3 - 0
elastic-publish-service/src/main/resources/application-beta.yml View File

@@ -25,7 +25,10 @@ mybatis:
25 25
 debug: true
26 26
 app:
27 27
   index:
28
+    name: unit-agency
28 29
     config: index-text
29 30
     reload: true
31
+    update-unit: true
32
+    update-agency: false
30 33
   scheduling:
31 34
     enable: true

+ 3 - 0
elastic-publish-service/src/main/resources/application-dev.yml View File

@@ -26,7 +26,10 @@ mybatis:
26 26
 debug: true
27 27
 app:
28 28
   index:
29
+    name: unit-agency
29 30
     config: index-text
30 31
     reload: true
32
+    update-unit: true
33
+    update-agency: false
31 34
   scheduling:
32 35
     enable: false

+ 3 - 0
elastic-publish-service/src/main/resources/application-production.yml View File

@@ -27,7 +27,10 @@ mybatis:
27 27
     map-underscore-to-camel-case: true
28 28
 app:
29 29
   index:
30
+    name: unit-agency
30 31
     config: index-text
31 32
     reload: false
33
+    update-unit: true
34
+    update-agency: false
32 35
   scheduling:
33 36
     enable: true

+ 3 - 2
elastic-publish-service/src/test/java/com/vcarecity/publish/elastic/service/impl/ElasticIndexServiceImplTest.java View File

@@ -1,9 +1,9 @@
1 1
 package com.vcarecity.publish.elastic.service.impl;
2 2
 
3
-import com.vcarecity.publish.constants.ElasticConstant;
4 3
 import com.vcarecity.publish.elastic.service.ElasticIndexService;
5 4
 import org.junit.jupiter.api.Test;
6 5
 import org.springframework.beans.factory.annotation.Autowired;
6
+import org.springframework.beans.factory.annotation.Value;
7 7
 import org.springframework.boot.test.context.SpringBootTest;
8 8
 import org.springframework.test.context.TestPropertySource;
9 9
 
@@ -16,7 +16,8 @@ class ElasticIndexServiceImplTest {
16 16
     @Autowired
17 17
     private ElasticIndexService elasticIndexService;
18 18
 
19
-    public static final String INDEX = ElasticConstant.UNIT_AGENCY_INDEX;
19
+    @Value("${app.index.name}")
20
+    public String INDEX;
20 21
 
21 22
     @Test
22 23
     void indexExist() throws Exception {