【问题标题】:How to create a partitioned table in BigQuery from java?如何从 java 在 BigQuery 中创建分区表?
【发布时间】:2019-12-30 12:11:17
【问题描述】:

我想在 Java 的 BigQuery 中创建一个分区表(按 DATE 类型的字段分区)。我搜索了很多,但没有太多关于此的信息。我使用的代码是

        TimePartitioning timePartitioning = TimePartitioning.of(TimePartitioning.Type.DAY);
        timePartitioning.toBuilder().setField("col3");
        TableDefinition tableDefinition = StandardTableDefinition.newBuilder().setSchema(schema2).setTimePartitioning(timePartitioning).build();
        TableInfo tableInfo = TableInfo.newBuilder(tableId, tableDefinition).build();
        bigquery.create(tableInfo);

在这里,我有几个问题

  1. 即使我们想按日期分区,是否也应该使用 TimePartitioning?
  2. 我无法在 BigQuery UI 中的“字段分区”附近看到列名。我使用this 作为参考。我不得不使用 TimePartitioning 类而不是 TimePartitioningBuilder,因为 setTimePartitioning() 只接受 TimePartitioning。

【问题讨论】:

    标签: java google-bigquery


    【解决方案1】:

    最简单的方法是发出标准查询 - 如果您可以从 Java 查询(您已经这样做了?),只需发送这样的查询:

    #standardSQL
    CREATE TABLE `project.dataset.table`
    (
       x INT64 OPTIONS(description="An optional INTEGER field"),
       y STRUCT<
         a ARRAY<STRING> OPTIONS(description="A repeated STRING field"),
         b BOOL
       >, 
       date_column DATE
    )
    PARTITION BY date_column
    CLUSTER BY i_recommend_you_to_choose_a_clustering_column
    

    【讨论】:

      【解决方案2】:

      我还没有尝试过,但我会使用 this 表创建示例替换 StandardTableDefinition 的单行代码

      TableDefinition tableDefinition = StandardTableDefinition.of(schema);
      

      代码取自here。您可以借用对您有意义的 StandardTableDefinition 创建/配置选项,然后将单行替换为 TimePartitioning

      TimePartitioning TIME_PARTITIONING = TimePartitioning.of(TimePartitioning.Type.DAY, 42);
      

      代码取自there,例如

      TimePartitioning TIME_PARTITIONING =
            TimePartitioning.newBuilder(TYPE)
                .setExpirationMs(EXPIRATION_MS)
                .setRequirePartitionFilter(REQUIRE_PARTITION_FILTER)
                .setField(FIELD)
                .build();
      

      仅当您希望禁止不利用分区的查询时才使用.setRequirePartitionFilter(...)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-10-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-14
        • 2018-02-27
        • 2020-06-01
        相关资源
        最近更新 更多