Commit 4b035ef8 by xmh

修改查询pool的逻辑

1 parent e3e2a55d
......@@ -64,4 +64,8 @@ public class ResponseVo {
responseVo.setErrCode(0);
return responseVo;
}
public static ResponseVo poolIdNotExists(String rid) {
return ResponseVo.error(rid, 4, "poolId not exists");
}
}
package com.viontech.match.service;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.viontech.keliu.model.Person;
import com.viontech.match.config.Constant;
import com.viontech.match.entity.PoolInfo;
import com.viontech.match.entity.vo.RequestVo;
import com.viontech.match.entity.vo.ResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.elasticsearch.ElasticsearchStatusException;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.core.CountRequest;
import org.elasticsearch.client.core.CountResponse;
import org.elasticsearch.client.*;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.rest.RestStatus;
......@@ -25,9 +23,11 @@ import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.io.InputStream;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* .
......@@ -42,6 +42,8 @@ public class PoolService {
private PersonService personService;
@Resource
private RestHighLevelClient client;
@Resource
private ObjectMapper objectMapper;
/**
* 添加特征池
......@@ -100,7 +102,7 @@ public class PoolService {
return ResponseVo.success(rid);
} catch (ElasticsearchStatusException e) {
if (e.status() == RestStatus.NOT_FOUND) {
return ResponseVo.error(rid, 4, "poolId not exists");
return ResponseVo.poolIdNotExists(rid);
} else {
return ResponseVo.error(rid, e.getDetailedMessage());
}
......@@ -117,7 +119,7 @@ public class PoolService {
Integer updateType = requestVo.getUpdateType();
log.info("特征池修改操作开始:[{}],updateType:[{}]", poolId, updateType);
if (!client.indices().exists(new GetIndexRequest(poolId), RequestOptions.DEFAULT)) {
return ResponseVo.error(rid, 4, "poolId not exists");
return ResponseVo.poolIdNotExists(rid);
}
try {
BulkResponse bulkItemResponses = personService.addPerson(poolId, personPool);
......@@ -141,25 +143,21 @@ public class PoolService {
Integer listAll = requestVo.getListAll();
String poolId = requestVo.getPoolId();
try {
List<PoolInfo> poolInfos = new ArrayList<>();
List<PoolInfo> poolInfos;
log.info("查询特征池操作开始:[{}],rid:[{}]", poolId, rid);
if (listAll != 0) {
poolId = "*";
poolInfos = queryPoolInfo(null);
} else {
if (!existPool(poolId)) {
return ResponseVo.poolIdNotExists(rid);
}
log.info("查询特征池操作开始:[{}],ListAll:[{}]", poolId, listAll);
GetIndexResponse response = client.indices().get(new GetIndexRequest(poolId), RequestOptions.DEFAULT);
String[] indices = response.getIndices();
for (String index : indices) {
PoolInfo poolInfo = new PoolInfo();
poolInfo.setPoolId(index);
CountRequest countRequest = new CountRequest(index);
CountResponse count = client.count(countRequest, RequestOptions.DEFAULT);
poolInfo.setPersonCount(count.getCount());
poolInfos.add(poolInfo);
poolInfos = queryPoolInfo(poolId);
}
ResponseVo success = ResponseVo.success(rid, "success");
success.setPoolIds(poolInfos);
log.info("查询特征池操作完成:[{}],ListAll:[{}]", poolId, listAll);
log.info("查询特征池操作完成:[{}],rid:[{}]", poolId, rid);
return success;
} catch (ElasticsearchStatusException e) {
log.error("queryPool", e);
......@@ -227,4 +225,28 @@ public class PoolService {
return client.indices().exists(new GetIndexRequest(poolId), RequestOptions.DEFAULT);
}
public List<PoolInfo> queryPoolInfo(String poolId) throws IOException {
String endPoint = poolId == null ? "/_stats" : "/" + poolId + "/_stats";
LinkedList<PoolInfo> poolInfos = new LinkedList<>();
RestClient lowLevelClient = client.getLowLevelClient();
Request request = new Request("GET", endPoint);
Response response = lowLevelClient.performRequest(request);
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
HashMap responseMap = objectMapper.readValue(content, HashMap.class);
HashMap<String, HashMap> indices1 = (HashMap<String, HashMap>) responseMap.get("indices");
for (Map.Entry<String, HashMap> entry : indices1.entrySet()) {
String id = entry.getKey();
HashMap value = entry.getValue();
HashMap<String, HashMap> primaries = (HashMap<String, HashMap>) value.get("primaries");
HashMap docs = primaries.get("docs");
Integer count = (Integer) docs.get("count");
PoolInfo poolInfo = new PoolInfo();
poolInfo.setPersonCount(count.longValue());
poolInfo.setPoolId(id);
poolInfos.add(poolInfo);
}
return poolInfos;
}
}
......@@ -4,4 +4,5 @@ spring.jackson.time-zone=GMT+8
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
# es
#spring.elasticsearch.rest.uris=http://192.168.9.116:9200
spring.elasticsearch.rest.uris=http://127.0.0.1:9200
\ No newline at end of file
#spring.elasticsearch.rest.uris=http://127.0.0.1:9200
spring.elasticsearch.rest.uris=http://139.217.100.35:9200,http://139.217.100.35:9201
\ No newline at end of file
package com.viontech.match;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.viontech.match.entity.PoolInfo;
import org.apache.http.HttpEntity;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.json.JSONException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
/**
* .
*
......@@ -15,5 +31,34 @@ import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
public class Test0 {
@Resource
private RestHighLevelClient client;
@Resource
private ObjectMapper objectMapper;
@Test
public void ttt() throws IOException, JSONException {
LinkedList<PoolInfo> poolInfos = new LinkedList<>();
RestClient lowLevelClient = client.getLowLevelClient();
Request request = new Request("GET", "/_stats");
Response response = lowLevelClient.performRequest(request);
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
HashMap responseMap = objectMapper.readValue(content, HashMap.class);
HashMap<String, HashMap> indices1 = (HashMap<String, HashMap>) responseMap.get("indices");
for (Map.Entry<String, HashMap> entry : indices1.entrySet()) {
String poolId = entry.getKey();
HashMap value = entry.getValue();
HashMap<String, HashMap> primaries = (HashMap<String, HashMap>) value.get("primaries");
HashMap docs = primaries.get("docs");
Integer count = (Integer) docs.get("count");
PoolInfo poolInfo = new PoolInfo();
poolInfo.setPersonCount(count.longValue());
poolInfo.setPoolId(poolId);
poolInfos.add(poolInfo);
}
System.out.println(1);
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!