use of org.elasticsearch.search.aggregations.metrics.sum.ParsedSum in project incubator-inlong by apache.
the class AuditServiceImpl method listByCondition.
/**
* Query audit data for list by condition
*
* @param request The audit request of query condition
* @return The result of query
*/
@Override
public List<AuditVO> listByCondition(AuditRequest request) throws IOException {
LOGGER.info("begin query audit list request={}", request);
Preconditions.checkNotNull(request, "request is null");
List<AuditVO> result = new ArrayList<>();
AuditQuerySource querySource = AuditQuerySource.valueOf(auditQuerySource);
for (String auditId : request.getAuditIds()) {
if (AuditQuerySource.MYSQL == querySource) {
String format = "%Y-%m-%d %H:%i:00";
// Support min agg at now
DateTimeFormatter forPattern = DateTimeFormat.forPattern("yyyy-MM-dd");
DateTime dtDate = forPattern.parseDateTime(request.getDt());
String eDate = dtDate.plusDays(1).toString(forPattern);
List<Map<String, Object>> sumList = auditEntityMapper.sumByLogTs(request.getInlongGroupId(), request.getInlongStreamId(), auditId, request.getDt(), eDate, format);
List<AuditInfo> auditSet = sumList.stream().map(s -> {
AuditInfo vo = new AuditInfo();
vo.setLogTs((String) s.get("logTs"));
vo.setCount(((BigDecimal) s.get("total")).longValue());
return vo;
}).collect(Collectors.toList());
result.add(new AuditVO(auditId, auditSet));
} else if (AuditQuerySource.ELASTICSEARCH == querySource) {
String index = String.format("%s_%s", request.getDt().replaceAll("-", ""), auditId);
if (elasticsearchApi.indexExists(index)) {
SearchResponse response = elasticsearchApi.search(toAuditSearchRequest(index, request.getInlongGroupId(), request.getInlongStreamId()));
final List<Aggregation> aggregations = response.getAggregations().asList();
if (CollectionUtils.isNotEmpty(aggregations)) {
ParsedTerms terms = (ParsedTerms) aggregations.get(0);
if (CollectionUtils.isNotEmpty(terms.getBuckets())) {
List<AuditInfo> auditSet = terms.getBuckets().stream().map(bucket -> {
AuditInfo vo = new AuditInfo();
vo.setLogTs(bucket.getKeyAsString());
vo.setCount((long) ((ParsedSum) bucket.getAggregations().asList().get(0)).getValue());
return vo;
}).collect(Collectors.toList());
result.add(new AuditVO(auditId, auditSet));
}
}
} else {
LOGGER.warn("Elasticsearch index={} not exists", index);
}
}
}
LOGGER.info("success to query audit list for request={}", request);
return result;
}
use of org.elasticsearch.search.aggregations.metrics.sum.ParsedSum in project blog-example by my-dlq.
the class AggrMetricService method aggregationSum.
/**
* sum 统计员工工资总值
*/
public Object aggregationSum() {
String responseResult = "";
try {
// 设置聚合条件
SumAggregationBuilder aggr = AggregationBuilders.sum("salary_sum").field("salary");
// 查询源构建器
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.aggregation(aggr);
searchSourceBuilder.size(0);
// 创建查询请求对象,将查询条件配置到其中
SearchRequest request = new SearchRequest("mydlq-user");
request.source(searchSourceBuilder);
// 执行请求
SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
// 获取响应中的聚合信息
Aggregations aggregations = response.getAggregations();
// 输出内容
if (RestStatus.OK.equals(response.status()) || aggregations != null) {
// 转换为 Sum 对象
ParsedSum aggregation = aggregations.get("salary_sum");
log.info("-------------------------------------------");
log.info("聚合信息:");
log.info("sum:{}", String.valueOf((aggregation.getValue())));
log.info("-------------------------------------------");
}
// 根据具体业务逻辑返回不同结果,这里为了方便直接将返回响应对象Json串
responseResult = response.toString();
} catch (IOException e) {
log.error("", e);
}
return responseResult;
}
Aggregations