在配置云服务集成触发器时,您可以为触发器配置过滤器,以便对区块链数据进行过滤和简单的处理。区块链各类事件的数据结构请参考Fabric示例消息。 过滤器表达式遵循的语法请参考jq工具基本语法。下面介绍如何使用过滤器对区块链事件进行过滤和简单的预处理,本示例通过函数计算触发器来演示功能,您需要按如下步骤准备环境:下载云服务集成示例代码。安装函数计算工具 Serverless Devs,具体可参见 Serverless Devs介绍及安装方式。进入目录 event-with-filter 使用 s deploy 将函数部署到函数计算。进入函数计算控制台,为我们刚才创建的函数 LoggerFunc 开通日志查询。按照文档部署链码将示例链码 taskmgr 部署到通道中。配置云服务集成触发器:根据帮助,填写函数计算实例的相关信息。在高级选项中,根据以下不同的示例场景,填写过滤器表达式。进入 blockchain2sms 目录,按照注释修改 main.js 中的配置参数,填入REST API地址、Refresh Token、通道名和智能合约名称。更多信息请参考文档使用REST API。执行 npm install 安装依赖包,通过 node main.js 发起示例交易。查看函数计算的日志,观察收到的事件内容。示例场景一、只推送满足特定条件的事件只推送智能合约事件名称为 event-create-task 的事件:事件类型选择: Contract过滤器配置: select(.name=="event-create-task")推送数据示例:{
"content": "eyJuYW1lIjoidGFzay0xNTgwOTcxODQyOTM3IiwiY3JlYXRvciI6ImUyZWhtZnFhc3RoTVNQLm9jdG9wdXNfMjY4NDJfMTIzNDU2Nzg5MDEyMzQiLCJyZXF1aXJlcyI6WyJlMmVobWZxYXN0aE1TUC5vY3RvcHVzXzI2ODQyXzEyMzQ1Njc4OTAxMjM0Il0sImFwcHJvdmVkIjpudWxsLCJkZXNjcmlwdGlvbiI6IuekuuS+i+S7u+WKoe+8jHJlcXVpcmVzIOmFjee9ruWuoeaJueS7u+WKoeWujOaIkOmcgOimgemCo+S6m+eUqOaIt+WQjOaEj+OAgueUqOaIt+aPj+i/sOS4uiAn57uE57uHTVNQLueUqOaIt+WQjeensCcifQ==",
"id": "contract-131-ca144c3385f56a429b9e8874173f7c97fbb49de69f931aec583ec50222a3a2ed",
"instance_id": "csi-e2ehmfqasth-bcw7tzao2dzeo",
"name": "event-create-task",
"network": "channel3",
"platform": "Fabric",
"type": "Contract"
}二、只推送有效的特定交易事件只推送发送给智能合约 taskmgr 的交易,且交易状态为有效:事件类型选择: Tx过滤器配置:select(.content.to=="taskmgr" and .content.state=="VALID")推送数据示例:{
"content": {
"events": [
"event-create-task"
],
"from": "e2ehmfqasthMSP.octopus_26842_12345678901234",
"id": "3a2e0e375c6ee0d9c6c20d756e787db44d50bfeba51f22d8f262dd1556dedba0",
"input": "[\"create\",\"task-1580971842937\",\"{\\n \\\"requires\\\": [\\\"e2ehmfqasthMSP.octopus_26842_12345678901234\\\"],\\n \\\"description\\\": \\\"示例任务,requires 配置审批任务完成需要那些用户同意。用户描述为 '组织MSP.用户名称'\\\"\\n }\"]",
"state": "VALID",
"to": "taskmgr"
},
"id": "tx-128-3a2e0e375c6ee0d9c6c20d756e787db44d50bfeba51f22d8f262dd1556dedba0",
"instance_id": "csi-e2ehmfqasth-bcw7tzao2dzeo",
"name": "3a2e0e375c6ee0d9c6c20d756e787db44d50bfeba51f22d8f262dd1556dedba0",
"network": "channel3",
"platform": "Fabric",
"type": "Tx"
}三、只推送事件的部分内容只推送智能合约事件的内容,且事件的名称为 event-create-task:事件类型选择: Contract过滤器配置:select(.name=="event-create-task") | .content推送数据示例:"eyJuYW1lIjoidGFzay0xNTgwOTcxODQyOTM3IiwiY3JlYXRvciI6ImUyZWhtZnFhc3RoTVNQLm9jdG9wdXNfMjY4NDJfMTIzNDU2Nzg5MDEyMzQiLCJyZXF1aXJlcyI6WyJlMmVobWZxYXN0aE1TUC5vY3RvcHVzXzI2ODQyXzEyMzQ1Njc4OTAxMjM0Il0sImFwcHJvdmVkIjpudWxsLCJkZXNjcmlwdGlvbiI6IuekuuS+i+S7u+WKoe+8jHJlcXVpcmVzIOmFjee9ruWuoeaJueS7u+WKoeWujOaIkOmcgOimgemCo+S6m+eUqOaIt+WQjOaEj+OAgueUqOaIt+aPj+i/sOS4uiAn57uE57uHTVNQLueUqOaIt+WQjeensCcifQ=="四、获取区块链的写入情况只推送智能合约 taskmgr 中所有 Key 的写入情况,且状态为有效的交易:事件类型选择: Tx过滤器配置:select(.content.state=="VALID" and .content.to=="taskmgr") | .content.data.data.actions.[0].payload.action.proposal_response_payload.extension.results.ns_rwset[] | select(.namespace=="taskmgr") | .rwset.writes推送数据示例:[{"is_delete":false,"key":"\u0000e2ehmfqasthMSP.octopus_26842_12345678901234\u0000task-1581479306267\u0000","value":"eyJuYW1lIjoidGFzay0xNTgwOTcxODQyOTM3IiwiY3JlYXRvciI6ImUyZWhtZnFhc3RoTVNQLm9jdG9wdXNfMjY4NDJfMTIzNDU2Nzg5MDEyMzQiLCJyZXF1aXJlcyI6WyJlMmVobWZxYXN0aE1TUC5vY3RvcHVzXzI2ODQyXzEyMzQ1Njc4OTAxMjM0Il0sImFwcHJvdmVkIjpudWxsLCJkZXNjcmlwdGlvbiI6IuekuuS+i+S7u+WKoe+8jHJlcXVpcmVzIOmFjee9ruWuoeaJueS7u+WKoeWujOaIkOmcgOimgemCo+S6m+eUqOaIt+WQjOaEj+OAgueUqOaIt+aPj+i/sOS4uiAn57uE57uHTVNQLueUqOaIt+WQjeensCcifQ=="}]五、对特定Key进行监控监控智能合约 taskmgr 中特定 Key 的 Value 变化情况,且状态为有效的交易:事件类型选择: Tx过滤器配置:select(.content.state=="VALID" and .content.to=="taskmgr") | .content.data.data.actions.[0].payload.action.proposal_response_payload.extension.results.ns_rwset[] | select(.namespace=="taskmgr") | .rwset.writes[] | select(.key=="special_key")推送数据示例:{"is_delete":false,"key":"special_key","value":"MTA="}六、导出数据到数据库表利用过滤器我们可以将区块链数据导出至数据库的自定义表中。过滤器对数据进行处理转换成一个Object的数组,每个Object表示需要插入的一行数据,Object的Key为表的列名,Value为需要插入的数据:[
{
"columnName1": "value1",
"columnName2": "value2",
"columnName3": "value3",
},
{
"columnName1": "value3",
"columnName2": "value4",
"columnName3": "value5",
},
...
]将智能合约 taskmgr 所有合法交易的写入内容导出到以下表中:表结构:主键: (event_id,namespace,key)event_id, 事件IDnamespace, 写入的命名空间(智能合约名称)key, 写入的Keyvalue, 经过Base64编码的写入值create_time, 写入的时间tx_id, 对应的交易IDis_delete, 是否为删除操作creator, 写入操作的发起者过滤器配置:select(.content.state=="VALID" and .content.to=="taskmgr") | .id as $eventID | .content.id as $txID | .content.from as $creator | content.data.header.channel_header.timestamp as $createTime | .content.data.data.actions.[0].payload.action.proposal_response_payload.extension.results.ns_rwset | ap(.namespace as $namespace | .rwset.writes | map( .event_id = $eventID | .namespace = $namespace | .tx_id = $txID | .create_time = $createTime | .creator = $creator )) | dd事件类型选择: Tx推送数据示例:[
{
"event_id": "tx-128-3a2e0e375c6ee0d9c6c20d756e787db44d50bfeba51f22d8f262dd1556dedba0",
"namespace": "taskmgr",
"tx_id": "3a2e0e375c6ee0d9c6c20d756e787db44d50bfeba51f22d8f262dd1556dedba0",
"is_delete": false,
"key": "\u0000e2ehmfqasthMSP.octopus_26842_12345678901234\u0000task-1581479306267\u0000",
"value": "eyJuYW1lIjoidGFzay0xNTgwOTcxODQyOTM3IiwiY3JlYXRvciI6ImUyZWhtZnFhc3RoTVNQLm9jdG9wdXNfMjY4NDJfMTIzNDU2Nzg5MDEyMzQiLCJyZXF1aXJlcyI6WyJlMmVobWZxYXN0aE1TUC5vY3RvcHVXzI2ODQyXzEyMzQ1Njc4OTAxMjM0Il0sImFwcHJvdmVkIjpudWxsLCJkZXNjcmlwdGlvbiI6IuekuuS+i+S7u+WKoe+8jHlcXVpcmVzIOmFjee9ruWuoeaJueS7u+WKoeWujOaIkOmcgOimgemCo+S6m+eUqOaIt+WQjOaEj+OAgueUqOaIt+aPj+i/sOS4uiAn57uE57uHTVNQLueUqOaIt+WQjeensCcifQ==",
"create_time": "2020-02-06T06:50:42.267092Z",
"creator": "e2ehmfqasthMSP.octopus_26842_12345678901234"
}
]
上一篇:没有了