【问题标题】:Google cloud function bigquery json insert TypeError: job.promise is not a function谷歌云函数 bigquery json insert TypeError: job.promise is not a function
【发布时间】:2018-07-25 18:04:08
【问题描述】:

I'm replicating this Google authored tutorial 我遇到了一个我不知道如何解决的问题和错误。

在 Google Cloud Function import json to bigquery 上,我收到错误“TypeError: job.promise is not a function”

位于函数底部,有问题的代码是:

.then(([job]) => job.promise())

The error led me to this discussion about the API used,但我不明白如何解决错误。

我尝试了.then(([ job ]) => waitJobFinish(job)),删除该行解决了错误,但没有插入任何内容。

第三个问题:我也找不到有关如何触发功能测试的文档,以便我可以在谷歌云功能控制台中阅读我的 console.logs,这将有助于解决这个问题。我可以测试这个函数的 json POST 部分,但是我找不到什么 json 来触发一个新文件写入云存储的测试 - 测试说必须包含一个存储桶,但我不知道要格式化什么 json(我用来测试帖子的 json -> 存储到云存储不起作用)

这是我将其提取到它自己的函数中的完整函数:

(function () {
   'use strict';


    // Get a reference to the Cloud Storage component
    const storage = require('@google-cloud/storage')();
    // Get a reference to the BigQuery component
    const bigquery = require('@google-cloud/bigquery')();


    function getTable () {
      const dataset = bigquery.dataset("iterableToBigquery");

      return dataset.get({ autoCreate: true })
        .then(([dataset]) => dataset.table("iterableToBigquery").get({ autoCreate: true }));
    }


    //set trigger for new files to google storage bucket
    exports.iterableToBigquery = (event) => {
      const file = event.data;

      if (file.resourceState === 'not_exists') {
        // This was a deletion event, we don't want to process this
        return;
      }

      return Promise.resolve()
        .then(() => {
          if (!file.bucket) {
            throw new Error('Bucket not provided. Make sure you have a "bucket" property in your request');
          } else if (!file.name) {
            throw new Error('Filename not provided. Make sure you have a "name" property in your request');
          }

          return getTable();
        })
        .then(([table]) => {
          const fileObj = storage.bucket(file.bucket).file(file.name);
          console.log(`Starting job for ${file.name}`);
          const metadata = {
            autodetect: true,
            sourceFormat: 'NEWLINE_DELIMITED_JSON'
          };
          return table.import(fileObj, metadata);
        })
        .then(([job]) => job.promise())
        //.then(([ job ]) => waitJobFinish(job))

        .then(() => console.log(`Job complete for ${file.name}`))
        .catch((err) => {
          console.log(`Job failed for ${file.name}`);
          return Promise.reject(err);
        });
    };


}());

【问题讨论】:

    标签: javascript google-bigquery google-cloud-functions


    【解决方案1】:

    所以我无法弄清楚如何修复 google 的示例,但我能够从 js 获取此负载以使用 google cloud 函数中的以下代码:

    'use strict';
    /*jshint esversion: 6 */
    // Get a reference to the Cloud Storage component
    const storage = require('@google-cloud/storage')();
    // Get a reference to the BigQuery component
    const bigquery = require('@google-cloud/bigquery')();
    
    exports.iterableToBigquery = (event) => {
      const file = event.data;
    
      if (file.resourceState === 'not_exists') {
        // This was a deletion event, we don't want to process this
        return;
      }
    
        const importmetadata = {
            autodetect: false,
            sourceFormat: 'NEWLINE_DELIMITED_JSON'
        };
    
        let job;
    
      // Loads data from a Google Cloud Storage file into the table
      bigquery
        .dataset("analytics")
        .table("iterable")
        .import(storage.bucket(file.bucket).file(file.name),importmetadata)
        .then(results => {
          job = results[0];
          console.log(`Job ${job.id} started.`);
    
          // Wait for the job to finish
          return job;
        })
        .then(metadata => {
          // Check the job's status for errors
          const errors = metadata.status.errors;
          if (errors && errors.length > 0) {
            throw errors;
          }
        })
        .then(() => {
          console.log(`Job ${job.id} completed.`);
        })
        .catch(err => {
          console.error('ERROR:', err);
        });
    
    
    };
    

    【讨论】:

    • @Dilion Doyle .import() 上的 table() 真的适合你吗?
    • @mauricioSanchez 是的,但是 bigquery/google 对进口的限制是一个大问题。我认为如果你有巨大的 json 文件并且每隔 10 分钟才导入一次,它可能会大规模工作。我改用流式插入,只是摆脱了像这样使用 json 的概念。我从谷歌自己的云函数示例中得到了这个想法,但它无法扩展。
    • @Dillion Doyle,很有趣。我没有在文档中看到这一点,但我明白你的意思。流媒体与一个块负载
    • @mauricioSanchez sorry it is here,我猜这不是文档,而是一个示例。它与我们的用例非常吻合,这是我首先找到他们的示例的地方!
    猜你喜欢
    • 2022-10-14
    • 1970-01-01
    • 2014-12-30
    • 1970-01-01
    • 1970-01-01
    • 2015-06-24
    • 1970-01-01
    • 2018-12-10
    • 1970-01-01
    相关资源
    最近更新 更多