使用 fs.createWriteStream 将 JSON 数据写入 bigquery table 时未指定作业架构或 table 错误

No schema specified on job or table error when using fs.createWriteStream to write JSON data to bigquery table

代码基本上是 bigquery API 文档的精确副本:

const { BigQuery } = require("@google-cloud/bigquery");
const bigquery = new BigQuery();
const dataset = bigquery.dataset("firebase_test_data");
const table = dataset.table("flattened_data");
const fs = require("fs");

fs.createReadStream("./data.json")
  .pipe(table.createWriteStream("json"))
  .on("job", (job) => {
    // `job` is a Job object that can be used to check the status of the
    // request.
    console.log(job);
  })
  .on("complete", (job) => {
    // The job has completed successfully.
  });

抛出的错误如下:作业中未指定架构或 table。

不知道为什么会这样,因为它几乎是文档代码的精确副本!我也尝试过使用不同的格式 fs.createWriteStream({sourceFormat: "json"}) - 导致同样的错误。

您收到此错误是因为 const table = dataset.table("flattened_data"); 中定义的 table 没有您在 data.json 中传递的适当架构。

我根据 Google documentation 尝试了以下代码,并在 BigQuery 中指定了我的 table 的模式,它成功地将数据加载到我的 table.

const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
const dataset = bigquery.dataset('my-dataset');
const table = dataset.table('my-table');

//-
// Load data from a JSON file.
//-
const fs = require('fs');

fs.createReadStream('/path-to-json/data.json')
 .pipe(table.createWriteStream('json'))
 .on('job', (job) => {
   // `job` is a Job object that can be used to check the status of the
   // request.
 })
 .on('complete', (job) => {
   // The job has completed successfully.
 });