|
@@ -2,7 +2,18 @@ package com.yoho.datasync.fullsync.service.impl; |
|
@@ -2,7 +2,18 @@ package com.yoho.datasync.fullsync.service.impl; |
2
|
|
2
|
|
3
|
|
3
|
|
4
|
import com.alibaba.fastjson.JSON;
|
4
|
import com.alibaba.fastjson.JSON;
|
5
|
-import com.google.common.collect.Lists;
|
5
|
+import org.elasticsearch.action.delete.DeleteRequest;
|
|
|
6
|
+import org.elasticsearch.action.delete.DeleteResponse;
|
|
|
7
|
+import org.elasticsearch.action.search.SearchRequest;
|
|
|
8
|
+import org.elasticsearch.action.search.SearchResponse;
|
|
|
9
|
+import org.elasticsearch.index.query.BoolQueryBuilder;
|
|
|
10
|
+import org.elasticsearch.index.query.QueryBuilder;
|
|
|
11
|
+import org.elasticsearch.index.query.QueryBuilders;
|
|
|
12
|
+import org.elasticsearch.index.reindex.BulkByScrollResponse;
|
|
|
13
|
+import org.elasticsearch.index.reindex.DeleteByQueryRequest;
|
|
|
14
|
+import org.elasticsearch.search.builder.SearchSourceBuilder;
|
|
|
15
|
+import org.springframework.data.domain.PageRequest;
|
|
|
16
|
+import org.springframework.data.domain.Pageable;
|
6
|
import com.yoho.datasync.fullsync.dal.repository.yhcommunity.model.UserInfo;
|
17
|
import com.yoho.datasync.fullsync.dal.repository.yhcommunity.model.UserInfo;
|
7
|
import org.elasticsearch.action.bulk.BulkRequest;
|
18
|
import org.elasticsearch.action.bulk.BulkRequest;
|
8
|
import org.elasticsearch.action.bulk.BulkResponse;
|
19
|
import org.elasticsearch.action.bulk.BulkResponse;
|
|
@@ -17,6 +28,7 @@ import org.slf4j.LoggerFactory; |
|
@@ -17,6 +28,7 @@ import org.slf4j.LoggerFactory; |
17
|
import org.springframework.beans.factory.annotation.Autowired;
|
28
|
import org.springframework.beans.factory.annotation.Autowired;
|
18
|
import org.springframework.beans.factory.annotation.Qualifier;
|
29
|
import org.springframework.beans.factory.annotation.Qualifier;
|
19
|
import org.springframework.stereotype.Service;
|
30
|
import org.springframework.stereotype.Service;
|
|
|
31
|
+import org.springframework.util.CollectionUtils;
|
20
|
|
32
|
|
21
|
import java.io.IOException;
|
33
|
import java.io.IOException;
|
22
|
import java.util.List;
|
34
|
import java.util.List;
|
|
@@ -33,34 +45,105 @@ public class UserInfoDataSyncServiceImpl { |
|
@@ -33,34 +45,105 @@ public class UserInfoDataSyncServiceImpl { |
33
|
@Autowired
|
45
|
@Autowired
|
34
|
private UserInfoRepository userInfoRepository;
|
46
|
private UserInfoRepository userInfoRepository;
|
35
|
|
47
|
|
36
|
- public String sync(){
|
|
|
37
|
- List list = Lists.newArrayList();
|
|
|
38
|
- UserInfo userInfo = new UserInfo();
|
|
|
39
|
- userInfo.setNickName("zhangsan");
|
|
|
40
|
- userInfo.setHeadIco("www");
|
|
|
41
|
- userInfo.setUid(500031572);
|
|
|
42
|
- userInfo.setGender(1);
|
|
|
43
|
- list.add(userInfo);
|
|
|
44
|
- writeData(list);
|
|
|
45
|
- return "";
|
48
|
+ public String sync(Integer uid,Integer flag, Integer size, Integer pageSize){
|
|
|
49
|
+ int endUid;
|
|
|
50
|
+ //flag =0 只跑真实用户uid flag=1 时跑mars 马甲信息
|
|
|
51
|
+ if(flag == 0){
|
|
|
52
|
+ endUid = 1000000000;
|
|
|
53
|
+ }else {
|
|
|
54
|
+ endUid = 2100000000;
|
|
|
55
|
+ }
|
|
|
56
|
+ int count = userInfoRepository.countAllByYoho_uid(uid, endUid);
|
|
|
57
|
+ int totalPage;
|
|
|
58
|
+ String result = "";
|
|
|
59
|
+ if(count > size){
|
|
|
60
|
+ totalPage = size/pageSize;
|
|
|
61
|
+ }else {
|
|
|
62
|
+ totalPage = count%pageSize==0 ? count/pageSize: count/pageSize+1;
|
|
|
63
|
+ }
|
|
|
64
|
+ logger.info("userInfo count is {}, size is {}, pagesize is {}, totalpage is {}",count,size,pageSize,totalPage);
|
|
|
65
|
+ for (int i = 0; i < totalPage; i++) {
|
|
|
66
|
+ Pageable pageReq = PageRequest.of(i, pageSize);
|
|
|
67
|
+ List<UserInfo> userInfoList = userInfoRepository.queryAllByYoho_uid(uid,endUid, pageReq);
|
|
|
68
|
+ if(!CollectionUtils.isEmpty(userInfoList)){
|
|
|
69
|
+ writeData(userInfoList);
|
|
|
70
|
+ }
|
|
|
71
|
+ try {
|
|
|
72
|
+ Thread.sleep(200);
|
|
|
73
|
+ } catch (InterruptedException e) {
|
|
|
74
|
+ e.printStackTrace();
|
|
|
75
|
+ }
|
|
|
76
|
+ if(i == totalPage-1){
|
|
|
77
|
+ result = String.valueOf(userInfoList.get(userInfoList.size()-1).getYoho_uid());
|
|
|
78
|
+ }
|
|
|
79
|
+ }
|
|
|
80
|
+
|
|
|
81
|
+ return result;
|
46
|
}
|
82
|
}
|
47
|
|
83
|
|
48
|
private void writeData(List<UserInfo> userInfoList){
|
84
|
private void writeData(List<UserInfo> userInfoList){
|
49
|
// 批量增加
|
85
|
// 批量增加
|
50
|
BulkRequest bulkAddRequest = new BulkRequest();
|
86
|
BulkRequest bulkAddRequest = new BulkRequest();
|
51
|
- for (int i = 0; i < userInfoList.size(); i++) {
|
|
|
52
|
- UserInfo info = userInfoList.get(i);
|
|
|
53
|
- IndexRequest indexRequest = new IndexRequest("grass", "userInfo", "");
|
87
|
+ for (UserInfo info : userInfoList) {
|
|
|
88
|
+ IndexRequest indexRequest = new IndexRequest("grass", "userInfo", String.valueOf(info.getYoho_uid()));
|
54
|
indexRequest.source(JSON.toJSONString(info), XContentType.JSON);
|
89
|
indexRequest.source(JSON.toJSONString(info), XContentType.JSON);
|
55
|
bulkAddRequest.add(indexRequest);
|
90
|
bulkAddRequest.add(indexRequest);
|
56
|
}
|
91
|
}
|
57
|
BulkResponse bulkAddResponse = null;
|
92
|
BulkResponse bulkAddResponse = null;
|
58
|
try {
|
93
|
try {
|
59
|
bulkAddResponse = rhlClient.bulk(bulkAddRequest, RequestOptions.DEFAULT);
|
94
|
bulkAddResponse = rhlClient.bulk(bulkAddRequest, RequestOptions.DEFAULT);
|
60
|
- logger.info("bulkAdd: {}" , JSON.toJSONString(bulkAddResponse));
|
95
|
+ logger.info("writeData: {}" , JSON.toJSONString(bulkAddResponse));
|
|
|
96
|
+
|
|
|
97
|
+ } catch (IOException e) {
|
|
|
98
|
+ logger.info("writeData error :{}" ,e);
|
|
|
99
|
+ }
|
|
|
100
|
+ }
|
61
|
|
101
|
|
|
|
102
|
+ private void deleteData(){
|
|
|
103
|
+ try {
|
|
|
104
|
+ DeleteRequest deleteRequest = new DeleteRequest("grass", "userInfo", "");
|
|
|
105
|
+ DeleteResponse response = rhlClient.delete(deleteRequest, RequestOptions.DEFAULT);
|
|
|
106
|
+ logger.info("deleteData: {}" , JSON.toJSONString(response));
|
62
|
} catch (IOException e) {
|
107
|
} catch (IOException e) {
|
63
|
e.printStackTrace();
|
108
|
e.printStackTrace();
|
64
|
}
|
109
|
}
|
65
|
}
|
110
|
}
|
|
|
111
|
+
|
|
|
112
|
+ public void deleteAllUserData(){
|
|
|
113
|
+ try {
|
|
|
114
|
+ BoolQueryBuilder queryBuilder = new BoolQueryBuilder();
|
|
|
115
|
+ queryBuilder.must(QueryBuilders.matchAllQuery());
|
|
|
116
|
+ DeleteByQueryRequest deleteRequest = new DeleteByQueryRequest("grass");
|
|
|
117
|
+ deleteRequest.setQuery(queryBuilder);
|
|
|
118
|
+ deleteRequest.setDocTypes("userInfo");
|
|
|
119
|
+ BulkByScrollResponse response = rhlClient.deleteByQuery(deleteRequest, RequestOptions.DEFAULT);
|
|
|
120
|
+ logger.info("deleteAllUserData: {}" , JSON.toJSONString(response));
|
|
|
121
|
+ } catch (IOException e) {
|
|
|
122
|
+ e.printStackTrace();
|
|
|
123
|
+ }
|
|
|
124
|
+ }
|
|
|
125
|
+
|
|
|
126
|
+ public String searchAllUserData(){
|
|
|
127
|
+ try {
|
|
|
128
|
+ BoolQueryBuilder queryBuilder = new BoolQueryBuilder();
|
|
|
129
|
+ queryBuilder.must(QueryBuilders.matchAllQuery());
|
|
|
130
|
+
|
|
|
131
|
+ SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
|
|
|
132
|
+ sourceBuilder.query(queryBuilder);
|
|
|
133
|
+ sourceBuilder.from(0);
|
|
|
134
|
+ sourceBuilder.size(200); // 获取记录数,默认10
|
|
|
135
|
+ SearchRequest searchRequest = new SearchRequest("grass");
|
|
|
136
|
+ searchRequest.source(sourceBuilder);
|
|
|
137
|
+ searchRequest.types("userInfo");
|
|
|
138
|
+
|
|
|
139
|
+ SearchResponse response = rhlClient.search(searchRequest, RequestOptions.DEFAULT);
|
|
|
140
|
+ logger.info("searchAllUserData: {}" , JSON.toJSONString(response));
|
|
|
141
|
+ return JSON.toJSONString(response.getHits().totalHits);
|
|
|
142
|
+ } catch (IOException e) {
|
|
|
143
|
+ e.printStackTrace();
|
|
|
144
|
+ }
|
|
|
145
|
+ return "";
|
|
|
146
|
+ }
|
|
|
147
|
+
|
|
|
148
|
+
|
66
|
} |
149
|
} |