the*_*w89 3 google-cloud-pubsub apache-beam
我正在尝试使用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();
}
Run Code Online (Sandbox Code Playgroud)
我附近有错误:
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梁版本0.5.0,启动PubsubIO.Read并PubsubIO.Write需要使用实例化PubsubIO.<T>read()和PubsubIO.<T>write()代替静态的工厂方法,如PubsubIO.Read.topic(String).
.withCoder(Coder)需要为输出类型指定编码器通道Read.指定一个编码器,用于对输入的类型,或指定经由格式化功能.withAttributes(SimpleFunction<T, PubsubMessage>)需要Write.
| 归档时间: |
|
| 查看次数: |
1928 次 |
| 最近记录: |