【问题标题】:Joining CSV files and Remove Duplicates with Google Functions使用 Google Functions 加入 CSV 文件并删除重复项
【发布时间】:2018-02-09 11:35:43
【问题描述】:

我是 Google 平台的新手,不了解如何使用外部库创建 Google 函数。

我想将 CSV 文件上传到 Cloud Storage,然后触发 Google Cloud Function 以将其与 Google Cloud Storage 中的另一个 CSV 文件联接,并将 JOIN 结果导出到新的 csv 文件并删除重复项。

我已经看到用于 npm 的库“csv-join”和“csv-reorder”,但我不确定如何将它与 Cloud Functions 一起使用,以及是否可能,因为我被困在这一点上。

感谢您的建议。

问候。

这是我的代码:

exports.Test_BQ = (event, callback) => {
  const file = event.data;

  if (file.resourceState === 'not_exists') {
    console.log(`File ${file.name} deleted.`);
  } else if (file.metageneration === '1') {


const reorder = require('csv-reorder');

reorder({
  input: 'https://storage.googleapis.com/staging.XXXXX.appspot.com/test.csv',
  output: 'https://storage.googleapis.com/staging.XXXXX.appspot.com/test_2.csv',
  sort: 'policyID',
  type: 'string',
  descending: false,
  remove: true,
  metadata: false
});

  }

  callback();
    };

我收到此错误:

TypeError: promisify 不是 Object 的函数。 (/user_code/node_modules/csv-reorder/lib/read.js:6:18) 在 Module._compile (module.js:570:32) 在 Object.Module._extensions..js (module.js:579:10 ) 在 Module.load (module.js:487:32) 在 tryModuleLoad (module.js:446:12) 在 Function.Module._load (module.js:438:3) 在 Module.require (module.js:497 :17) 在 Object 的 require (internal/module.js:20:19) 处。 (/user_code/node_modules/csv-reorder/index.js:4:14) 在 Module._compile (module.js:570:32)

现在我只测试“csv-reorder”来了解这个外部库是如何工作的。我在本地得到了结果,但在云端没有。

【问题讨论】:

    标签: node.js csv google-cloud-platform google-cloud-storage


    【解决方案1】:

    csv-reorder 库使用 util.promisify,它是 Node 8.X 版本的一部分,而 Google Cloud Functions 当前运行 Node.js v6.11.5。您可以通过以下方式为旧版本提供 polyfill:

    npm install --save util.promisify 
    

    然后您可以像这样修补 util 库:

    const util = require('util');
    require('util.promisify').shim();
    

    这应该可以解决您面临的错误。但是,在仔细查看 csv-reorder code 之后,它似乎只能从文件系统读取文件。 Google Cloud Functions 没有为您提供要写入的文件系统,因此您需要找到另一种方法。

    【讨论】:

    • 您好,您是对的,因为它只能访问本地系统,最后我们使用 Google Functions 更改了策略。
    猜你喜欢
    • 2016-03-29
    • 2023-03-27
    • 1970-01-01
    • 2012-06-13
    • 1970-01-01
    • 2012-12-16
    • 1970-01-01
    • 2020-05-18
    • 1970-01-01
    相关资源
    最近更新 更多