apache beam入门之旁路输入

目录:apache beam 个人使用经验总结目录和入门指导(Java)

如果我们希望给某个PCollection数据集输入1个 计算后 的结果,则就需要用到旁路输入。

例如要计算某个数据集的方差,其公式为
在这里插入图片描述
这个计算过程就需要用到旁路输入,来将平均值u传递给数据集中的每个数字进行计算。

我们下面这个数据集为例

PCollection<Integer> numbers = pipeline.apply(Create.of(10,20,40,70,80));

创建旁路输入视窗

数据集的平均值就是旁路输入,注意这个旁路输入的值必须是单个值,即数据集聚合后的1个值。创建方式如下:

// 使用bema自带的平均值sdk计算平局值
Combine.Globally<Double, Double> combineGlobally = Mean.<Double>globally();
// 加上asSingletonView()转为旁路输入视图
PCollectionView<Double> meanView = numbers.apply(combineGlobally.asSingletonView());

向PTransform对象组装旁路输入

接着建立均值差平方的转化类

// 建立均值差值 平方的转化类
ParDo.SingleOutput<Double, Double> meanSquareTrans = ParDo.<Double, Double>of(new DoFn<Double, Double>() {
    @ProcessElement
    public void precessElement(ProcessContext context) {
        // 根据旁路输入的引用,取出旁路输入值
        Double mean = context.sideInput(meanView);
        Double number = context.element();

        Double answer = Math.abs(number - mean) * Math.abs(number - mean);
        context.output(answer);
    }
});

然后在apply组装的时候,用withSideInput将旁路输入组装进去,便可进行计算

// 组装转化类
// 注意要把meanView作为旁路输入组装进去
// 得到 |数值-平均值|^2 的数据集
PCollection<Double> meanSquare = numbers.apply(meanSquareTrans.withSideInputs(meanView));

注意事项: 如果是基于分布式计算引擎例如spark、flink, 需要确认运行在各executor时, 能否加载到meanView这个PCollectionView引用,如果加载不到,则需要作为私有成员放进DoFn子类中去获取

combine聚合操作组装旁路输入

计算方差需要把meanSquare数据集里的元素一一相加再除去元素总数,因此也要用到旁路输入。

// 计算数据集内的元素数量
PCollectionView<Long> numberCount = numbers.apply(Combine.globally(Count.<Double>combineFn()).asSingletonView());

// 如果是自定义Combine类,numberCount需要作为成员传入,否则分布式计算时无法获取
PCollection<Double> variance = meanSquare.apply(
	Combine.globally(
		new CaculateVarianceCombineFn(numberCount)
	).withSideInputs(numberCount)
);

因为之前介绍的combine方法无法获取context,因为需要改用另一种combineFn,可以拿到上下文文本context,并获取旁路输入

static class CaculateVarianceCombineFn extends CombineWithContext.CombineFnWithContext<Double, Double, Double> {
    private PCollectionView<Long> numberCountView;

    public CaculateVarianceCombineFn(PCollectionView<Long> numberCountView) {
        this.numberCountView = numberCountView;
    }

    // 主要要实现defaultValue这个接口
    @Override
    public Double defaultValue() {
        return Double.valueOf(0);
    }

    @Override
    public Double createAccumulator(CombineWithContext.Context c) {
        return Double.valueOf(0);
    }

    @Override
    public Double addInput(Double accumulator, Double input, CombineWithContext.Context c) {
        return accumulator + input;
    }

    @Override
    public Double mergeAccumulators(Iterable<Double> accumulators, CombineWithContext.Context c) {
        Double mergeResult = createAccumulator(c);
        for(Double accmulator : accumulators) {
            mergeResult += accmulator;
        }
        return mergeResult;
    }

    // 计算最终聚合结果
    @Override
    public Double extractOutput(Double accumulator, CombineWithContext.Context c) {
        Long numberCount = c.sideInput(numberCountView);
        System.out.println("end result=" + accumulator / (numberCount - 1));
        return accumulator / (numberCount - 1);
    }
}

最终计算拓扑图

计算拓扑图如下:

numbers
mean
meanSquare
count
variance
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值