Skip to content

Instantly share code, notes, and snippets.

@mr-pascal
Last active March 21, 2021 07:00
Show Gist options
  • Select an option

  • Save mr-pascal/fc07e856ecfae69cc84992d9bd9fab4d to your computer and use it in GitHub Desktop.

Select an option

Save mr-pascal/fc07e856ecfae69cc84992d9bd9fab4d to your computer and use it in GitHub Desktop.
const importCSV = async (datasetId, tableId, pathToLocalFile, timePartitioning) => {
const metadata = {
sourceFormat: 'CSV',
// Ignore first row since it contains the column headers
skipLeadingRows: 1,
schema: {
fields: [
{ name: 'date', type: 'DATETIME', mode: 'REQUIRED' },
{ name: 'name', type: 'STRING', mode: 'REQUIRED' },
{ name: 'event', type: 'INT64', mode: 'REQUIRED' },
],
},
location: 'US',
timePartitioning,
};
// Create table and load CSV data into it
const [job] = await bigquery
.dataset(datasetId)
.table(tableId)
.load(pathToLocalFile, metadata);
console.log(`Job ${job.id} completed.`);
// Check the job's status for errors
const errors = job.status.errors;
if (errors && errors.length > 0) {
throw errors;
}
}
// TODO: Developer, make sure to add your dataset ID here!
const datasetId = 'my_dataset';
// TODO: Developer, make sure to add the path to your CSV file here!
const pathToLocalFile = './dump.csv';
// Create new partitioned table and fill it
// with the data from the CSV
importCSV(
datasetId,
'partitioned_table',
pathToLocalFile,
{
type: 'DAY',
field: 'date',
}
);
// Create new table and fill it
// with the data from the CSV
importCSV(
datasetId,
'non_partitioned_table',
pathToLocalFile
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment