There are a number of ways to get data into BigQuery but one of the most convenient as far as Apps Script is concerned is to write it straight there from data generated during some Apps Script project.
There are limits on Drive file size that's it's hard to get past - so you sometimes can't just dump it out and load it using the bigquery console tools, and also BigQuery takes something called delimited JSON as input - so you do have to do a little tweaking.
Apps Script does have a BigQuery advanced service, but it also has a 10mb limit on data that can be loaded. However it's easy to load data in multiple stages. Here's how.
You have to enable billing to use BigQuery, so use a developer console project that has it enabled. Just select the project in the big query console at https://bigquery.cloud.google.com
Add a dataset and pick an Id. In this example, I've created a dataset called jeopardydata. It's going to hold more or less all the jeopardy questions ever asked . I came across it here on Reddit.
Within datasets. you can have tables,and you'll be tempted to create one here. However, I'll create a table when I load the data so we're done in the bigquery console for now.
Creating a table will need a table reference object - it looks like this. By default, a load operation will create the table if it doesn't already exist.
You'll need to enable the advanced service for BigQuery, and also enable it in the target project.
My source data is a 53mb Drive file, and is a JSON array of objects that looks like this.
BigQuery needs JSON delimited data, so I'll use Apps Script to transform and load the data directly.
I'm confident that the objects in each array element are consistent, so I'll just use the first row to create the fields for the schema
Since my data is larger than 10mb, I have to load it in chunks, whilst at the same time transforming it from an array of objects, into new line delimited JSON.
Here's how - I've actually limited my chunk size to 8mb in this example.
Once kicked off, you can find progress of your jobs in the BigQuery console. It looks like this when complete.
Each load looks like this
And here's a preview of the data in BigQuery
The entire code is below. The file shown is public so you can use it if you like. You'll need to change the project id to one of your own. It takes quite a while so you may need to modify it to do it a section at a time.
For more like this, see Google Apps Scripts snippets. Why not join our forum, follow the blog or follow me on twitter to ensure you get updates when they are available.
You want to learn Google Apps Script?
Learning Apps Script, (and transitioning from VBA) are covered comprehensively in my my book, Going Gas - from VBA to Apps script, available All formats are available now from O'Reilly,Amazon and all good bookshops. You can also read a preview on O'Reilly.
Services > Desktop Liberation - the definitive resource for Google Apps Script and Microsoft Office automation > Google Apps Scripts snippets >