【问题标题】:Node.js pg-promise Azure function to write to Postgres (timescaleDB)Node.js pg-promise Azure 函数写入 Postgres (timescaleDB)
【发布时间】:2021-01-13 13:09:44
【问题描述】:

Azure 又让我发疯了。我试图实现的是需要将通过事件中心传入的数据写入数据库。到目前为止,我所做的是数据到达事件中心,并且 Azure 函数能够将数据发布到数据库。我更喜欢用 Node.JS 来做这件事,因为在 Azure 中集成看起来不错。我用来向数据库发送一些虚假数据的脚本如下:

module.exports = async function (context, eventHubMessages){
    const initOptions = {
        query(e) {context.log(e.query)},
        capSQL: true
        //capSQL: true // capitalize all generated SQL
    };
    const pgp = require('pg-promise')(initOptions);
    const db = pgp({
                    host: '####',
                    user: '####',
                    password: '####',
                    database: 'iotdemo',
                    port: 5432,
                    ssl: true
                    });
    // our set of columns, to be created only once (statically), and then reused,
    // to let it cache up its formatting templates for high performance:
    const cs = new pgp.helpers.ColumnSet(['customer', 'tag', 'value', 'period'], {table: 'testtable'});
    // generating a multi-row insert query:
    const query = pgp.helpers.insert(JSON.parse(eventHubMessages), cs);
    //=> INSERT INTO "tmp"("col_a","col_b") VALUES('a1','b1'),('a2','b2')
    // executing the query:
    db.none(query);
};

是的,这是来自其他地方的 sn-p。 “eventHubMessages”应包含有效负载。到目前为止,我遇到的几个问题是:

  • 我可以发送脚本中定义的有效负载,或者同时给它一个测试有效负载,但我无法发送实际消息的有效负载
  • pg-promise 无论失败与否都会返回 202,因此目前调试是“盲目的”。任何有关如何获得正确日志记录的提示将不胜感激。
  • 我在事件中心实例中使用了“捕获事件”来捕获实际消息。这些存储在 blob 存储中。注意到格式是 Avro。我是否需要剥离该对象才能到达实际数组?

输入应如下所示:

[{"customer": duderino, "tag": nice_rug, "value": 10, "period": 163249839}]

我认为我有 2 个问题:

  1. 我不知道如何使用 Node.JS 有意义地退出 Azure 函数
  2. 我的有效载荷是如何进入的。

一个更深层次的问题是,我如何知道 Azure 函数是否正在获取它应该获取的数据。我知道事件网格获取数据,但没有吞吐量。命名空间是一致的,Azure 函数应该由该命名空间触发并以字符串形式获取输入。

我迷失了方向,超出了我的深度。除了解决方案之外,我还将感谢对我的请求的反馈。我不是 StackOverflow 的专家,不想浪费你的时间。

问候

【问题讨论】:

    标签: javascript node.js postgresql azure


    【解决方案1】:

    好的,经过一番挖掘,我找到了一些解决问题的方法。首先,我以字符串形式接收有效负载,这意味着我需要先对其进行解析,然后才能使其成为可调用对象。代码很简单,是node.js的部分基础功能

    var parsed_payload = JSON.parse(payload_that_is_a_string);
    

    最后,为了获得有意义的日志记录,我发现 PG-Promise 模块对此有很好的支持,并且可以在加载模块本身时进行配置。我对错误特别感兴趣,所以我像这样启用了该选项:

    const initOptions = {
            query(e) {console.log(e.query)},
            capSQL: true,
            //capSQL: true // capitalize all generated SQL
            error: function (error, e) {
                if (e.cn) {
                    // A connection-related error;
                    // console.log("DC:", e.cn);
                    // console.log("EVENT:", error.message);
                }
            }
        };
    
    

    然后可以用作加载 PG-Promise 的设置对象:

    const pgp = require('pg-promise')(initOptions);
    
    

    感谢您考虑我的请求。我希望这对任何人都有用!

    问候彼得

    【讨论】:

      猜你喜欢
      • 2018-01-07
      • 1970-01-01
      • 2021-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-12
      • 2020-06-21
      • 2018-02-28
      相关资源
      最近更新 更多