norgematos
norgematos

Reputation: 620

How to clone a dataset to another BigQuery project

Suppose you have a Google-BigQuery project per Client. Now a new Client is coming aboard. What is the best procedure to clone a project, a dataset or an object like a view to a new project?

I have a dataset with a set of views containing analytics logic. It would be great if I could copy those views to the new project.

Thanks

Upvotes: 1

Views: 3241

Answers (3)

Douglas Martins
Douglas Martins

Reputation: 11

Here's a solution based on Mikhail Berlyant's answer. After creating the new dataset:

from google.cloud import bigquery
from google.cloud.bigquery.table import Table


ORIGINAL_PROJECT_ID = "original-project-id"
NEW_PROJECT_ID = "new-project-id"
ORIGINAL_DATASET_ID = "original-dataset-id"
NEW_DATASET_ID = "new-dataset-id"

client = bigquery.Client(project=ORIGINAL_PROJECT_ID)

tables_list = [
    table
    for table in client.list_tables(dataset=ORIGINAL_DATASET_ID)
    if table.table_type == "VIEW"
]

tables = [client.get_table(table) for table in tables_list]

for table in tables:
    new_table = Table(f"{NEW_PROJECT_ID}.{NEW_DATASET_ID}.{table.table_id}")
    new_table.view_query = table.view_query
    client.create_table(new_table)

Upvotes: 0

Pratap Singh
Pratap Singh

Reputation: 419

We had similar requirement to transfer all or selective Views and Table definitions from One Big Query project to another per Dataset level, Hence have coded in-house to transfer all/selective tables Schemas and Views Definitions using Biq Query APIs. It is shallow cloning(no table data transferred) to destination Big Query project.

Feel free to ask for code example if you still need it. I will be happy to share my Java Code.

Upvotes: 0

Mikhail Berlyant
Mikhail Berlyant

Reputation: 172994

as an option - you can have script that

Should be pretty simple

Upvotes: 3

Related Questions