3

我正在尝试使用 apache-beam 创建一个流管道,该管道从 google pub/sub 读取句子并将单词写入 Bigquery 表。

我正在使用0.6.0apache-beam 版本。

按照示例,我做了这个:

public class StreamingWordExtract {

/**
 * A DoFn that tokenizes lines of text into individual words.
 */
static class ExtractWords extends DoFn<String, String> {
    @ProcessElement
    public void processElement(ProcessContext c) {
        String[] words = ((String) c.element()).split("[^a-zA-Z']+");
        for (String word : words) {
            if (!word.isEmpty()) {
                c.output(word);
            }
        }
    }
}

/**
 * A DoFn that uppercases a word.
 */
static class Uppercase extends DoFn<String, String> {
    @ProcessElement
    public void processElement(ProcessContext c) {
        c.output(c.element().toUpperCase());
    }
}


/**
 * A DoFn that uppercases a word.
 */
static class StringToRowConverter extends DoFn<String, TableRow> {
    @ProcessElement
    public void processElement(ProcessContext c) {
        c.output(new TableRow().set("string_field", c.element()));
    }

    static TableSchema getSchema() {
        return new TableSchema().setFields(new ArrayList<TableFieldSchema>() {
            // Compose the list of TableFieldSchema from tableSchema.
            {
                add(new TableFieldSchema().setName("string_field").setType("STRING"));
            }
        });
    }

}

private interface StreamingWordExtractOptions extends ExampleBigQueryTableOptions, ExamplePubsubTopicOptions {
    @Description("Input file to inject to Pub/Sub topic")
    @Default.String("gs://dataflow-samples/shakespeare/kinglear.txt")
    String getInputFile();

    void setInputFile(String value);
}

public static void main(String[] args) {
    StreamingWordExtractOptions options = PipelineOptionsFactory.fromArgs(args)
            .withValidation()
            .as(StreamingWordExtractOptions.class);

    options.setBigQuerySchema(StringToRowConverter.getSchema());

    Pipeline p = Pipeline.create(options);

    String tableSpec = new StringBuilder()
            .append(options.getProject()).append(":")
            .append(options.getBigQueryDataset()).append(".")
            .append(options.getBigQueryTable())
            .toString();

    p.apply(PubsubIO.read().topic(options.getPubsubTopic()))
            .apply(ParDo.of(new ExtractWords()))
            .apply(ParDo.of(new StringToRowConverter()))
            .apply(BigQueryIO.Write.to(tableSpec)
                    .withSchema(StringToRowConverter.getSchema())
                    .withCreateDisposition(BigQueryIO.Write.CreateDisposition.CREATE_IF_NEEDED)
                    .withWriteDisposition(BigQueryIO.Write.WriteDisposition.WRITE_APPEND));

    PipelineResult result = p.run();


}

我在附近有一个错误:

apply(ParDo.of(new ExtractWords()))

因为前一个apply不是返回一个String而是一个Object

我想问题是从返回的类型PubsubIO.read().topic(options.getPubsubTopic())。类型是PTransform<PBegin, PCollection<T>>而不是PTransform<PBegin, PCollection<String>>

使用 apache-beam 从 google pub/sub 读取的正确方法是什么?

4

1 回答 1

6

您最近在 Beam 中遇到了向后不兼容的更改——对此感到抱歉!

从 Apache Beam 版本 0.5.0 开始,PubsubIO.Read需要PubsubIO.Write使用PubsubIO.<T>read()andPubsubIO.<T>write()而不是静态工厂方法(例如PubsubIO.Read.topic(String).

.withCoder(Coder)需要为输出类型指定编码器通孔Read.withAttributes(SimpleFunction<T, PubsubMessage>)需要为输入类型指定编码器,或通过指定格式函数Write

于 2017-03-21T00:16:20.810 回答