|
@ -4,9 +4,15 @@ import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.yihu.base.es.config.ElastricSearchHelper;
|
|
|
import com.yihu.base.hbase.HBaseHelper;
|
|
|
import com.yihu.iot.datainput.enums.DataTypeEnum;
|
|
|
import com.yihu.iot.datainput.util.ConstantUtils;
|
|
|
import com.yihu.iot.datainput.util.RowKeyUtils;
|
|
|
import io.searchbox.core.Search;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.apache.hadoop.hbase.Cell;
|
|
|
import org.apache.hadoop.hbase.CellUtil;
|
|
|
import org.apache.hadoop.hbase.client.Result;
|
|
|
import org.apache.hadoop.hbase.util.Bytes;
|
|
|
import org.elasticsearch.action.search.SearchRequestBuilder;
|
|
|
import org.elasticsearch.index.query.QueryBuilders;
|
|
|
import org.elasticsearch.index.query.TermQueryBuilder;
|
|
@ -17,7 +23,9 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
@Component
|
|
|
public class DataSearchService {
|
|
@ -48,6 +56,13 @@ public class DataSearchService {
|
|
|
JSONArray jsonArray = new JSONArray();
|
|
|
for(String key : jsonObject.keySet()){
|
|
|
|
|
|
//默认为must
|
|
|
if(StringUtils.equalsIgnoreCase("or",key) && StringUtils.equals("or",jsonObject.getString("or"))){
|
|
|
mustOrShouldQuery.put("should",jsonArray);
|
|
|
}else{
|
|
|
mustOrShouldQuery.put("must",jsonArray);
|
|
|
}
|
|
|
|
|
|
//分页用,from表示数据从第几条开始取
|
|
|
if(StringUtils.equalsIgnoreCase("from",key)){
|
|
|
query.put("from",jsonObject.getInteger("from"));
|
|
@ -74,7 +89,8 @@ public class DataSearchService {
|
|
|
|
|
|
JSONObject matchQuery = new JSONObject();
|
|
|
JSONObject subQuery = new JSONObject();
|
|
|
if(DataStandardConvertService.dataMap.get("body_sign_params").contains(key)){
|
|
|
String baseName = DataTypeEnum.body_sign_params.name().toString();
|
|
|
if(null != DataStandardConvertService.dataMap.get(baseName) && DataStandardConvertService.dataMap.get(baseName).contains(key)){
|
|
|
subQuery.put("data."+key,jsonObject.get(key)); //data数据里内嵌的字段,真正的数据值内容
|
|
|
}else{
|
|
|
subQuery.put(key,jsonObject.get(key));
|
|
@ -82,12 +98,7 @@ public class DataSearchService {
|
|
|
matchQuery.put("match",subQuery);
|
|
|
jsonArray.add(matchQuery);
|
|
|
}
|
|
|
//默认为must
|
|
|
if(null != jsonObject.get("should") && StringUtils.equals("should",jsonObject.getString("should"))){
|
|
|
mustOrShouldQuery.put("should",jsonArray);
|
|
|
}else{
|
|
|
mustOrShouldQuery.put("must",jsonArray);
|
|
|
}
|
|
|
|
|
|
boolQuery.put("bool",mustOrShouldQuery);
|
|
|
query.put("query",boolQuery);
|
|
|
|
|
@ -96,16 +107,59 @@ public class DataSearchService {
|
|
|
|
|
|
public String getData(String jsonData){
|
|
|
String query = getQueryString(jsonData);
|
|
|
String result = elastricSearchHelper.search(ConstantUtils.esIndex,ConstantUtils.esType,query);
|
|
|
return result;
|
|
|
String esResult = elastricSearchHelper.search(ConstantUtils.esIndex,ConstantUtils.esType,query);
|
|
|
JSONArray jsonArray = (JSONArray)JSONArray.parse(esResult);
|
|
|
List<String> rowkeys = new ArrayList<>();
|
|
|
for(Object object:jsonArray){
|
|
|
JSONObject jsonObject = (JSONObject)object;
|
|
|
JSONArray datas = (JSONArray)jsonObject.get("data");
|
|
|
for(Object data:datas){
|
|
|
JSONObject dataJson = (JSONObject)data;
|
|
|
rowkeys.add(dataJson.getString("rid"));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
List<Map<String, Object>> resultList = new ArrayList<>();
|
|
|
try {
|
|
|
//拿到rowkey后,去hbase读取数据内容
|
|
|
Result[] hbaseData = hBaseHelper.getResultList(ConstantUtils.tableName,rowkeys);
|
|
|
for(int i = 0;i < hbaseData.length; i++){
|
|
|
List<Cell> ceList = hbaseData[i].listCells();
|
|
|
Map<String, Object> map = new HashMap<String, Object>();
|
|
|
String rowkey = Bytes.toString(hbaseData[i].getRow());
|
|
|
//rowkey是根据一些头部数据加密而来,解密即可还原
|
|
|
String tag = RowKeyUtils.getMessageFromRowKey(rowkey);
|
|
|
String[] tags = tag.split(",");
|
|
|
map.put("accessToken", tags[0]);
|
|
|
map.put("deviceSn", tags[1]);
|
|
|
map.put("extCode", tags[2]);
|
|
|
if (ceList != null && ceList.size() > 0) {
|
|
|
for (Cell cell : ceList) {
|
|
|
map.put(Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength()),
|
|
|
Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()));
|
|
|
}
|
|
|
}
|
|
|
resultList.add(map);
|
|
|
}
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
logger.error("get data from hbase fail.",e.getMessage());
|
|
|
return esResult;
|
|
|
}
|
|
|
JSONArray resultArray = new JSONArray();
|
|
|
resultArray.addAll(resultList);
|
|
|
return resultArray.toJSONString();
|
|
|
}
|
|
|
|
|
|
public static void main(String args[]){
|
|
|
String str = "{\n" +
|
|
|
"\t\"systolic\":\"59\",\n" +
|
|
|
"\t\"extcode\":\"1\",\n" +
|
|
|
"\t\"or\":\"\",\n" +
|
|
|
"\t\"extCode\":\"1\",\n" +
|
|
|
"\t\"idcard\":\"test\",\n" +
|
|
|
"\t\"from\":1,\n" +
|
|
|
"\t\"size\":5\n" +
|
|
|
"\t\"size\":5,\n" +
|
|
|
"\t\"sort\":{\"measure_time\":\"desc\"}\n" +
|
|
|
"}";
|
|
|
System.out.println(getQueryString(str));
|
|
|
}
|