Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy table structure alone in Bigquery

In Google's Big query, is there a way to clone (copy the structure alone) a table without data?

bq cp doesn't seem to have an option to copy structure without data. And Create table as Select (CTAS) with filter such as "1=2" does create the table without data. But, it doesn't copy the partitioning/clustering properties.

like image 900
Thasleem Basha Avatar asked Jan 05 '19 16:01

Thasleem Basha


People also ask

How do I copy a table structure in BigQuery?

In the BigQuery UI, select the table you wish to copy, then push the Copy Table button. Enter the desired new table name. BigQuery documentation lists additional methods for copying a table (via API, with Python, PHP, etc).

How do I copy a table from one project to another in BigQuery?

In the Google Cloud console, go to the BigQuery page. Click Data transfers. Select a transfer for which you want to view the transfer details. On the Transfer details page, select a transfer run.

How do I copy a table from one region to another in BigQuery?

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.


2 Answers

You can use DDL and limit 0, but you need to express partitioning and clustering in the query as well

#standardSQL
 CREATE TABLE mydataset.myclusteredtable
 PARTITION BY DATE(timestamp)
 CLUSTER BY
   customer_id
 AS SELECT * FROM mydataset.myothertable LIMIT 0
like image 68
Pentium10 Avatar answered Oct 12 '22 23:10

Pentium10


If you want to clone structure of table along with partitioning/clustering properties w/o having need in knowing what exactly those partitioning/clustering properties - follow below steps:

Step 1: just copy your_table to new table - let's say your_table_copy. This will obviously copy whole table including all properties (including such like descriptions, partition's expiration etc. - which is very simple to miss if you will try to set them manually) and data. Note: copy is cost free operation

Step 2: To get rid of data in newly created table - run below query statement

SELECT * FROM `project.dataset.your_table_copy` LIMIT 0    

while running above make sure you set project.dataset.your_table_copy as destination table with 'Overwrite Table' as 'Write Preference'. Note: this is also cost free step (because of LIMIT 0)

You can easily do both above steps from within Web UI or Command Line or API or any client of your choice - whatever you are most comfortable with

like image 33
Mikhail Berlyant Avatar answered Oct 12 '22 23:10

Mikhail Berlyant