【发布时间】:2017-11-01 06:48:48
【问题描述】:
我从文档和this answer 中都读到,可以动态确定表目标。我使用了与以下完全相同的方法:
PCollection<Foo> foos = ...;
foos.apply(BigQueryIO.write().to(new SerializableFunction<ValueInSingleWindow<Foo>, TableDestination>() {
@Override
public TableDestination apply(ValueInSingleWindow<Foo> value) {
Foo foo = value.getValue();
// Also available: value.getWindow(), getTimestamp(), getPane()
String tableSpec = ...;
String tableDescription = ...;
return new TableDestination(tableSpec, tableDescription);
}
}).withFormatFunction(new SerializableFunction<Foo, TableRow>() {
@Override
public TableRow apply(Foo foo) {
return ...;
}
}).withSchema(...));
但是,我得到以下编译错误:
The method to(String) in the type BigQueryIO.Write<Object> is not applicable for the arguments (new SerializableFunction<ValueInSingleWindow<Foo>,TableDestination>(){})
任何帮助将不胜感激。
编辑以澄清我如何在我的案例中使用窗口:
PCollection<Foo> validFoos = ...;
PCollection<TableRow> validRows = validFoos.apply(ParDo.named("Convert Foo to table row")
.of(new ConvertToValidTableRowFn()))
.setCoder(TableRowJsonCoder.of());
TableSchema validSchema = ConvertToValidTableRowFn.getSchema();
validRows.apply(Window.<TableRow>into(CalendarWindows.days(1))).apply(BigQueryIO.writeTableRows()
.to(new SerializableFunction<ValueInSingleWindow<TableRow>, TableDestination>() {
@Override
public TableDestination apply(ValueInSingleWindow<TableRow> value) {
TableRow t = value.getValue();
String fooName = ""; // get name from table
TableDestination td = new TableDestination(
"my-project:dataset.table$" + fooName, "");
return td;
}
}));
在这种情况下,我收到以下错误The method apply(PTransform<? super PCollection<TableRow>,OutputT>) in the type PCollection<TableRow> is not applicable for the arguments (Window<TableRow>)。
【问题讨论】:
-
您使用的是什么版本的 SDK?来自其他帖子 - “此功能将包含在 Apache Beam 的第一个稳定版本和 Dataflow SDK 的下一个版本中(它将基于 Apache Beam 的第一个稳定版本)。现在你可以使用这个通过对来自 github 的 HEAD 的 Beam 快照运行管道。”
-
我使用的是 5 月发布的 Apache Beam 2.0.0 稳定版。在其文档中据说包含此功能。请参阅this documentation 中的分片部分。
-
我刚刚遇到this fresh blogpost 处理这个问题。它在语法上有一些差异(返回
TableReferences 而不是TableDestinations)并将代码分成一个类(使其更简洁)。我自己没有测试过(过去我使用过与你类似的代码),但我希望这会有所帮助。 -
我自己用你的代码很快就试过了,它似乎没有给我任何错误。你能检查你的 POM 并确保你的 BEAM 版本是 2.1.0-SNAPSHOT 吗?
标签: google-bigquery google-cloud-dataflow apache-beam apache-beam-io