【发布时间】: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 个问题:
- 我不知道如何使用 Node.JS 有意义地退出 Azure 函数
- 我的有效载荷是如何进入的。
一个更深层次的问题是,我如何知道 Azure 函数是否正在获取它应该获取的数据。我知道事件网格获取数据,但没有吞吐量。命名空间是一致的,Azure 函数应该由该命名空间触发并以字符串形式获取输入。
我迷失了方向,超出了我的深度。除了解决方案之外,我还将感谢对我的请求的反馈。我不是 StackOverflow 的专家,不想浪费你的时间。
问候
【问题讨论】:
标签: javascript node.js postgresql azure