Skip to content

Instantly share code, notes, and snippets.

@deepak7093
Created May 5, 2020 03:37
Show Gist options
  • Save deepak7093/a413d5197981617ff8be46fca06968b2 to your computer and use it in GitHub Desktop.
Save deepak7093/a413d5197981617ff8be46fca06968b2 to your computer and use it in GitHub Desktop.
BigQuery Copy Datasets
from google.cloud import bigquery
client = bigquery.Client()
projectFrom = ''
datasetFrom = ''
projectTo = ''
datasetTo = ''
# 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)
tables = list(client.list_tables(dataset_from))
print(tables)
for source_table_ref in tables:
# 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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment