【问题标题】:Sharding BigQuery output tables对 BigQuery 输出表进行分片
【发布时间】: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&lt;? super PCollection&lt;TableRow&gt;,OutputT&gt;) in the type PCollection&lt;TableRow&gt; is not applicable for the arguments (Window&lt;TableRow&gt;)

【问题讨论】:

  • 您使用的是什么版本的 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


【解决方案1】:

我相信编译错误来自于您在PCollection&lt;Foo&gt; 上执行此操作,而实际上它需要窗口值。 所以你应该先使用.apply(Window.&lt;Foo&gt;into(...)),然后根据你的window确定table的目的地。

您可以在this answerthis answer 以及您提到的documentation 中查看示例。

【讨论】:

  • 感谢您的回复,但是当我应用this answer 中完全描述的窗口时,这次我得到The method apply(PTransform&lt;? super PCollection&lt;TableRow&gt;,OutputT&gt;) in the type PCollection&lt;TableRow&gt; is not applicable for the arguments (Window&lt;TableRow&gt;)
  • 您应该将代码更改为(Window.&lt;Foo&gt;into(..)),因为在您的情况下,您有一个PCollection&lt;Foo&gt;,并且您使用更通用的write() 方法,而答案代码使用PCollection&lt;TableRow&gt; 和@ 987654333@方法。
  • 是的,但在这种情况下,我在使用此功能之前将我的PCollection&lt;Foo&gt; foos 转换为PCollection&lt;TableRow&gt; fooRows
  • 可以尝试来自this doc 的sn-p,即:PCollection&lt;TableRow&gt; quotes = ... quotes.apply(Window.&lt;TableRow&gt;into(CalendarWindows.days(1))) .apply(BigQueryIO.writeTableRows() .withSchema(schema) .to(new SerializableFunction&lt;ValueInSingleWindow, String&gt;() { public String apply(ValueInSingleWindow value) { ... } })); 代码没有为您准确编译的部分?
  • 我也这样做了,但我在第一个 apply 上遇到了同样的错误。它抱怨为The method apply(PTransform&lt;? super PCollection&lt;TableRow&gt;,OutputT&gt;) in the type PCollection&lt;TableRow&gt; is not applicable for the arguments (Window&lt;TableRow&gt;)
猜你喜欢
  • 1970-01-01
  • 2021-11-26
  • 1970-01-01
  • 2016-07-07
  • 1970-01-01
  • 1970-01-01
  • 2015-08-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多