简体   繁体   English

如何从 Java 在 BigQuery 中创建分区表?

[英]How to create a partitioned table in BigQuery from java?

I would like to create a partitioned table(partition by field which is of DATE type) in BigQuery from java.我想从 Java 在 BigQuery 中创建一个分区表(按日期类型的字段分区)。 I searched a lot but there is not much information on this.我搜索了很多,但关于这方面的信息并不多。 The code I used is我使用的代码是

        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);

Here, I have a couple of questions在这里,我有几个问题

  1. Should we use TimePartitioning even if we want to partition by date?即使我们想按日期分区,我们也应该使用 TimePartitioning 吗?
  2. I am not able to see the column name near 'Partitioned on field' in the BigQuery UI.我无法在 BigQuery 用户界面中的“在字段上分区”附近看到列名称。 I used this as reference.以此作为参考。 I had to use TimePartitioning class and not TimePartitioningBuilder because setTimePartitioning() accepts TimePartitioning only.我必须使用 TimePartitioning 类而不是 TimePartitioningBuilder 因为 setTimePartitioning() 只接受 TimePartitioning。

Easiest way would be to issue a standard query - if you can query from Java (which you already do?), just send a query like this:最简单的方法是发出标准查询 - 如果您可以从 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

I haven't tried but I'd use this table creation sample replacing the one-liner for StandardTableDefinition我还没有尝试过,但我会使用表创建示例替换StandardTableDefinition的单行

TableDefinition tableDefinition = StandardTableDefinition.of(schema);

with the code taken from here .使用从这里获取的代码。 You could borrow the StandardTableDefinition creation/configuration options that make sense for you and then replace the one-liner for TimePartitioning您可以借用对您有意义的StandardTableDefinition创建/配置选项,然后替换TimePartitioning

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

with code taken from there eg使用从那里获取的代码例如

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

Use .setRequirePartitionFilter(...) only if you would like to disallow queries that doesn't take advantage of partitioning.仅当您想禁止不利用分区的查询时才使用.setRequirePartitionFilter(...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM