...
|
...
|
@@ -2,7 +2,18 @@ package com.yoho.datasync.fullsync.service.impl; |
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.google.common.collect.Lists;
|
|
|
import org.elasticsearch.action.delete.DeleteRequest;
|
|
|
import org.elasticsearch.action.delete.DeleteResponse;
|
|
|
import org.elasticsearch.action.search.SearchRequest;
|
|
|
import org.elasticsearch.action.search.SearchResponse;
|
|
|
import org.elasticsearch.index.query.BoolQueryBuilder;
|
|
|
import org.elasticsearch.index.query.QueryBuilder;
|
|
|
import org.elasticsearch.index.query.QueryBuilders;
|
|
|
import org.elasticsearch.index.reindex.BulkByScrollResponse;
|
|
|
import org.elasticsearch.index.reindex.DeleteByQueryRequest;
|
|
|
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
|
|
import org.springframework.data.domain.PageRequest;
|
|
|
import org.springframework.data.domain.Pageable;
|
|
|
import com.yoho.datasync.fullsync.dal.repository.yhcommunity.model.UserInfo;
|
|
|
import org.elasticsearch.action.bulk.BulkRequest;
|
|
|
import org.elasticsearch.action.bulk.BulkResponse;
|
...
|
...
|
@@ -17,6 +28,7 @@ import org.slf4j.LoggerFactory; |
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.util.List;
|
...
|
...
|
@@ -33,34 +45,105 @@ public class UserInfoDataSyncServiceImpl { |
|
|
@Autowired
|
|
|
private UserInfoRepository userInfoRepository;
|
|
|
|
|
|
public String sync(){
|
|
|
List list = Lists.newArrayList();
|
|
|
UserInfo userInfo = new UserInfo();
|
|
|
userInfo.setNickName("zhangsan");
|
|
|
userInfo.setHeadIco("www");
|
|
|
userInfo.setUid(500031572);
|
|
|
userInfo.setGender(1);
|
|
|
list.add(userInfo);
|
|
|
writeData(list);
|
|
|
return "";
|
|
|
public String sync(Integer uid,Integer flag, Integer size, Integer pageSize){
|
|
|
int endUid;
|
|
|
//flag =0 只跑真实用户uid flag=1 时跑mars 马甲信息
|
|
|
if(flag == 0){
|
|
|
endUid = 1000000000;
|
|
|
}else {
|
|
|
endUid = 2100000000;
|
|
|
}
|
|
|
int count = userInfoRepository.countAllByYoho_uid(uid, endUid);
|
|
|
int totalPage;
|
|
|
String result = "";
|
|
|
if(count > size){
|
|
|
totalPage = size/pageSize;
|
|
|
}else {
|
|
|
totalPage = count%pageSize==0 ? count/pageSize: count/pageSize+1;
|
|
|
}
|
|
|
logger.info("userInfo count is {}, size is {}, pagesize is {}, totalpage is {}",count,size,pageSize,totalPage);
|
|
|
for (int i = 0; i < totalPage; i++) {
|
|
|
Pageable pageReq = PageRequest.of(i, pageSize);
|
|
|
List<UserInfo> userInfoList = userInfoRepository.queryAllByYoho_uid(uid,endUid, pageReq);
|
|
|
if(!CollectionUtils.isEmpty(userInfoList)){
|
|
|
writeData(userInfoList);
|
|
|
}
|
|
|
try {
|
|
|
Thread.sleep(200);
|
|
|
} catch (InterruptedException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
if(i == totalPage-1){
|
|
|
result = String.valueOf(userInfoList.get(userInfoList.size()-1).getYoho_uid());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
private void writeData(List<UserInfo> userInfoList){
|
|
|
// 批量增加
|
|
|
BulkRequest bulkAddRequest = new BulkRequest();
|
|
|
for (int i = 0; i < userInfoList.size(); i++) {
|
|
|
UserInfo info = userInfoList.get(i);
|
|
|
IndexRequest indexRequest = new IndexRequest("grass", "userInfo", "");
|
|
|
for (UserInfo info : userInfoList) {
|
|
|
IndexRequest indexRequest = new IndexRequest("grass", "userInfo", String.valueOf(info.getYoho_uid()));
|
|
|
indexRequest.source(JSON.toJSONString(info), XContentType.JSON);
|
|
|
bulkAddRequest.add(indexRequest);
|
|
|
}
|
|
|
BulkResponse bulkAddResponse = null;
|
|
|
try {
|
|
|
bulkAddResponse = rhlClient.bulk(bulkAddRequest, RequestOptions.DEFAULT);
|
|
|
logger.info("bulkAdd: {}" , JSON.toJSONString(bulkAddResponse));
|
|
|
logger.info("writeData: {}" , JSON.toJSONString(bulkAddResponse));
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
logger.info("writeData error :{}" ,e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private void deleteData(){
|
|
|
try {
|
|
|
DeleteRequest deleteRequest = new DeleteRequest("grass", "userInfo", "");
|
|
|
DeleteResponse response = rhlClient.delete(deleteRequest, RequestOptions.DEFAULT);
|
|
|
logger.info("deleteData: {}" , JSON.toJSONString(response));
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void deleteAllUserData(){
|
|
|
try {
|
|
|
BoolQueryBuilder queryBuilder = new BoolQueryBuilder();
|
|
|
queryBuilder.must(QueryBuilders.matchAllQuery());
|
|
|
DeleteByQueryRequest deleteRequest = new DeleteByQueryRequest("grass");
|
|
|
deleteRequest.setQuery(queryBuilder);
|
|
|
deleteRequest.setDocTypes("userInfo");
|
|
|
BulkByScrollResponse response = rhlClient.deleteByQuery(deleteRequest, RequestOptions.DEFAULT);
|
|
|
logger.info("deleteAllUserData: {}" , JSON.toJSONString(response));
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public String searchAllUserData(){
|
|
|
try {
|
|
|
BoolQueryBuilder queryBuilder = new BoolQueryBuilder();
|
|
|
queryBuilder.must(QueryBuilders.matchAllQuery());
|
|
|
|
|
|
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
|
|
|
sourceBuilder.query(queryBuilder);
|
|
|
sourceBuilder.from(0);
|
|
|
sourceBuilder.size(200); // 获取记录数,默认10
|
|
|
SearchRequest searchRequest = new SearchRequest("grass");
|
|
|
searchRequest.source(sourceBuilder);
|
|
|
searchRequest.types("userInfo");
|
|
|
|
|
|
SearchResponse response = rhlClient.search(searchRequest, RequestOptions.DEFAULT);
|
|
|
logger.info("searchAllUserData: {}" , JSON.toJSONString(response));
|
|
|
return JSON.toJSONString(response.getHits().totalHits);
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
return "";
|
|
|
}
|
|
|
|
|
|
|
|
|
} |
...
|
...
|
|