【问题标题】:How can I transfer data from Google CloudPlatform NodeJS app to BigQuery?如何将数据从 Google Cloud Platform Node JS 应用程序传输到 BigQuery?
【发布时间】:2021-09-15 16:55:19
【问题描述】:

我目前在 GoogleCloud Platform 服务器上设置了一个 NodeJS“服务器”,我正在监听和接收 POST HTTP 调用。 每个调用的正文都包含一个 JSON,我想对其进行解析并传输到 Google BigQuery。

现在,这是我为接收 POST 请求而创建的代码:

"use strict";

const express = require("express");
const messages = [];

async function startServer() {

  const app = express();
  app.use(express.json());

  app.post("/receive2", (req, res) => {
    res.status(200).json({ message: req.body });
    //Here I should manager the json contained in req.body
  });

  const PORT = process.env.PORT || 8080;
  logger.info({ PORT }, "bonjour");

  app.listen(PORT, () => {
    console.log(`Server listening on port ${PORT}...`);
  });

}

startServer();

JSON 类似于:https://gist.github.com/philipgiuliani/b63b22309d04048f0827

是的,我正在尝试保存在我的 iOS 应用中进行的应用内购买的收据。

谁能提供一种将我收到的收据数据传输到 BigQuery 表的方法?

【问题讨论】:

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


    【解决方案1】:

    您可以使用 NodeJS 将 csv 数据附加到现有表或创建新表。我建议使用 Google Cloud Storage 作为存储 csv 的中间位置,并将 bigquery 指向它应该加载的 csv 文件。但是,您也可以流式传输数据。

    流:

    let _count = await _BigQuery_
                        .dataset('dataset_name')
                        .table('table_name')
                        .query(`SELECT count(*) from table_name`)
                        .catch(e => console.error(e))
    if (_count[0][0].count > 0)
       console.log('no data')
    

    谷歌云存储 流:

    const gcs_file = Storage.file(gcs_path)
    const gcs_write_stream = gcs_file.createWriteStream()
    stream_from_csv_file
       .on('error', (error) => {
            gcs_write_stream.close()
       })
       .on('data', data => {
            gcs_write_stream.write(row + '\r\n')
       })
       .on('end', function () {
            gcs_write_stream.end();
            loadFileToBigquery();
       });
    
    async function loadFIleToBigQuery(){
       
       await _BigQuery_
              .etl
              .table('current_geoms_raw')
              .load(gcs_file , {
                      sourceFormat: 'CSV',
                      skipLeadingRows: 1, // if headers are in csv
                      autodetect: false, // sometimes fails if true
                      location: 'US',
              })
              .catch(e => console.error(e))
    }
    

    【讨论】:

      猜你喜欢
      • 2020-08-03
      • 1970-01-01
      • 2019-09-13
      • 1970-01-01
      • 2014-09-18
      • 2016-12-26
      • 1970-01-01
      • 2020-09-03
      • 2020-11-29
      相关资源
      最近更新 更多