Google Bigquery:如何更新分区表的分区到期时间?

mmz*_*yad 3 google-bigquery

Bigquery文档说,它有可能更新分区表的分区时间到期。而我只能在摄取时间分区表上执行此操作。我尝试了以下方法:

  bq query --use_legacy_sql=false ' 
  CREATE TABLE IF NOT EXISTS [DATASET].partitioned_table_ingestion_time ( 
      event_date DATE NOT NULL, 
      event_id INT64) 
  PARTITION BY DATE(_PARTITIONTIME)
  OPTIONS( 
     partition_expiration_days=10, 
     description="table partitioned by ingestion time and with expiry" 
   )' 
Run Code Online (Sandbox Code Playgroud)

更新查询和结果:

  # update expiry to 7 days = 7 * 24 * 60 * 60 = 604800 s
  bq update --time_partitioning_expiration 604800 [PROJECT-ID]:[DATASET].partitioned_table_ingestion_time

  Table 'PROJECT-ID]:[DATASET].partitioned_table_ingestion_time' successfully updated.
Run Code Online (Sandbox Code Playgroud)

现在,对于分区表:

  bq query --use_legacy_sql=false ' 
  CREATE TABLE IF NOT EXISTS [DATASET].partitioned_table ( 
      event_date DATE NOT NULL, 
      event_id INT64) 
  PARTITION BY event_date
  OPTIONS( 
     partition_expiration_days=10, 
     description="table partitioned by event_date with expiry" 
   )' 
Run Code Online (Sandbox Code Playgroud)

在这种情况下更新失败

  # update expiry to 7 days
  bq update --time_partitioning_expiration 604800 [PROJECT-ID]:[DATASET].partitioned_table 

  BigQuery error in update operation: Cannot change partitioning spec for a partitioned table.
Run Code Online (Sandbox Code Playgroud)

如何在这里更新分区时间到期?

Ell*_*ard 5

尝试以下操作,在bq update命令中指定分区字段:

bq update --time_partitioning_field=event_date \
  --time_partitioning_expiration 604800 [PROJECT-ID]:[DATASET].partitioned_table
Run Code Online (Sandbox Code Playgroud)

编辑:您现在也可以使用该ALTER TABLE SET OPTIONS语句来更改分区过期。例如:

ALTER TABLE `project-name`.dataset_name.table_name
SET OPTIONS (partition_expiration_days=7);
Run Code Online (Sandbox Code Playgroud)