Apache Beam:简介

276 阅读4分钟

目录

阅读时间: 3 分钟

Apache Beam是一个统一的编程模型,以同样的方式处理流和批处理数据。我们可以在beam中创建一个管道,任何一个beam SDK(Python/Java/Go语言)都可以在任何支持的执行引擎上运行,即Apache Spark, Apache Flink, Apache Apex, Apache Samza, Apache Gearpump, 和Google Cloud dataflow(未来还有更多加入)。

使用Apache beam,你可以应用同样的操作,不管是来自一些批处理数据源(如HDFS文件)的有界数据,还是来自一些流媒体源(如Kafka)的无界数据。

apache驱动程序是如何工作的?

首先,创建一个Pipeline对象,并设置管道的执行方式(使用Apache Spark、Apache Apex等哪个运行器)。

其次,从一些外部存储或内存数据中创建Pcollection。然后应用PTransforms对Pcollection中的每个元素进行转换,产生输出Pcollection。

你可以对数据进行过滤、分组、分析或做任何其他处理。

最后,使用IO库将最终的Pcollection存储到一些外部存储系统。当我们运行这个驱动程序时,它会从管道中创建一个工作流图,然后作为一个异步作业在底层运行器上执行。

Apache驱动程序

PipelineOptions options = PipelineOptionsFactory.create();
// Then create the pipeline using pipeline options

Pipeline p = Pipeline.create(options);

// Create the PCollection reading from file
PCollection < String >
    lines = p.apply

    ("TextFile", TextIO.read().from("protocol://inputPath"));

//Apply transformation
PCollection < String >
    output = lines.apply(Some transformation)

//Write final Pcollection to some external source
output.apply(TextIO.write().to("protocol://outputPath"));

Beam转换

Pardo

Pardo转换在PCollection中的每个元素上应用一个处理函数,在产生的PCollection中产生零、一个或多个元素。

你可以使用Pardo函数对PCollection中的元素进行过滤、格式化、计算、提取和类型转换。

为了应用Pardo,你需要创建一个扩展DoFn的类,它将包含一个用@ProcessElement注释的方法,该函数将包含应用于PCollection中每个元素的处理逻辑,并给出结果。

PipelineOptions options = PipelineOptionsFactory.create();
// Then create the pipeline using pipeline options

Pipeline p = Pipeline.create(options);

// Create Pcollection from a text file.
PCollection < String >
    words = p.apply

    (“TextFile ", TextIO.read ().from("
    protocol: //inputPath "));

    // The DoFn to count length of each element in the input PCollection.
    static class WordLengthFn extends DoFn < String, Integer > {
        @ProcessElement
        public void processElement(@Element String word, OutputReceiver < Integer >
            out) {
            out.output(word.length());
        }
    }

    // Apply a ParDo to the PCollection "words" to compute lengths for each word.
    PCollection < Integer >
    wordLengths = words.apply(
        ParDo.of(new ComputeWordLengthFn()));
}

GroupByKey

GroupByKey将所有与某一特定键相关的值分组。假设我们有下面的数据,其中键是月份,值是生日在那个月的人的名字,我们想把所有生日在同一个月的人分组。

要在无界数据上使用GroupByKey,你可以使用窗口或触发器来对落在该特定窗口的有限数据集进行分组操作。例如,如果你定义了一个固定的3分钟的窗口大小,那么所有在3分钟内的数据都会被基于键进行分组。

CoGroupByKey

CoGroupByKey连接两组或更多具有相同键的数据。例如,你有一个文件,其中包含以电话号码为键的人名,第二个文件以人名为键,以电子邮件地址为值。

因此,使用CoGroupByKey将这两个文件基于人名连接起来,将产生一个新的Pcollection,这个Pcollection将以人名为键,以电话和电子邮件地址为值。正如在GroupByKey中所讨论的,对于无界的数据,我们必须使用窗口和触发器来使用CoGroupByKey聚合数据。

扁平化

Flatten将PC集合的列表合并成一个PC集合。

// Flatten takes a list of Pcollection and returns a single PCollection PCollection

<String>
    pc1 = … PCollection
    <String>
        pc2 = … PCollection
        <String>
            pc3 = … PCollectionList
            <String>
                collections = PCollectionList.of(pc1).and(pc2).and(pc3); PCollection
                <String> mergedPCollections = collections.apply(Flatten.
                    <String>pCollections());

分区

与Flatten相反,它根据用户提供的分区功能,将一个单一的PC集合分割成多个更小的PC集合。

// It takes desired number of result partitions and a PartitionFn
PCollection < Student >
    students = …
    // Split students up into 10 partitions, by percentile:
    PCollectionList < Student >
    studentsByPercentile =
    students.apply(Partition.of(10, new PartitionFn < Student >
        () {
            public int partitionFor(Student student, int numPartitions) {
                return student.getPercentile() * numPartitions / 100;
            }
        }));

总结

毫无疑问,Apache beam是并行处理的未来,它的 "一次写入,随处执行 "使它在大数据开发解决方案生态系统中更加流行。

目前,对后端执行转轮的支持和集成是有限的,但在未来,越来越多的框架会得到集成,使分布式编程更加稳定和统一。