您可以将 Firestore 备份直接导入 BigQuery。设置一个load job,sourceFormat 等于DATASTORE_BACKUP(即使对于firestore也是如此),writeDisposition 为WRITE_TRUNCATE
您可以将其包装到云函数中。您可以直接使用 API 或client libraries。如果您需要代码示例,请将您的语言告诉我,我会看看能为您做什么。
编辑
你需要在你的 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