[英]How to write to BigQuery with BigQuery IO in Apache Beam?
我正在嘗試設置一個 Apache Beam 管道,該管道從 Kafka 讀取並使用 Apache Beam 寫入 BigQuery。 我正在使用這里的邏輯來過濾掉一些坐標: https://www.talend.com/blog/2018/08/07/developing-data-processing-job-using-apache-beam-streaming-pipeline/ TLDR:主題中的消息格式為 id,x,y。 過濾掉所有 x>100 或 y>100 的消息
我讀取數據,進行幾次轉換,然后定義我的表模式,然后嘗試寫入 Bigquery。 我不確定如何調用 write 方法。 這可能是缺乏 Java Generics 知識。 我相信它應該是一個 PCollection,但不能安靜地弄清楚。
這是管道代碼 - 如果它被認為是代碼轉儲,我只想給出整個上下文:
Pipeline pipeline = Pipeline.create(options);
pipeline
.apply(
KafkaIO.<Long, String>read()
.withBootstrapServers(options.getBootstrap())
.withTopic(options.getInputTopic())
.withKeyDeserializer(LongDeserializer.class)
.withValueDeserializer(StringDeserializer.class))
.apply(
ParDo.of(
new DoFn<KafkaRecord<Long, String>, String>() {
@ProcessElement
public void processElement(ProcessContext processContext) {
KafkaRecord<Long, String> record = processContext.element();
processContext.output(record.getKV().getValue());
}
}))
.apply(
"FilterValidCoords",
Filter.by(new FilterObjectsByCoordinates(options.getCoordX(), options.getCoordY())))
.apply(
"ExtractPayload",
ParDo.of(
new DoFn<String, KV<String, String>>() {
@ProcessElement
public void processElement(ProcessContext c) throws Exception {
c.output(KV.of("filtered", c.element()));
}
}));
TableSchema tableSchema =
new TableSchema()
.setFields(
ImmutableList.of(
new TableFieldSchema()
.setName("x_cord")
.setType("STRING")
.setMode("NULLABLE"),
new TableFieldSchema()
.setName("y_cord")
.setType("STRING")
.setMode("NULLABLE")
));
pipeline
.apply(
"Write data to BQ",
BigQueryIO
.<String, KV<String, String>>write() //I'm not sure how to call this method
.optimizedWrites()
.withSchema(tableSchema)
.withCreateDisposition(BigQueryIO.Write.CreateDisposition.CREATE_IF_NEEDED)
.withWriteDisposition(BigQueryIO.Write.WriteDisposition.WRITE_APPEND)
.withSchemaUpdateOptions(ImmutableSet.of(BigQueryIO.Write.SchemaUpdateOption.ALLOW_FIELD_ADDITION))
.withMethod(FILE_LOADS)
.to(new TableReference()
.setProjectId("prod-analytics-264419")
.setDatasetId("publsher")
.setTableId("beam_load_test"))
);
你想要這樣的東西:
[..]
pipeline.apply(BigQueryIO.writeTableRows()
.to(String.format("%s.dataset.table", options.getProject()))
.withCreateDisposition(CREATE_IF_NEEDED)
.withWriteDisposition(WRITE_APPEND)
.withMethod(BigQueryIO.Write.Method.STREAMING_INSERTS)
.withSchema(getTableSchema()));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.