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

我正在使用0.6.0 apache-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中读取的正确方法?

最佳答案

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

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

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

07-24 21:45