|
@ -53,94 +53,94 @@ public class AggregationBuildHandler {
|
|
@Autowired
|
|
@Autowired
|
|
private CubeService cubeService;
|
|
private CubeService cubeService;
|
|
|
|
|
|
/**
|
|
|
|
* 简单聚合
|
|
|
|
* @param aggType 聚合类型
|
|
|
|
* @param aggName 聚合名称
|
|
|
|
* @param fieldName 聚合字段
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public AbstractAggregationBuilder addAggregationBuilder(String aggType,String aggName, String fieldName){
|
|
|
|
AbstractAggregationBuilder builder = null;
|
|
|
|
if(aggType.equals("sum")){
|
|
|
|
builder = AggregationBuilders.sum(aggName).field(fieldName);
|
|
|
|
}else if(aggType.equals("count")){
|
|
|
|
builder = AggregationBuilders.count(aggName).field(fieldName);
|
|
|
|
}else if(aggType.equals("avg")){
|
|
|
|
builder = AggregationBuilders.avg(aggName).field(fieldName);
|
|
|
|
}else if(aggType.equals("max")){
|
|
|
|
builder = AggregationBuilders.max(aggName).field(fieldName);
|
|
|
|
}else if(aggType.equals("min")){
|
|
|
|
builder = AggregationBuilders.min(aggName).field(fieldName);
|
|
|
|
}
|
|
|
|
return builder;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 分组后,简单聚合
|
|
|
|
* @param aggType 聚合类型
|
|
|
|
* @param aggName 聚合名称
|
|
|
|
* @param fieldName 聚合字段
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public AbstractAggregationBuilder addTermAggregationBuilder(String termName, String termFieldName,String aggType,String aggName, String fieldName){
|
|
|
|
AbstractAggregationBuilder builder = null;
|
|
|
|
TermsBuilder termsBuilder = addTermsBuilder(termName,termFieldName);
|
|
|
|
if(aggType.equals("sum")){
|
|
|
|
builder = termsBuilder.subAggregation(AggregationBuilders.sum(aggName).field(fieldName));
|
|
|
|
}else if(aggType.equals("count")){
|
|
|
|
builder = termsBuilder.subAggregation(AggregationBuilders.count(aggName).field(fieldName));
|
|
|
|
}else if(aggType.equals("avg")){
|
|
|
|
builder = termsBuilder.subAggregation(AggregationBuilders.avg(aggName).field(fieldName));
|
|
|
|
}else if(aggType.equals("max")){
|
|
|
|
builder = termsBuilder.subAggregation(AggregationBuilders.max(aggName).field(fieldName));
|
|
|
|
}else if(aggType.equals("min")){
|
|
|
|
builder = termsBuilder.subAggregation(AggregationBuilders.min(aggName).field(fieldName));
|
|
|
|
}
|
|
|
|
return builder;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 添加分组
|
|
|
|
* @param termName
|
|
|
|
* @param fieldName
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public TermsBuilder addTermsBuilder(String termName, String fieldName){
|
|
|
|
TermsBuilder termBuilder = AggregationBuilders.terms(termName).field(fieldName);
|
|
|
|
return termBuilder;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param client
|
|
|
|
* @param boolQueryBuilder 查询的过滤条件
|
|
|
|
* @param aggBuilderList 聚合组
|
|
|
|
* 聚合组中成员是 单个的聚合查询,其中单个的聚合查询可以嵌套子聚合查询 如:
|
|
|
|
* 单个聚合 查询 :TermsBuilder firstAgg= AggregationBuilders.terms("player_count ").field("team");
|
|
|
|
* 带有子聚合的聚合查询 :
|
|
|
|
* TermsBuilder secondAgg= AggregationBuilders.terms("pos_count").field("position")
|
|
|
|
* .subAggregation(
|
|
|
|
* AggregationBuilders.dateHistogram("by_year").field("dateOfBirth").interval((DateHistogramInterval.YEAR))
|
|
|
|
* .subAggregation(
|
|
|
|
* AggregationBuilders.avg("avg_children").field("children")
|
|
|
|
* )
|
|
|
|
* );
|
|
|
|
* @return 聚合结果集
|
|
|
|
*/
|
|
|
|
public Map<String, Aggregation> structAggregationQuery(TransportClient client,String index,String type,
|
|
|
|
BoolQueryBuilder boolQueryBuilder,
|
|
|
|
LinkedList<AbstractAggregationBuilder> aggBuilderList){
|
|
|
|
SearchRequestBuilder searchRequestBuilder = client.prepareSearch(index).setTypes(type).setQuery(boolQueryBuilder);
|
|
|
|
for(AbstractAggregationBuilder aggBuilder : aggBuilderList){
|
|
|
|
searchRequestBuilder.addAggregation(aggBuilder);
|
|
|
|
}
|
|
|
|
SearchResponse response = searchRequestBuilder.execute().actionGet();
|
|
|
|
Map<String, Aggregation> map = response.getAggregations().getAsMap();
|
|
|
|
client.close();
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
|
// /**
|
|
|
|
// * 简单聚合
|
|
|
|
// * @param aggType 聚合类型
|
|
|
|
// * @param aggName 聚合名称
|
|
|
|
// * @param fieldName 聚合字段
|
|
|
|
// * @return
|
|
|
|
// */
|
|
|
|
// public AbstractAggregationBuilder addAggregationBuilder(String aggType,String aggName, String fieldName){
|
|
|
|
// AbstractAggregationBuilder builder = null;
|
|
|
|
// if(aggType.equals("sum")){
|
|
|
|
// builder = AggregationBuilders.sum(aggName).field(fieldName);
|
|
|
|
// }else if(aggType.equals("count")){
|
|
|
|
// builder = AggregationBuilders.count(aggName).field(fieldName);
|
|
|
|
// }else if(aggType.equals("avg")){
|
|
|
|
// builder = AggregationBuilders.avg(aggName).field(fieldName);
|
|
|
|
// }else if(aggType.equals("max")){
|
|
|
|
// builder = AggregationBuilders.max(aggName).field(fieldName);
|
|
|
|
// }else if(aggType.equals("min")){
|
|
|
|
// builder = AggregationBuilders.min(aggName).field(fieldName);
|
|
|
|
// }
|
|
|
|
// return builder;
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// /**
|
|
|
|
// * 分组后,简单聚合
|
|
|
|
// * @param aggType 聚合类型
|
|
|
|
// * @param aggName 聚合名称
|
|
|
|
// * @param fieldName 聚合字段
|
|
|
|
// * @return
|
|
|
|
// */
|
|
|
|
// public AbstractAggregationBuilder addTermAggregationBuilder(String termName, String termFieldName,String aggType,String aggName, String fieldName){
|
|
|
|
// AbstractAggregationBuilder builder = null;
|
|
|
|
// TermsBuilder termsBuilder = addTermsBuilder(termName,termFieldName);
|
|
|
|
// if(aggType.equals("sum")){
|
|
|
|
// builder = termsBuilder.subAggregation(AggregationBuilders.sum(aggName).field(fieldName));
|
|
|
|
// }else if(aggType.equals("count")){
|
|
|
|
// builder = termsBuilder.subAggregation(AggregationBuilders.count(aggName).field(fieldName));
|
|
|
|
// }else if(aggType.equals("avg")){
|
|
|
|
// builder = termsBuilder.subAggregation(AggregationBuilders.avg(aggName).field(fieldName));
|
|
|
|
// }else if(aggType.equals("max")){
|
|
|
|
// builder = termsBuilder.subAggregation(AggregationBuilders.max(aggName).field(fieldName));
|
|
|
|
// }else if(aggType.equals("min")){
|
|
|
|
// builder = termsBuilder.subAggregation(AggregationBuilders.min(aggName).field(fieldName));
|
|
|
|
// }
|
|
|
|
// return builder;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// /**
|
|
|
|
// * 添加分组
|
|
|
|
// * @param termName
|
|
|
|
// * @param fieldName
|
|
|
|
// * @return
|
|
|
|
// */
|
|
|
|
// public TermsBuilder addTermsBuilder(String termName, String fieldName){
|
|
|
|
// TermsBuilder termBuilder = AggregationBuilders.terms(termName).field(fieldName);
|
|
|
|
// return termBuilder;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// /**
|
|
|
|
// *
|
|
|
|
// * @param client
|
|
|
|
// * @param boolQueryBuilder 查询的过滤条件
|
|
|
|
// * @param aggBuilderList 聚合组
|
|
|
|
// * 聚合组中成员是 单个的聚合查询,其中单个的聚合查询可以嵌套子聚合查询 如:
|
|
|
|
// * 单个聚合 查询 :TermsBuilder firstAgg= AggregationBuilders.terms("player_count ").field("team");
|
|
|
|
// * 带有子聚合的聚合查询 :
|
|
|
|
// * TermsBuilder secondAgg= AggregationBuilders.terms("pos_count").field("position")
|
|
|
|
// * .subAggregation(
|
|
|
|
// * AggregationBuilders.dateHistogram("by_year").field("dateOfBirth").interval((DateHistogramInterval.YEAR))
|
|
|
|
// * .subAggregation(
|
|
|
|
// * AggregationBuilders.avg("avg_children").field("children")
|
|
|
|
// * )
|
|
|
|
// * );
|
|
|
|
// * @return 聚合结果集
|
|
|
|
// */
|
|
|
|
// public Map<String, Aggregation> structAggregationQuery(TransportClient client,String index,String type,
|
|
|
|
// BoolQueryBuilder boolQueryBuilder,
|
|
|
|
// LinkedList<AbstractAggregationBuilder> aggBuilderList){
|
|
|
|
// SearchRequestBuilder searchRequestBuilder = client.prepareSearch(index).setTypes(type).setQuery(boolQueryBuilder);
|
|
|
|
// for(AbstractAggregationBuilder aggBuilder : aggBuilderList){
|
|
|
|
// searchRequestBuilder.addAggregation(aggBuilder);
|
|
|
|
// }
|
|
|
|
// SearchResponse response = searchRequestBuilder.execute().actionGet();
|
|
|
|
// Map<String, Aggregation> map = response.getAggregations().getAsMap();
|
|
|
|
// client.close();
|
|
|
|
// return map;
|
|
|
|
// }
|
|
|
|
|
|
/**
|
|
/**
|
|
* 表格型 - 数据结果解析
|
|
* 表格型 - 数据结果解析
|
|
@ -148,24 +148,10 @@ public class AggregationBuildHandler {
|
|
* @param map 聚合查询结果
|
|
* @param map 聚合查询结果
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
public List<Map<String, Object>> tableDataParsing(String viewCode ,Map<String, Aggregation> map){
|
|
|
|
// List<String> rowList = new LinkedList<>();
|
|
|
|
// rowList.add("town_terms");
|
|
|
|
// rowList.add("total");
|
|
|
|
// List<String> cloumnList = new ArrayList<>();
|
|
|
|
// cloumnList.add("total_count_result");
|
|
|
|
// cloumnList.add("sex_terms");
|
|
|
|
|
|
|
|
View view = viewDao.findByCode(viewCode);
|
|
|
|
if(view == null){
|
|
|
|
logger.debug("视图不存在");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
public List<Map<String, Object>> tableDataParsing(View view,Map<String, Aggregation> map){
|
|
List<String> rowList = getRowDimensionList(view.getId());
|
|
List<String> rowList = getRowDimensionList(view.getId());
|
|
List<String> cloumnList = getColumnDimensionList(view.getId());
|
|
List<String> cloumnList = getColumnDimensionList(view.getId());
|
|
|
|
|
|
List<ViewQuota> viewQuotas = viewQuotaDao.findByViewId(view.getId());
|
|
List<ViewQuota> viewQuotas = viewQuotaDao.findByViewId(view.getId());
|
|
// List<ViewQuotaFilter> viewFilters = viewQuotaFilterDao.findByRelationId(view.getId());
|
|
|
|
Map<String, Object> quotaMap = new HashMap<>();
|
|
Map<String, Object> quotaMap = new HashMap<>();
|
|
for(ViewQuota viewQuota :viewQuotas){
|
|
for(ViewQuota viewQuota :viewQuotas){
|
|
List<ViewQuotaFilter> viewQuotaFilters = viewQuotaFilterDao.findByRelationId(viewQuota.getId());
|
|
List<ViewQuotaFilter> viewQuotaFilters = viewQuotaFilterDao.findByRelationId(viewQuota.getId());
|
|
@ -254,13 +240,8 @@ public class AggregationBuildHandler {
|
|
* @param map 聚合查询结果
|
|
* @param map 聚合查询结果
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
public Map<String, Object> numericalDataParsing(String viewCode ,Map<String, Aggregation> map){
|
|
|
|
|
|
public Map<String, Object> numericalDataParsing(View view,Map<String, Aggregation> map){
|
|
Map<String, Object> resultMap = new HashMap<>();
|
|
Map<String, Object> resultMap = new HashMap<>();
|
|
View view = viewDao.findByCode(viewCode);
|
|
|
|
if(view == null){
|
|
|
|
logger.debug("视图不存在");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
List<ViewQuota> viewQuotas = viewQuotaDao.findByViewId(view.getId());
|
|
List<ViewQuota> viewQuotas = viewQuotaDao.findByViewId(view.getId());
|
|
Map<String, Object> quotaMap = new HashMap<>();
|
|
Map<String, Object> quotaMap = new HashMap<>();
|
|
for(ViewQuota viewQuota :viewQuotas){
|
|
for(ViewQuota viewQuota :viewQuotas){
|
|
@ -294,33 +275,22 @@ public class AggregationBuildHandler {
|
|
* @param map 聚合查询结果
|
|
* @param map 聚合查询结果
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
public Option ehartDataParsing(String viewCode ,Map<String, Aggregation> map){
|
|
|
|
|
|
public Option ehartDataParsing(View view,Map<String, Aggregation> map) throws Exception{
|
|
Option option = null;
|
|
Option option = null;
|
|
ReportOption reportOption = new ReportOption();
|
|
ReportOption reportOption = new ReportOption();
|
|
List<String> xDataList = new LinkedList<>();
|
|
List<String> xDataList = new LinkedList<>();
|
|
List<String> charTypes = new LinkedList<>();
|
|
List<String> charTypes = new LinkedList<>();
|
|
List<String> lineNames = new LinkedList<>();
|
|
List<String> lineNames = new LinkedList<>();
|
|
List<List<Object>> lineDataList = new LinkedList<>();
|
|
List<List<Object>> lineDataList = new LinkedList<>();
|
|
|
|
|
|
List<String> rowList = new LinkedList<>();
|
|
|
|
rowList.add("town_terms");
|
|
|
|
View view = viewDao.findByCode(viewCode);
|
|
|
|
// rowList = getRowDimensionList(view.getId());
|
|
|
|
|
|
List<String> rowList = getRowDimensionList(view.getId());
|
|
|
|
Map<String,List<Object>> quotasDataMap = new LinkedHashMap<>();
|
|
if(rowList.size() != 1){
|
|
if(rowList.size() != 1){
|
|
logger.debug("图表型指标维度配置有误");
|
|
logger.debug("图表型指标维度配置有误");
|
|
return null;
|
|
|
|
|
|
throw new Exception("图表型指标维度配置有误");
|
|
}
|
|
}
|
|
String rowAggCode = rowList.get(0);
|
|
String rowAggCode = rowList.get(0);
|
|
|
|
|
|
List<String> quotaList = new LinkedList<>();
|
|
List<String> quotaList = new LinkedList<>();
|
|
Map<String,List<Object>> quotasDataMap = new LinkedHashMap<>();
|
|
|
|
List<ViewQuota> viewQuotas = new ArrayList<>();
|
|
|
|
ViewQuota vq = new ViewQuota();
|
|
|
|
vq.setChartType("1");
|
|
|
|
vq.setCode("total");
|
|
|
|
vq.setName("各区县总人数");
|
|
|
|
viewQuotas.add(vq);
|
|
|
|
// viewQuotas = viewQuotaDao.findByViewId(view.getId());
|
|
|
|
|
|
List<ViewQuota> viewQuotas = viewQuotaDao.findByViewId(view.getId());
|
|
for(ViewQuota viewQuota :viewQuotas){
|
|
for(ViewQuota viewQuota :viewQuotas){
|
|
quotaList.add(viewQuota.getCode());
|
|
quotaList.add(viewQuota.getCode());
|
|
charTypes.add(viewQuota.getChartType());
|
|
charTypes.add(viewQuota.getChartType());
|
|
@ -331,14 +301,14 @@ public class AggregationBuildHandler {
|
|
Terms terms = (Terms)aggregation;
|
|
Terms terms = (Terms)aggregation;
|
|
String key = "";
|
|
String key = "";
|
|
List<Terms.Bucket> buckets = terms.getBuckets();
|
|
List<Terms.Bucket> buckets = terms.getBuckets();
|
|
for(String quotaCode: quotaList){
|
|
|
|
|
|
for(ViewQuota viewQuota: viewQuotas){
|
|
List<Object> quotaData = new ArrayList<>();
|
|
List<Object> quotaData = new ArrayList<>();
|
|
for(Terms.Bucket bucket : buckets){
|
|
for(Terms.Bucket bucket : buckets){
|
|
key = bucket.getKeyAsString() != null ? bucket.getKeyAsString() : bucket.getKey().toString();
|
|
key = bucket.getKeyAsString() != null ? bucket.getKeyAsString() : bucket.getKey().toString();
|
|
xDataList.add(key);
|
|
xDataList.add(key);
|
|
List<Aggregation> aggregationList = bucket.getAggregations().asList();
|
|
List<Aggregation> aggregationList = bucket.getAggregations().asList();
|
|
for(Aggregation subAgg : aggregationList){
|
|
for(Aggregation subAgg : aggregationList){
|
|
if(subAgg.getName().contains(quotaCode)){
|
|
|
|
|
|
if(subAgg.getName().contains(viewQuota.getBasicFormulaType())){
|
|
Map<String, Object> subDataMap = getInternalAggValue(subAgg.getName(),subAgg);
|
|
Map<String, Object> subDataMap = getInternalAggValue(subAgg.getName(),subAgg);
|
|
if(subDataMap != null || subDataMap.size() > 0){
|
|
if(subDataMap != null || subDataMap.size() > 0){
|
|
quotaData.add(subDataMap.get(subAgg.getName()));
|
|
quotaData.add(subDataMap.get(subAgg.getName()));
|
|
@ -346,7 +316,7 @@ public class AggregationBuildHandler {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
quotasDataMap.put(quotaCode, quotaData);
|
|
|
|
|
|
quotasDataMap.put(viewQuota.getCode(), quotaData);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
for (Map.Entry<String,List<Object>> entry : quotasDataMap.entrySet()) {
|
|
for (Map.Entry<String,List<Object>> entry : quotasDataMap.entrySet()) {
|