【问题标题】:No schema specified on job or table error when using fs.createWriteStream to write JSON data to bigquery table使用 fs.createWriteStream 将 JSON 数据写入 bigquery 表时,作业或表错误未指定架构
【发布时间】:2021-12-13 13:55:59
【问题描述】:

代码基本上是 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.
  });

引发的错误如下:作业或表上未指定架构。

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

【问题讨论】:

  • 您的 data.json 文件是否包含表中存在的所有列?
  • 表完全是空的,我在尝试之前刚刚创建了它。我应该先将列添加到表中吗:O?
  • 对不起,我想我误解了你在这里想要做什么。您是否尝试从 ./data.json 文件中插入数据?
  • 如果您觉得我的回答对您的问题有帮助,请考虑接受并按Stack Overflow guidelines 点赞,帮助更多 Stack 贡献者进行研究。

标签: node.js google-cloud-platform google-bigquery fs


【解决方案1】:

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

我根据Google documentation 尝试了以下代码,并在 BigQuery 中指定了我的表的架构,它成功地将数据加载到我的表中。

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.
 });

【讨论】:

    猜你喜欢
    • 2023-01-23
    • 2020-03-06
    • 1970-01-01
    • 2020-12-02
    • 2020-03-22
    • 2018-11-02
    • 1970-01-01
    • 2021-10-06
    • 2021-12-28
    相关资源
    最近更新 更多