|
@@ -9,6 +9,8 @@ import cn.com.taiji.service.FusionAnalysisService;
|
|
|
import cn.com.taiji.utils.DateUtils;
|
|
|
import cn.hutool.core.io.LineHandler;
|
|
|
import com.alibaba.excel.EasyExcel;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.http.client.config.RequestConfig;
|
|
@@ -42,10 +44,7 @@ import java.io.IOException;
|
|
|
import java.lang.reflect.Field;
|
|
|
import java.net.URLEncoder;
|
|
|
import java.text.ParseException;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
+import java.util.*;
|
|
|
|
|
|
import static cn.com.taiji.constants.EsIndexConstants.*;
|
|
|
|
|
@@ -86,57 +85,109 @@ public class FusionAnalysisServiceImpl implements FusionAnalysisService {
|
|
|
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
|
|
|
boolQueryBuilder.filter(QueryBuilders.rangeQuery("mergeTime").gte(query.getStartTime()).lte(query.getEndTime()));
|
|
|
searchSourceBuilder.query(boolQueryBuilder);
|
|
|
- TermsAggregationBuilder aggBuilder = AggregationBuilders.terms("ship_field_agg")
|
|
|
- .field("mergeTarget")
|
|
|
- .size(query.getPageSize())
|
|
|
- .subAggregation(
|
|
|
- AggregationBuilders.topHits("ship_field_agg_top1")
|
|
|
- .size(100)
|
|
|
- .sort(
|
|
|
- SortBuilders.fieldSort("mergeTime").order(SortOrder.ASC)
|
|
|
- )
|
|
|
- );
|
|
|
+// TermsAggregationBuilder aggBuilder = AggregationBuilders.terms("ship_field_agg")
|
|
|
+// .field("mergeTarget")
|
|
|
+// .size(query.getPageSize())
|
|
|
+// .subAggregation(
|
|
|
+// AggregationBuilders.topHits("ship_field_agg_top1")
|
|
|
+// .size(100)
|
|
|
+// .sort(
|
|
|
+// SortBuilders.fieldSort("mergeTime").order(SortOrder.ASC)
|
|
|
+// )
|
|
|
+// );
|
|
|
searchSourceBuilder
|
|
|
.trackTotalHits(true)
|
|
|
- .aggregation(aggBuilder)
|
|
|
+ .sort(SortBuilders.fieldSort("mergeTime").order(SortOrder.ASC))
|
|
|
+// .aggregation(aggBuilder)
|
|
|
.size(query.getPageSize())
|
|
|
.timeout(TimeValue.timeValueHours(1L))
|
|
|
.timeout(TimeValue.timeValueMinutes(30L))
|
|
|
.timeout(TimeValue.timeValueSeconds(500L));
|
|
|
request.source(searchSourceBuilder);
|
|
|
SearchResponse search = client.search(request, COMMON_OPTIONS);
|
|
|
- Aggregations aggregations = search.getAggregations();
|
|
|
- ParsedStringTerms aggregation = aggregations.get("ship_field_agg");
|
|
|
- for (Terms.Bucket bucket : aggregation.getBuckets()) {
|
|
|
- FusionData data = new FusionData();
|
|
|
- ParsedTopHits topHits = bucket.getAggregations().get("ship_field_agg_top1");
|
|
|
- SearchHits searchHits = topHits.getHits();
|
|
|
- Long value = searchHits.getTotalHits().value;
|
|
|
- SearchHit[] hits = searchHits.getHits();
|
|
|
- Integer num = 0;
|
|
|
- for (int i = 0; i < hits.length; i++) {
|
|
|
- if (i != hits.length-1){
|
|
|
- Map<String, Object> map = hits[i].getSourceAsMap();
|
|
|
- Map<String, Object> map2 = hits[i+1].getSourceAsMap();
|
|
|
- Date date = DateUtils.parse(map.get("mergeTime").toString(),"yyyy-MM-dd");
|
|
|
- Date date2 = DateUtils.parse(map2.get("mergeTime").toString(),"yyyy-MM-dd");
|
|
|
- Long interval = date2.getTime()-date.getTime();
|
|
|
- if (interval>6500L){
|
|
|
- num++;
|
|
|
+ SearchHits searchHits = search.getHits();
|
|
|
+ SearchHit[] hits = searchHits.getHits();
|
|
|
+ List<Map<String, Object>> inList = new ArrayList<Map<String, Object>>();
|
|
|
+ List<Map<String, Object>> outList = new ArrayList<Map<String, Object>>();
|
|
|
+ for (SearchHit hit : hits) {
|
|
|
+ Map<String, Object> map = hit.getSourceAsMap();
|
|
|
+ inList.add(map);
|
|
|
+ }
|
|
|
+ outList = change(inList, "mergeTarget", outList);
|
|
|
+ outList.forEach(map->{
|
|
|
+ JSONObject maps = JSONObject.parseObject(JSON.toJSONString(map));
|
|
|
+// JSONObject maps = new JSONObject();
|
|
|
+// Iterator it = map.keySet().iterator();
|
|
|
+// while (it.hasNext()) {
|
|
|
+// String key = (String) it.next();
|
|
|
+// maps.put(key, map.get(key));
|
|
|
+// }
|
|
|
+ JSONArray jsonArray =JSONArray.parseArray(maps.getString("array"));
|
|
|
+ if (jsonArray.size()>0){
|
|
|
+ FusionData data = new FusionData();
|
|
|
+ Integer num = 0;
|
|
|
+ for (int i = 0; i < jsonArray.size(); i++) {
|
|
|
+ if (i<jsonArray.size()-1){
|
|
|
+ JSONObject job = jsonArray.getJSONObject(i);
|
|
|
+ JSONObject job2 = jsonArray.getJSONObject(i+1);
|
|
|
+ Date date = null;
|
|
|
+ Date date2 = null;
|
|
|
+ try {
|
|
|
+ date = DateUtils.parse(job.get("mergeTime").toString(),"yyyy-MM-dd");
|
|
|
+ date2 = DateUtils.parse(job2.get("mergeTime").toString(),"yyyy-MM-dd");
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ Long interval = date2.getTime()-date.getTime();
|
|
|
+ if (interval>6500L){
|
|
|
+ num++;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
+ if (num>0){
|
|
|
+ JSONObject sourceItem = jsonArray.getJSONObject(0);
|
|
|
+ JSONObject sourceItem2 = jsonArray.getJSONObject(jsonArray.size()-1);
|
|
|
+ data.setMergeTarget(sourceItem.getString("mergeTarget"));
|
|
|
+ data.setTrackCount((long) jsonArray.size());
|
|
|
+ data.setStartTime(sourceItem.getString("mergeTime"));
|
|
|
+ data.setEndTime(sourceItem2.getString("mergeTime"));
|
|
|
+ data.setFluctuateNumber(num);
|
|
|
+ list.add(data);
|
|
|
+ }
|
|
|
}
|
|
|
- if (num>0){
|
|
|
- JSONObject sourceItem = JSONObject.parseObject(hits[0].getSourceAsString());
|
|
|
- JSONObject sourceItem2 = JSONObject.parseObject(hits[value.intValue()-1].getSourceAsString());
|
|
|
- data.setMergeTarget(sourceItem.getString("mergeTarget"));
|
|
|
- data.setTrackCount(value);
|
|
|
- data.setStartTime(sourceItem.getString("mergeTime"));
|
|
|
- data.setEndTime(sourceItem2.getString("mergeTime"));
|
|
|
- data.setFluctuateNumber(num);
|
|
|
- list.add(data);
|
|
|
- }
|
|
|
- }
|
|
|
+ });
|
|
|
+// Aggregations aggregations = search.getAggregations();
|
|
|
+// ParsedStringTerms aggregation = aggregations.get("ship_field_agg");
|
|
|
+// for (Terms.Bucket bucket : aggregation.getBuckets()) {
|
|
|
+// FusionData data = new FusionData();
|
|
|
+// ParsedTopHits topHits = bucket.getAggregations().get("ship_field_agg_top1");
|
|
|
+// SearchHits searchHits = topHits.getHits();
|
|
|
+// Long value = searchHits.getTotalHits().value;
|
|
|
+// SearchHit[] hits = searchHits.getHits();
|
|
|
+// Integer num = 0;
|
|
|
+// for (int i = 0; i < hits.length; i++) {
|
|
|
+// if (i != hits.length-1){
|
|
|
+// Map<String, Object> map = hits[i].getSourceAsMap();
|
|
|
+// Map<String, Object> map2 = hits[i+1].getSourceAsMap();
|
|
|
+// Date date = DateUtils.parse(map.get("mergeTime").toString(),"yyyy-MM-dd");
|
|
|
+// Date date2 = DateUtils.parse(map2.get("mergeTime").toString(),"yyyy-MM-dd");
|
|
|
+// Long interval = date2.getTime()-date.getTime();
|
|
|
+// if (interval>6500L){
|
|
|
+// num++;
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+// if (num>0){
|
|
|
+// JSONObject sourceItem = JSONObject.parseObject(hits[0].getSourceAsString());
|
|
|
+// JSONObject sourceItem2 = JSONObject.parseObject(hits[value.intValue()-1].getSourceAsString());
|
|
|
+// data.setMergeTarget(sourceItem.getString("mergeTarget"));
|
|
|
+// data.setTrackCount(value);
|
|
|
+// data.setStartTime(sourceItem.getString("mergeTime"));
|
|
|
+// data.setEndTime(sourceItem2.getString("mergeTime"));
|
|
|
+// data.setFluctuateNumber(num);
|
|
|
+// list.add(data);
|
|
|
+// }
|
|
|
+// }
|
|
|
try {
|
|
|
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
|
|
response.setCharacterEncoding("utf-8");
|
|
@@ -250,13 +301,14 @@ public class FusionAnalysisServiceImpl implements FusionAnalysisService {
|
|
|
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
|
|
|
boolQueryBuilder.filter(QueryBuilders.rangeQuery("receiveTime").gte(query.getStartTime()).lte(query.getEndTime()));
|
|
|
searchSourceBuilder.query(boolQueryBuilder);
|
|
|
- TermsAggregationBuilder aggBuilder = AggregationBuilders.terms("ship_field_agg")
|
|
|
- .field("fusionBatchNum")
|
|
|
- .size(query.getPageSize());
|
|
|
+// TermsAggregationBuilder aggBuilder = AggregationBuilders.terms("ship_field_agg")
|
|
|
+// .field("fusionBatchNum")
|
|
|
+// .size(query.getPageSize());
|
|
|
searchSourceBuilder
|
|
|
.trackTotalHits(true)
|
|
|
- .aggregation(aggBuilder)
|
|
|
+// .aggregation(aggBuilder)
|
|
|
.size(query.getPageSize())
|
|
|
+ .sort(SortBuilders.fieldSort("receiveTime").order(SortOrder.ASC))
|
|
|
.timeout(TimeValue.timeValueHours(1L))
|
|
|
.timeout(TimeValue.timeValueMinutes(30L))
|
|
|
.timeout(TimeValue.timeValueSeconds(500L));
|
|
@@ -560,4 +612,39 @@ public class FusionAnalysisServiceImpl implements FusionAnalysisService {
|
|
|
System.out.println("导出天奥静态数据与动态轨迹融合excel失败:{}" + e.getMessage() + e);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 按照List<Map<String,Object>>里面map的某个value重新封装成多个不同的list, 原始数据类型List<Map<String,Object>>, 转换后数据类型Map<String,List<Map<String,Object>>>
|
|
|
+ * @param inList
|
|
|
+ * @param oneMapKey
|
|
|
+ * @param outList
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private static List<Map<String, Object>> change(List<Map<String, Object>> inList, String oneMapKey,
|
|
|
+ List<Map<String, Object>> outList) {
|
|
|
+ // 1.将某个key的值存在set中
|
|
|
+ Set<Object> setTmp = new HashSet<Object>();
|
|
|
+ for (Map<String, Object> tmp : inList) {
|
|
|
+ setTmp.add(tmp.get(oneMapKey));
|
|
|
+ }
|
|
|
+ // 2.遍历set
|
|
|
+ Iterator<Object> it = setTmp.iterator();
|
|
|
+ while (it.hasNext()) {
|
|
|
+ String oneMapValueStr = "";
|
|
|
+ String oneSetTmpStr = (String) it.next();
|
|
|
+ Map<String, Object> oneSetTmpMap = new HashMap<String, Object>();
|
|
|
+ List<Map<String, Object>> oneSetTmpList = new ArrayList<Map<String, Object>>();
|
|
|
+
|
|
|
+ for (Map<String, Object> tmp : inList) {
|
|
|
+ oneMapValueStr = (String) tmp.get(oneMapKey);
|
|
|
+ if (oneSetTmpStr.equals(oneMapValueStr)) {
|
|
|
+ oneSetTmpMap.put("text", oneSetTmpStr);
|
|
|
+ oneSetTmpList.add(tmp);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ oneSetTmpMap.put("array", oneSetTmpList);
|
|
|
+ outList.add(oneSetTmpMap);
|
|
|
+ }
|
|
|
+ return outList;
|
|
|
+ }
|
|
|
}
|