# aggregation **Repository Path**: 412/aggregation ## Basic Information - **Project Name**: aggregation - **Description**: Java聚合操作,代码层面对数据进行分组聚合统计,解耦统计规则 - **Primary Language**: Unknown - **License**: MulanPSL-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 1 - **Created**: 2021-10-27 - **Last Updated**: 2023-01-06 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # aggregation #### 介绍 Java聚合操作,代码层面对数据进行分组聚合统计,解耦统计规则 #### 使用说明 #### 输入对象类ProfitInput: protected String complete_time;// 订单支付时间 yyyy-mm-dd hh:mm:ss pay_info_ref>complete_time 支付数据中的支付完成时间 protected String channel;// 渠道 channel protected Integer applyType;// 退款类型 1自愿 2非自愿 serv_order_ref>applyReason 通过applyReason判断自愿或者非自愿 protected int relatedPassengers;// 退款人数 serv_order_ref>relatedPassengers 退票id计数 protected BigDecimal profit; #### 聚合输出对象类ProfitOut: protected String payDate;// 支付日期 订单支付时间 退票数据中的支付时间解析出的支付日期 protected String channel;// 渠道 channel 聚合的渠道名。 protected int nfPassgerCount;// 自愿退票量 退款类型,退款乘机人数 每个支付日期的自愿的退票总人数 protected int yfPassgerCount;// 非自愿退票量 退款类型,退款乘机人数 每个支付日期的非自愿的退票总人数 protected int passgerCount;// 退票总票量 退款乘机人数 每个支付日期的退票总人数 protected BigDecimal totalProfit = new BigDecimal(0);;// 总退票利润 退款收益 每个日期的收益总和 protected BigDecimal avgProfit = new BigDecimal(0);;// #### 聚合实现过程: new Aggregation() #### 分组 .groupBy(new String[] { "complete_time", "payDate" }, new String[] { "channel", "channel" }) #### 聚合字段 .outClass(ProfitOut.class).addAggregationField("nfPassgerCount", 0, new IAggregation() { @Override public void aggregation(ProfitInput input, AggregationFieldResult out) { // 自愿 if (input.getApplyType() == 1) { out.setResult((Integer) out.getResult() + (Integer) input.getRelatedPassengers()); } } }); ####聚合后再計算 .addPostAggregationField("avgProfit", new BigDecimal(0), new IAggregationPost() { @Override public Object post(GroupByKey key, Map> aggregations) { int passgerCount = (int) aggregations.get("passgerCount").getResult(); BigDecimal totalMoney = (BigDecimal) aggregations.get("totalProfit").getResult(); return totalMoney.divide(new BigDecimal(passgerCount), 2, BigDecimal.ROUND_HALF_UP); } }) #### main执行 public static void main(String args[]) throws Exception { List inputs = Lists.newArrayList(); RandomUtil.randomEle(Lists.newArrayList("zhixng", "elong", "khj")); int count = 200; for (int i = 0; i < count; i++) { String complete_time = RandomUtil.randomEle(Lists.newArrayList("2021-10-27", "2021-10-26", "2021-10-25")); String channel = RandomUtil.randomEle(Lists.newArrayList("zhixng", "elong", "khj")); Integer applyType = RandomUtil.randomEle(Lists.newArrayList(1, 2)); Integer relatedPassengers = RandomUtil.randomEle(Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9)); BigDecimal profit = RandomUtil .randomEle(Lists.newArrayList(new BigDecimal("2"), new BigDecimal("3"), new BigDecimal("0"))); ProfitInput input = new ProfitInput(complete_time, channel, applyType, relatedPassengers, profit); inputs.add(input); } List outs = profit.aggregating(inputs); for (ProfitOut out : outs) { System.out.println(out); } } #### 执行结果 ProfitOut(payDate=2021-10-26, channel=elong, nfPassgerCount=39, yfPassgerCount=28, passgerCount=67, totalProfit=30) ProfitOut(payDate=2021-10-26, channel=khj, nfPassgerCount=37, yfPassgerCount=69, passgerCount=106, totalProfit=52) ProfitOut(payDate=2021-10-27, channel=zhixng, nfPassgerCount=60, yfPassgerCount=70, passgerCount=130, totalProfit=46) ProfitOut(payDate=2021-10-25, channel=zhixng, nfPassgerCount=64, yfPassgerCount=54, passgerCount=118, totalProfit=39) ProfitOut(payDate=2021-10-27, channel=elong, nfPassgerCount=58, yfPassgerCount=56, passgerCount=114, totalProfit=35) ProfitOut(payDate=2021-10-25, channel=elong, nfPassgerCount=30, yfPassgerCount=19, passgerCount=49, totalProfit=23) ProfitOut(payDate=2021-10-25, channel=khj, nfPassgerCount=18, yfPassgerCount=86, passgerCount=104, totalProfit=41) ProfitOut(payDate=2021-10-27, channel=khj, nfPassgerCount=57, yfPassgerCount=73, passgerCount=130, totalProfit=42) ProfitOut(payDate=2021-10-26, channel=zhixng, nfPassgerCount=25, yfPassgerCount=67, passgerCount=92, totalProfit=29)