-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
48 lines (40 loc) · 1.21 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'use strict';
// [START functions_load_file_setup]
const Storage = require('@google-cloud/storage');
const BigQuery = require('@google-cloud/bigquery');
// Instantiates a client
const storage = Storage();
const bigquery = new BigQuery();
// [END functions_load_file_setup]
// [START functions_load_file_setup]
/**
* Creates a BigQuery load job to load a file from Cloud Storage and write the data into BigQuery.
*
* @param {object} event The Cloud Functions event.
* @param {object} event.data A Google Cloud Storage File object.
* @param {function} callback The callback function.
*/
exports.loadFile = (event, callback) => {
const file = event.data;
if (!file.name.endsWith('.parquet')) {
console.log('file is not parquet, exit');
callback();
return;
}
const datasetId = 'finance';
const tableId = 'transactions';
const jobMetadata = {
format: 'PARQUET',
writeDisposition: 'WRITE_APPEND'
};
// Loads data from a Google Cloud Storage file into the table
bigquery
.dataset(datasetId)
.table(tableId)
.load(storage.bucket(file.bucket).file(file.name), jobMetadata)
.catch(err => {
console.error('ERROR:', err);
});
callback();
};
// [END functions_load_file_setup]