【问题标题】:Autodetect BigQuery schema within Dataflow?在 Dataflow 中自动检测 BigQuery 架构?
【发布时间】:2017-09-27 04:18:52
【问题描述】:

是否可以在 DataFlow 中使用 --autodetect 的等效项?

即我们能否在不指定架构的情况下将数据加载到 BQ 表中,相当于我们可以使用 --autodetect 从 CSV 加载数据?

(potentially related question)

【问题讨论】:

  • 请澄清您的具体问题或添加其他详细信息以准确突出您的需要。正如目前所写的那样,很难准确地说出你在问什么。请参阅How to Ask 页面以获得澄清此问题的帮助。
  • 我们可以在不指定架构的情况下写入 BQ 吗?

标签: google-bigquery google-cloud-dataflow apache-beam


【解决方案1】:

我不确定是否要从 BQ 读取,但对于写入,我认为这样的东西可以在最新的 java SDK 上运行。

.apply("WriteBigQuery", BigQueryIO.Write
    .withCreateDisposition(BigQueryIO.Write.CreateDisposition.CREATE_NEVER)
    .to(outputTableName));


Note: BigQuery Table must be of the form: <project_name>:<dataset_name>.<table_name>.

【讨论】:

  • 但这只有在表已经存在的情况下才有效 - 即BigQueryDisposition.CREATE_NEVER: fail the write if does not exist.
  • 是的,在这种情况下,我们需要一个存在的表,因为我们试图检测现有表的模式。如果目标/问题是定义一个方案并基于 java 数据类型创建一个表,那么我认为我们不支持。
  • 它期望什么输入?对象?
【解决方案2】:

如果您在 PCollections 中使用 protocol buffers 作为对象(应该在 Dataflow 后端表现良好),您也许可以使用我过去编写的实用程序。它会在运行时根据对 protobuffer 描述符的检查将 protobuffer 的架构解析为 BigQuery 架构。

我很快将它上传到GitHub,它是 WIP,但你也许可以使用它或受到启发,使用 Java 反射编写类似的东西(我可能会在某个时候自己做)。

您可以按如下方式使用该工具:

TableSchema schema = ProtobufUtils.makeTableSchema(ProtobufClass.getDescriptor());
enhanced_events.apply(BigQueryIO.Write.to(tableToWrite).withSchema(schema)
            .withCreateDisposition(BigQueryIO.Write.CreateDisposition.CREATE_IF_NEEDED)
            .withWriteDisposition(BigQueryIO.Write.WriteDisposition.WRITE_TRUNCATE));

create disposition 将使用指定的架构创建表,而 ProtobufClass 是使用您的 Protobuf 架构和 proto 编译器生成的类。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-06
    • 2018-07-22
    • 2020-04-17
    • 2021-08-10
    相关资源
    最近更新 更多