0

我想创建一个每天凌晨 2 点运行的工作。此作业必须通过从 Cloud Storage 存储分区读取我的文件来创建 BigQuery 表。我怎样才能做到这一点?

4

1 回答 1

2

您可以将 Firestore 备份直接导入 BigQuery。设置sourceFormat 等于(即使对于 firestore 也是如此)和 writeDisposition的加载作业DATASTORE_BACKUPWRITE_TRUNCATE

您可以将其包装到云函数中。您可以直接使用 API 或客户端库。如果您需要代码示例,请给我您的语言,我会看看我能为您做些什么。

编辑

您需要在 package.json 中导入这些依赖项

    "@google-cloud/bigquery": "^4.7.0",
    "@google-cloud/storage": "^5.0.1",

然后,这里是具有静态值的函数。如果需要,您可以构建更动态的东西(例如通过阅读函数参数)。

const {Storage} = require('@google-cloud/storage');
const {BigQuery} = require('@google-cloud/bigquery');

const bigquery = new BigQuery();
const storage = new Storage();
//
const bucketName = "my_bucket" //to change
const fileExport = "path/to/my_export.export_metadata" //to change
const datasetId = "data" //to change
const tableId = "dsexport" //to change
exports.loadDSExport = async (req, res) => {

    // Configure the load job. For full list of options, see:
    // https://cloud.google.com/bigquery/docs/reference/rest/v2/Job#JobConfigurationLoad
    const metadata = {
        sourceFormat: 'DATASTORE_BACKUP',
        autodetect: true,
        location: 'EU', // Set your correct region
        writeDisposition: "WRITE_TRUNCATE",
    };

    // Load data from a Google Cloud Storage file into the table
    const [job] = await bigquery
        .dataset(datasetId)
        .table(tableId)
        .load(storage.bucket(bucketName).file(fileExport), metadata);
    // load() waits for the job to finish
    // Can take time, increase function timeout if needed

    // Check the job's status for errors
    const errors = job.status.errors;
    if (errors && errors.length > 0) {
        //Handle error and return code here
        throw errors;
    }

    console.log(`Job ${job.id} completed.`);
    res.send(`Job ${job.id} completed.`);
};

然后,像这样部署您的功能(此处为私有模式)

gcloud beta functions deploy --runtime nodejs10 --trigger-http --entry-point loadDSExport --region europe-west1 loadDSExport
于 2020-05-21T12:08:44.110 回答