Brad Davis
Brad Davis

Reputation: 1170

Use Bigquery API or bq command line tool to create new table

I am trying to come up with a programmatic way to generate a new bigquery table from a pre-existing table. I know how to do this to create a new view using the bq command line tool

bq --project_id='myProject' mk --view="SELECT * FROM [MYDATASET.database]" myProject.newTable

But that creates a view and that doesn't help me, I need to create a table for a bunch of reasons.

I would be happy to create a view and then be able to generate a new table from that view periodically, but I can't figure out how to do that without doing it manually though the bigquery web interface.

I'd appreciate any help.

Brad

Upvotes: 1

Views: 1923

Answers (1)

Jeremy Condit
Jeremy Condit

Reputation: 7046

If it's a normal table (not a view), you can use the copy command:

bq cp <source> <destination>

If you're trying to materialize a view, or if you need to modify the contents of the table in the process (e.g., adding/removing/transforming fields), you can run a query with a destination table:

bq query \
  --destination_table=<destination> \
  --allow_large_results \
  --noflatten_results \
  'SELECT ... FROM <source>'

The query option is more powerful, but you'll get charged for running the query. The copy is free.

Upvotes: 3

Related Questions