ES实现百亿级数据实时分析实战案例
程序源代码
共 2688字,需浏览 6分钟
·
2021-01-15 00:48
背景
覆盖率:所有样本中出现某一特征的样本的比例
正样本占比:所有出现该特征的样本中,正样本的比例
负样本占比:所有出现该特征的样本中,负样本的比例
技术方案
第一种:用Spark流式计算,计算每一种可能单个或组合特征的相关指标
第二种:收到客户端请求后,遍历HDFS中相关数据,进行离线计算
第三种:将数据按照实验+小时分索引存入ES,收到客户端请求后,实时计算返回
技术架构
代码实现
// 启动并行任务
final Map>> futures = Maps.newHashMap();
for(String metric : metrics) { // 遍历要计算的指标
final SampleRatio sampleRatio = getSampleRatio(metric);
for (String exptId : expts) { // 遍历目标实验列表
for (String id : features) { // 遍历要分析的特征
final String name = getMetricsName(exptId, sampleRatio, id);
final List> resultList = Lists.newArrayList();
for (Date hour : coveredHours) { // 将时间按照小时进行拆分
final String fieldName = getFieldName(isFect ? Constants.FACET_COLLECT : Constants.FEATURE_COLLECT, id);
final GetCoverageTask task = new GetCoverageTask(exptId, fieldName, sampleRatio, hour);
// 启动并行任务
final Futurefuture = TaskExecutor.submit(task);
resultList.add(future);
}
futures.put(name, resultList);
}
}
}
final QueryRes queryRes = new QueryRes();
final Iterator>>> it = futures.entrySet().iterator();
while (it.hasNext()){
// 省略结果处理流程
}
// 1\. 对文档进行聚合运行,分别得到基础文档的数量,以及目标文档数量
final AggregationBuilder[] agg = getAggregationBuilder(sampleRatio, fieldName);
final SearchSourceBuilder searchBuilder = new SearchSourceBuilder();
searchBuilder.aggregation(agg[0]).aggregation(agg[1]).size(0);
// 2\. 得到覆盖率
final String indexName = getIndexName(exptId, hour);
final Search search = new Search.Builder(searchBuilder.toString())
.addIndex(indexName).addType(getType()).build();
final SearchResult result = jestClient.execute(search);
if(result.getResponseCode() != HttpUtils.STATUS_CODE_200){
// 请求出错
log.warn(result.getErrorMessage());
return 0f;
}
final MetricAggregation aggregations = result.getAggregations();
// 3\. 解析结果
final long dividend ;
if(SampleRatio.ALL == sampleRatio){
dividend = aggregations.getValueCountAggregation(Constants.DIVIDEND).getValueCount();
}else {
dividend = aggregations.getFilterAggregation(Constants.DIVIDEND).getCount();
}
// 防止出现被除数为0时程序异常
if(dividend <= 0){
return 0f;
}
long divisor = aggregations.getFilterAggregation(Constants.DIVISOR).getCount();
return divisor / (float)dividend;
int label = 0;
final ExistsQueryBuilder existsQuery = QueryBuilders.existsQuery(fieldName);
// 包含指定特征的正样本数量
final BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
final Listmust = boolQuery.must();
// 计算样本数量
TermQueryBuilder labelQuery = null;
if(SampleRatio.POSITIVE == sampleRatio) {
// 计算正样本数量
label = 1;
labelQuery = QueryBuilders.termQuery(Constants.LABEL, label);
must.add(labelQuery);
}else if(SampleRatio.NEGATIVE == sampleRatio) {
// 计算负样本数量
labelQuery = QueryBuilders.termQuery(Constants.LABEL, label);
must.add(labelQuery);
}
must.add(existsQuery);
final ValueCountAggregationBuilder existsCountAgg = AggregationBuilders.count(sampleRatio.getField());
existsCountAgg.field(fieldName);
final FilterAggregationBuilder filterAgg = AggregationBuilders.filter(aggName, boolQuery);
filterAgg.subAggregation(existsCountAgg);
return filterAgg;
上线效果
评论