All Products
Search
Document Center

Use Filter

Last Updated: Jun 11, 2020

When you configure a cloud service integration trigger, you can configure a filter for the trigger, which can filter and process the blockchain data. You can refer to Fabric Sample Message for various data structures of blockchain events. For filter expression syntax, please refer to jq tool basic syntax.

The following example describes how to use a filter to sort out and preprocess blockchain events. A Function Compute trigger is used in this example. You must perform the following steps to prepare the environment:

  1. Download the cloud service integration sample code.
  2. Install the Function Compute tool funcraft. For more information, see funcraft introduction and installation method.
  3. Go to the event-with-filter directory and execute fun deploy to deploy a function to Function Compute.
  4. Log on to the Function Compute console and enable log queries for the newly created function LoggerFunc.Enable log queries

  5. Deploy sample chaincode taskmgr in the channel by referring to Deploy a chaincode.

  6. Configure a cloud service integration trigger.
    1. Fill in information about the Function Compute instance by referring to the help documentation.
    2. In the advanced options, fill in filter expressions based on sample scenarios.
  7. Go to the blockchain2sms directory, modify the parameters in main.js based on comments, and fill in the REST API address, refresh token, channel name, and smart contract name. For more information, see Use REST APIs.
  8. Execute npm install to install dependent packages and node main.js to initiate a sample transaction.
  9. View Function Compute logs and check the event content.

Sample scenarios

1. Push only events that meet specific conditions

Push only events whose smart contract event name is event-create-task.

  • Event type: Contract
  • Filter configuration: select(.name=="event-create-task")
  • Example of pushed data:
  1. {
  2. "content": "eyJuYW1lIjoidGFzay0xNTgwOTcxODQyOTM3IiwiY3JlYXRvciI6ImUyZWhtZnFhc3RoTVNQLm9jdG9wdXNfMjY4NDJfMTIzNDU2Nzg5MDEyMzQiLCJyZXF1aXJlcyI6WyJlMmVobWZxYXN0aE1TUC5vY3RvcHVzXzI2ODQyXzEyMzQ1Njc4OTAxMjM0Il0sImFwcHJvdmVkIjpudWxsLCJkZXNjcmlwdGlvbiI6IuekuuS+i+S7u+WKoe+8jHJlcXVpcmVzIOmFjee9ruWuoeaJueS7u+WKoeWujOaIkOmcgOimgemCo+S6m+eUqOaIt+WQjOaEj+OAgueUqOaIt+aPj+i/sOS4uiAn57uE57uHTVNQLueUqOaIt+WQjeensCcifQ==",
  3. "id": "contract-131-ca144c3385f56a429b9e8874173f7c97fbb49de69f931aec583ec50222a3a2ed",
  4. "instance_id": "csi-e2ehmfqasth-bcw7tzao2dzeo",
  5. "name": "event-create-task",
  6. "network": "channel3",
  7. "platform": "Fabric",
  8. "type": 8
  9. }

2. Push only specific valid transaction events

Push only valid transactions sent to smart contract taskmgr.

  • Event type: Tx
  • Filter configuration: select(.content.to=="taskmgr" and .content.state=="VALID")
  • Example of pushed data:
    1. {
    2. "content": {
    3. "events": [
    4. "event-create-task"
    5. ],
    6. "from": "e2ehmfqasthMSP.octopus_26842_12345678901234",
    7. "id": "3a2e0e375c6ee0d9c6c20d756e787db44d50bfeba51f22d8f262dd1556dedba0",
    8. "input": "[\"create\",\"task-1580971842937\",\"{\\n \\\"requires\\\": [\\\"e2ehmfqasthMSP.octopus_26842_12345678901234\\\"],\\n \\\"description\\\": \\\"Sample task. Task completion requires the approval of certain users. The user is described as 'Organization MSP. User name'\\\"\\n }\"]",
    9. "state": "VALID",
    10. "to": "taskmgr"
    11. },
    12. "id": "tx-128-3a2e0e375c6ee0d9c6c20d756e787db44d50bfeba51f22d8f262dd1556dedba0",
    13. "instance_id": "csi-e2ehmfqasth-bcw7tzao2dzeo",
    14. "name": "3a2e0e375c6ee0d9c6c20d756e787db44d50bfeba51f22d8f262dd1556dedba0",
    15. "network": "channel3",
    16. "platform": "Fabric",
    17. "type": 2
    18. }

