将非分区流式表迁移到分区表Bigquery

时间:2018-04-10 13:00:47

标签: google-bigquery

我有一个遗留的未分区的大型查询表,可以从各种来源流式传输日志(比如表BigOldA)。目的是将其转移到新的day分区表(假设为PartByDay),这是通过以下链接完成的:

https://cloud.google.com/bigquery/docs/creating-column-partitions#creating_a_partitioned_table_from_a_query_result

bq query 
--allow_large_results 
--replace=true 
--destination_table <project>:<data-set>.<PartByDay>
--time_partitioning_field REQUEST_DATETIME 
--use_legacy_sql=false 'SELECT * FROM `<project>.<data-set>.<BigOldA>`'

我已将历史数据迁移到新表但我无法在表BigOldA中将其删除,因为我在运行same problem时遇到了在流缓冲表上运行DML的问题。

Error: UPDATE or DELETE DML statements are not supported over 
table <project>:<data-set>.BigOldA with streaming buffer

我计划每天运行批处理作业,将T-1数据从表BigOldA传输到表PartByDay并定期删除它们,以便我仍然可以在表{{1}中维护流缓冲区数据并开始使用BigOldA表进行分析。现在我不确定它是否可以实现。

我正在寻找关于如何定期转移和替代的替代解决方案或最佳实践。将流缓冲表维护到分区表。此外,由于数据是从独立生产源流式传输的,因此无法将所有源流式传输到PartByDay的{​​{1}}和PartByDay属性永远不会为空。

2 个答案:

答案 0 :(得分:1)

您可以删除原始表格,然后在运行历史记录作业后将已迁移的表格重命名为原始名称。这假设您的BigQuery流组件是容错的。如果设计得好,您就不应该丢失任何数据。无论流向BigQuery的流量应该能够存储事件,直到表重新联机。一旦表被分区,它就不应该为流组件改变任何东西。

答案 1 :(得分:0)

如果对脚本感兴趣的人,就到这里。

#!/bin/sh
# This script
# 1. copies the data as the partitioned table
# 2. delete the unpartitioned table
# 3. copy the partitioned table to the same dataset table name
# TODO 4. deletes the copied table

set -e
source_project="<source-project>"
source_dataset="<source-dataset>"
source_table="<source-table-to-partition>"

destination_project="<destination-project>"
destination_dataset="<destination-dataset>"
partition_field="<timestamp-partition-field>"
destination_table="<table-copy-partition>"

source_path="$source_project.$source_dataset.$source_table"
source_l_path="$source_project:$source_dataset.$source_table"
destination_path="$destination_project:$destination_dataset.$destination_table"

echo "copying table from $source_path to $destination_path"
query=$(cat <<-END
SELECT * FROM \`$source_path\`
END
)

echo "deleting old table"
bq rm -f -t $destination_path
echo "running the query: $query"

bq query --quiet=true --use_legacy_sql=false --apilog=stderr --allow_large_results --replace=true --destination_table $destination_path --time_partitioning_field $partition_field "$query"

echo "removing the original table: $source_path"
bq rm -f -t $source_l_path
echo "table deleted"
echo "copying the partition table to the original source path"
bq cp -f -n $destination_path $source_l_path
相关问题