在此参考https://developers.google.com/apps-script/advanced/bigquery中,
为了将 CSV 数据加载到 BigQuery,他们使用:
var file = DriveApp.getFileById(csvFileId);
var data = file.getBlob().setContentType('application/octet-stream');
// Create the data upload job.
var job = {
configuration: {
load: {
destinationTable: {
projectId: projectId,
datasetId: datasetId,
tableId: tableId
},
skipLeadingRows: 1
}
}
};
job = BigQuery.Jobs.insert(job, projectId, data);
据我了解,他们向 BigQuery 发送了一个file.getBlob().setContentType('application/octet-stream');
不友好的 blob
如何在 Apps 脚本中将 JSON 发送到 BigQuery?
使用库@google-cloud/bigquery
(在 Apps 脚本之外的项目中使用),我可以执行以下操作:
https://cloud.google.com/bigquery/streaming-data-into-bigquery#streaminginsertexamples
// Import the Google Cloud client library
const { BigQuery } = require('@google-cloud/bigquery')
const moment = require('moment')
exports.insertUsageLog = async (userId) => {
const datasetId = 'usage'
const tableId = 'logs'
const rows = [
// The JSON data is collected here
{
timestamp: moment.utc().toISOString(),
userId,
// Something else ...
},
]
// Create a client
const bigqueryClient = new BigQuery()
// Insert data into a table
await bigqueryClient
.dataset(datasetId)
.table(tableId)
.insert(rows)
console.log(`Inserted ${rows.length} rows`)
}