|
@ -0,0 +1,258 @@
|
|
|
|
|
|
package com.yihu.quota.util;
|
|
|
|
|
|
import com.yihu.quota.vo.ViewDataModel;
|
|
|
import org.elasticsearch.action.search.SearchRequestBuilder;
|
|
|
import org.elasticsearch.action.search.SearchResponse;
|
|
|
import org.elasticsearch.client.transport.TransportClient;
|
|
|
import org.elasticsearch.index.query.BoolQueryBuilder;
|
|
|
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
|
|
|
import org.elasticsearch.search.aggregations.Aggregation;
|
|
|
import org.elasticsearch.search.aggregations.AggregationBuilders;
|
|
|
import org.elasticsearch.search.aggregations.bucket.terms.*;
|
|
|
import org.elasticsearch.search.aggregations.metrics.avg.InternalAvg;
|
|
|
import org.elasticsearch.search.aggregations.metrics.max.InternalMax;
|
|
|
import org.elasticsearch.search.aggregations.metrics.min.InternalMin;
|
|
|
import org.elasticsearch.search.aggregations.metrics.sum.InternalSum;
|
|
|
import org.elasticsearch.search.aggregations.metrics.valuecount.InternalValueCount;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* Created by janseny on 2018/9/28.
|
|
|
*/
|
|
|
@Component
|
|
|
public class AggregationBuildHandler {
|
|
|
|
|
|
/**
|
|
|
* 简单聚合
|
|
|
* @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 termsBuilder= AggregationBuilders.terms(termName).field(fieldName);
|
|
|
return termsBuilder;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
*
|
|
|
* @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 List<Map<String, Object>> 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();
|
|
|
List<Map<String, Object>> resultList = new LinkedList<Map<String, Object>>();
|
|
|
Map<String, Aggregation> map = response.getAggregations().getAsMap();
|
|
|
for(String key:map.keySet()){
|
|
|
Aggregation aggregation = map.get(key);
|
|
|
parsingAggregation(resultList,aggregation);
|
|
|
}
|
|
|
client.close();
|
|
|
LinkedList<String> rowDimensionList = new LinkedList<>();
|
|
|
rowDimensionList.add("区县_terms");
|
|
|
LinkedList<String> columnDimensionList = new LinkedList<>();
|
|
|
columnDimensionList.add("性别_terms");
|
|
|
return convertList(resultList,rowDimensionList,columnDimensionList);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 递归解析 json
|
|
|
* @param resultList
|
|
|
* @param
|
|
|
*/
|
|
|
private void expainAggregationResult(List<Map<String, Object>> resultList,Iterator<Terms.Bucket> gradeBucket,String aggKey){
|
|
|
System.out.println("aggKey=" + aggKey);
|
|
|
while (gradeBucket.hasNext()) {
|
|
|
Terms.Bucket b = gradeBucket.next();
|
|
|
String subVal = StringUtils.isEmpty(aggKey) ? b.getKey().toString() : (aggKey + ":" + b.getKey());
|
|
|
for(Aggregation aggregation : b.getAggregations().asList()){
|
|
|
System.out.println("bucketNmae = " + b.getKey().toString() + "; aggName=" + aggregation.getName());
|
|
|
subVal += ";"+ aggregation.getName();
|
|
|
if (aggregation instanceof InternalValueCount) {
|
|
|
Map<String, Object> countMap = new HashMap<>();
|
|
|
InternalValueCount valueCount = (InternalValueCount) aggregation;
|
|
|
countMap.put(subVal, valueCount.getValue() );
|
|
|
resultList.add(countMap);
|
|
|
}else if (aggregation instanceof InternalSum) {
|
|
|
Map<String, Object> sumMap = new HashMap<>();
|
|
|
InternalSum valueCount = (InternalSum) aggregation;
|
|
|
sumMap.put(subVal, valueCount.getValue() );
|
|
|
resultList.add(sumMap);
|
|
|
}else if (aggregation instanceof InternalMax) {
|
|
|
Map<String, Object> maxMap = new HashMap<>();
|
|
|
InternalMax valueCount = (InternalMax) aggregation;
|
|
|
maxMap.put(subVal, valueCount.getValue() );
|
|
|
resultList.add(maxMap);
|
|
|
}else if (aggregation instanceof InternalMin) {
|
|
|
Map<String, Object> minMap = new HashMap<>();
|
|
|
InternalMin valueCount = (InternalMin) aggregation;
|
|
|
minMap.put(subVal, valueCount.getValue() );
|
|
|
resultList.add(minMap);
|
|
|
}else if (aggregation instanceof InternalAvg) {
|
|
|
Map<String, Object> avgMap = new HashMap<>();
|
|
|
InternalAvg valueCount = (InternalAvg) aggregation;
|
|
|
avgMap.put(subVal, valueCount.getValue() );
|
|
|
resultList.add(avgMap);
|
|
|
}else {
|
|
|
Terms terms = (Terms)aggregation;
|
|
|
expainAggregationResult(resultList,terms.getBuckets().iterator(),subVal);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void parsingAggregation(List<Map<String, Object>> resultList,Aggregation aggregation){
|
|
|
if (aggregation instanceof InternalValueCount) {
|
|
|
Map<String, Object> countMap = new HashMap<>();
|
|
|
InternalValueCount valueCount = (InternalValueCount) aggregation;
|
|
|
countMap.put(valueCount.getName() + ":"+ aggregation.getName(), valueCount.getValue() );
|
|
|
System.out.println(valueCount.getName());
|
|
|
resultList.add(countMap);
|
|
|
}else if (aggregation instanceof InternalSum) {
|
|
|
Map<String, Object> sumMap = new HashMap<>();
|
|
|
InternalSum valueCount = (InternalSum) aggregation;
|
|
|
sumMap.put(valueCount.getName(), valueCount.getValue() );
|
|
|
System.out.println(valueCount.getName());
|
|
|
resultList.add(sumMap);
|
|
|
}else if (aggregation instanceof InternalMax) {
|
|
|
Map<String, Object> maxMap = new HashMap<>();
|
|
|
InternalMax valueCount = (InternalMax) aggregation;
|
|
|
maxMap.put(valueCount.getName(), valueCount.getValue() );
|
|
|
resultList.add(maxMap);
|
|
|
}else if (aggregation instanceof InternalMin) {
|
|
|
Map<String, Object> minMap = new HashMap<>();
|
|
|
InternalMin valueCount = (InternalMin) aggregation;
|
|
|
minMap.put(valueCount.getName(), valueCount.getValue() );
|
|
|
resultList.add(minMap);
|
|
|
}else if (aggregation instanceof InternalAvg) {
|
|
|
Map<String, Object> avgMap = new HashMap<>();
|
|
|
InternalAvg valueCount = (InternalAvg) aggregation;
|
|
|
avgMap.put(valueCount.getName(), valueCount.getValue() );
|
|
|
resultList.add(avgMap);
|
|
|
}else {
|
|
|
Terms terms = (Terms)aggregation;
|
|
|
String termName = terms.getName();
|
|
|
System.out.println("termName:" + termName);
|
|
|
expainAggregationResult(resultList, terms.getBuckets().iterator(), termName);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 行列数据转换
|
|
|
* @param dataList
|
|
|
* @param rowDimensionList
|
|
|
* @param columnDimensionList
|
|
|
* @return
|
|
|
*/
|
|
|
public List<Map<String, Object>> convertList(List<Map<String, Object>> dataList,LinkedList<String> rowDimensionList ,LinkedList<String> columnDimensionList){
|
|
|
List<Map<String,Object>> dataMapList = new ArrayList<>();
|
|
|
List<String> rowList = new ArrayList<>();
|
|
|
for(String rowDimen : rowDimensionList){
|
|
|
for(Map<String, Object> map : dataList) {
|
|
|
for (String key : map.keySet()) {
|
|
|
List<String> result = Arrays.asList(key.split(";"));//区县_terms:信州区 ;性别_terms ; 合计_count_result
|
|
|
for(String str : result){
|
|
|
if(str.contains(rowDimen)){
|
|
|
if(!rowList.contains(str)){
|
|
|
rowList.add(str);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
Map<String,Object> dataMap = new HashMap<>();
|
|
|
for(String row : rowList){
|
|
|
dataMap.put(row,row);
|
|
|
for(String columnDimen : columnDimensionList){
|
|
|
|
|
|
for(Map<String, Object> map : dataList) {
|
|
|
for (String key : map.keySet()) {
|
|
|
if(key.contains(row) && key.contains(columnDimen)){
|
|
|
List<String> result = Arrays.asList(key.split(";"));
|
|
|
for(String str : result){
|
|
|
dataMap.put(columnDimen,columnDimen);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
dataMapList.add(dataMap);
|
|
|
}
|
|
|
//未调试完成
|
|
|
return dataMapList;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|