一个 api 服务器正在 Kubernetes Engine (GKE) 上运行。用户可以将相对较小的数据集(~100mb,具有相同数据结构的多个 .csv)从客户端应用程序上传到 Cloud Storage (GCS)。上传完成后,我需要将所有新 .csv 文件中的所有数据导入单个现有 BigQuery 表,其中包含一些特定于用户的参数(用用户 id 标记每一行可能是这样)。顺序无所谓。
Google 文档为此提供了基于 GUI 的解决方案和命令行解决方案。不过,我假设有一种方法可以从基于 GKE 的服务器本身触发上传并跟踪它的进度。我怎么做?
不确定这是否重要:GKE api 服务器是在 NodeJS 上编写的。
以下是将文件上传到 GCS 的示例,摘自BigQuery 文档。您可以根据需要配置作业;该页面上有一些参考资料,以及带有附加功能的 GitHub 存储库链接:
// Imports the Google Cloud client libraries
const BigQuery = require('@google-cloud/bigquery');
const Storage = require('@google-cloud/storage');
// The project ID to use, e.g. "your-project-id"
// const projectId = "your-project-id";
// The ID of the dataset of the table into which data should be imported, e.g. "my_dataset"
// const datasetId = "my_dataset";
// The ID of the table into which data should be imported, e.g. "my_table"
// const tableId = "my_table";
// The name of the Google Cloud Storage bucket where the file is located, e.g. "my-bucket"
// const bucketName = "my-bucket";
// The name of the file from which data should be imported, e.g. "file.csv"
// const filename = "file.csv";
// Instantiates clients
const bigquery = BigQuery({
projectId: projectId
});
const storage = Storage({
projectId: projectId
});
let job;
// Imports data from a Google Cloud Storage file into the table
bigquery
.dataset(datasetId)
.table(tableId)
.import(storage.bucket(bucketName).file(filename))
.then((results) => {
job = results[0];
console.log(`Job ${job.id} started.`);
// Wait for the job to finish
return job.promise();
})
.then((results) => {
// Get the job's status
return job.getMetadata();
}).then((metadata) => {
// Check the job's status for errors
const errors = metadata[0].status.errors;
if (errors && errors.length > 0) {
throw errors;
}
}).then(() => {
console.log(`Job ${job.id} completed.`);
})
.catch((err) => {
console.error('ERROR:', err);
});
上传后,您可以运行查询来查询新上传的 CSV 文件并将结果附加到所需的目标表。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句