3. Push only part of the event content

Push only the content of smart contract events that have the event name event-create-task.

  • Event type: Contract
  • Filter configuration: select(.name=="event-create-task") | .content
  • Example of pushed data:
    1. "eyJuYW1lIjoidGFzay0xNTgwOTcxODQyOTM3IiwiY3JlYXRvciI6ImUyZWhtZnFhc3RoTVNQLm9jdG9wdXNfMjY4NDJfMTIzNDU2Nzg5MDEyMzQiLCJyZXF1aXJlcyI6WyJlMmVobWZxYXN0aE1TUC5vY3RvcHVzXzI2ODQyXzEyMzQ1Njc4OTAxMjM0Il0sImFwcHJvdmVkIjpudWxsLCJkZXNjcmlwdGlvbiI6IuekuuS+i+S7u+WKoe+8jHJlcXVpcmVzIOmFjee9ruWuoeaJueS7u+WKoeWujOaIkOmcgOimgemCo+S6m+eUqOaIt+WQjOaEj+OAgueUqOaIt+aPj+i/sOS4uiAn57uE57uHTVNQLueUqOaIt+WQjeensCcifQ=="

4. Obtain the write-in information of a blockchain

Push only the write-in information for all keys. The information must be contained in the valid transactions generated by smart contract taskmgr.

  • Event type: Tx
  • Filter configuration: 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
  • Example of pushed data:
    1. [{"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=="}]

5. Monitor specific keys

Monitor valid transactions generated by smart contract taskmgr to check the value change of a specific key.

  • Event type: Tx
  • Filter configuration: 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")
  • Example of pushed data:
    1. {"is_delete":false,"key":"special_key","value":"MTA="}

5. Export Data to Database Table

We can export specific blockchain data to database table via filter. The data is processed and transformed to an Object Array by filter, each Object represents a record to be inserted. The Object Keys are table column names, while the Object Values are table data:

  1. [
  2. {
  3. "columnName1": "value1",
  4. "columnName2": "value2",
  5. "columnName3": "value3",
  6. },
  7. {
  8. "columnName1": "value3",
  9. "columnName2": "value4",
  10. "columnName3": "value5",
  11. },
  12. ...
  13. ]

Export the valid transactions content written by smart contract taskmgr to below table:

  • Table structure:
    • primary key: (event_id,namespace,key)
    • event_id: Event ID
    • namespace: name of smart contract
    • key: content of key
    • value: content of value in Base64 encoding
    • create_time: time of written
    • tx_id: transaction ID
    • is_delete: if this is a delete operation
    • creator: initiator of this operation
  • filter configuration: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
  • Event type: Tx
  • Sample of exported data:
    1. [
    2. {
    3. "event_id": "tx-128-3a2e0e375c6ee0d9c6c20d756e787db44d50bfeba51f22d8f262dd1556dedba0",
    4. "namespace": "taskmgr",
    5. "tx_id": "3a2e0e375c6ee0d9c6c20d756e787db44d50bfeba51f22d8f262dd1556dedba0",
    6. "is_delete": false,
    7. "key": "\u0000e2ehmfqasthMSP.octopus_26842_12345678901234\u0000task-1581479306267\u0000",
    8. "value": "eyJuYW1lIjoidGFzay0xNTgwOTcxODQyOTM3IiwiY3JlYXRvciI6ImUyZWhtZnFhc3RoTVNQLm9jdG9wdXNfMjY4NDJfMTIzNDU2Nzg5MDEyMzQiLCJyZXF1aXJlcyI6WyJlMmVobWZxYXN0aE1TUC5vY3RvcHVzzI2ODQyXzEyMzQ1Njc4OTAxMjM0Il0sImFwcHJvdmVkIjpudWxsLCJkZXNjcmlwdGlvbiI6IuekuuS+i+S7u+WKoe+8jHlcXVpcmVzIOmFjee9ruWuoeaJueS7u+WKoeWujOaIkOmcgOimgemCo+S6m+eUqOaIt+WQjOaEj+OAgueUqOaIt+aPj+i/sOS4uiAn57uE57uHTVNQLueUqOaIt+WQjeensCcifQ==",
    9. "create_time": "2020-02-06T06:50:42.267092Z",
    10. "creator": "e2ehmfqasthMSP.octopus_26842_12345678901234"
    11. }
    12. ]