我打算将一组表从一个数据集复制到同一项目中的另一个数据集。我在 Ipython 笔记本中执行代码。
我使用以下代码获取要复制到变量“value”中的表名称列表:
list = bq.DataSet('test:TestDataset')
for x in list.tables():
if(re.match('table1(.*)',x.name.table_id)):
value = 'test:TestDataset.'+ x.name.table_id
然后我尝试使用“bq cp”命令将表从一个数据集复制到另一个数据集。但我无法在笔记本中执行 bq 命令。
!bq cp $value proj1:test1.table1_20162020
注意:
我尝试使用bigquery命令检查是否有与其关联的复制命令,但找不到任何命令。
最佳答案
我创建了以下脚本,通过几次验证将所有表从一个数据集复制到另一个数据集。
from google.cloud import bigquery
client = bigquery.Client()
projectFrom = 'source_project_id'
datasetFrom = 'source_dataset'
projectTo = 'destination_project_id'
datasetTo = 'destination_dataset'
# Creating dataset reference from google bigquery cient
dataset_from = client.dataset(dataset_id=datasetFrom, project=projectFrom)
dataset_to = client.dataset(dataset_id=datasetTo, project=projectTo)
for source_table_ref in client.list_dataset_tables(dataset=dataset_from):
# Destination table reference
destination_table_ref = dataset_to.table(source_table_ref.table_id)
job = client.copy_table(
source_table_ref,
destination_table_ref)
job.result()
assert job.state == 'DONE'
dest_table = client.get_table(destination_table_ref)
source_table = client.get_table(source_table_ref)
assert dest_table.num_rows > 0 # validation 1
assert dest_table.num_rows == source_table.num_rows # validation 2
print ("Source - table: {} row count {}".format(source_table.table_id,source_table.num_rows ))
print ("Destination - table: {} row count {}".format(dest_table.table_id, dest_table.num_rows))
关于在谷歌大查询中将表从一个数据集复制到另一个数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38729036/