I intend to copy a set of tables from one dataset to another within the same project. I execute the code in Ipython notebook.
I get the list of table names to be copied in the variable “value” using the below code:
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
Then i tried using the “bq cp” command to copy table from one dataset to another. But I cannot execute the bq command in the notebook.
!bq cp $value proj1:test1.table1_20162020
Note:
I tried with bigquery command to check whether there is a copy command associated with it but could not find any.
You can copy dataset using BigQuery Copy Dataset (in/cross-region). The copy dataset UI is similar to copy table. Just click "copy dataset" button from the source dataset, and specify the destination dataset in the pop-up form. See screenshot below.
Open the BigQuery page in the Google Cloud console. In the Explorer panel, expand your project and dataset, then select the table. In the details panel, click Export and select Export to Cloud Storage.
Go to the BigQuery page. In the Explorer panel, expand your project and select a dataset. Expand the dataset and select a table or view. Click person_add Share.
I have created following script to copying all the tables from one dataset to another dataset with couple of validation.
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))
If you are using the BigQuery API with Python, you can run a copy job:
https://cloud.google.com/bigquery/docs/tables#copyingtable
Copying the Python example from the docs:
def copyTable(service):
try:
sourceProjectId = raw_input("What is your source project? ")
sourceDatasetId = raw_input("What is your source dataset? ")
sourceTableId = raw_input("What is your source table? ")
targetProjectId = raw_input("What is your target project? ")
targetDatasetId = raw_input("What is your target dataset? ")
targetTableId = raw_input("What is your target table? ")
jobCollection = service.jobs()
jobData = {
"projectId": sourceProjectId,
"configuration": {
"copy": {
"sourceTable": {
"projectId": sourceProjectId,
"datasetId": sourceDatasetId,
"tableId": sourceTableId,
},
"destinationTable": {
"projectId": targetProjectId,
"datasetId": targetDatasetId,
"tableId": targetTableId,
},
"createDisposition": "CREATE_IF_NEEDED",
"writeDisposition": "WRITE_TRUNCATE"
}
}
}
insertResponse = jobCollection.insert(projectId=targetProjectId, body=jobData).execute()
# Ping for status until it is done, with a short pause between calls.
import time
while True:
status = jobCollection.get(projectId=targetProjectId,
jobId=insertResponse['jobReference']['jobId']).execute()
if 'DONE' == status['status']['state']:
break
print 'Waiting for the import to complete...'
time.sleep(10)
if 'errors' in status['status']:
print 'Error loading table: ', pprint.pprint(status)
return
print 'Loaded the table:' , pprint.pprint(status)#!!!!!!!!!!
# Now query and print out the generated results table.
queryTableData(service, targetProjectId, targetDatasetId, targetTableId)
except HttpError as err:
print 'Error in loadTable: ', pprint.pprint(err.resp)
The bq cp
command does basically the same, internally (you could call that function too, depending on what bq
you are importing).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With