All Products
Search
Document Center

Export Blockchain Data to Database

Last Updated: Jun 11, 2020

This topic describes how to export the data in blockchain to external database. You can use sample SQL statements to create staging table ‘write_set’, business table ‘taskmgr’ and the trigger which transfer records in staging table to business table. After the blockchain transactions are initiated, you can query transaction write_set in staging table, and final information in business table.

Prerequisites

  1. The cloud service integration module is installed in the blockchain instance.
  2. Cloud service integration sample code is downloaded.
  3. You hava a database instance with public network access (take RDS for MySQL as an example, version >= 5.6).
  4. Install nodev8 locally (version >=8.17.0).

Procedure

  1. Connect to database instance, create relevant database and table:
    1. Enter into DMS console, login the Database instance;
    2. Open a new SQL window via “SQL Operation -> SQL Window” in upper menu bar;
    3. Copy SQL file ./blockchain2db/taskmgr.sql to SQL window and execute, make sure all the SQL statements are successful;
  2. According to the step 1 in Initiate sample transactions, install and instantiate the chaincode in the channel.
  3. Create a Database Trigger of Cloud Service Integration in BaaS console:
    1. Choose channel taskmgr and smart contract taskmgr
    2. Choose Tx as event type;
    3. Choose MySQL as database type (take MySQL as an example here, you can choose other types);
    4. Input the address, userid, paasword of the database instance;
    5. Input octopus as database name;
    6. Input write_set as table name.
  4. According to subsequent steps in Initiate sample transactions, generate transactions in the channel.
  5. Then you can check the data in database octopus:
    1. Check the write_set of transactions in staging table via SELECT * FROM write_setwrite_set
    2. Check the approval task details in business table via SELECT * FROM taskmgrtaskmgr

Initiate sample transactions

  1. Deploy sample chaincode taskmgr to the channel by referring to Chaincode Deployment.
  2. 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.
  3. Execute npm install to install dependent packages and node main.js to initiate a sample transaction.

Sample successful output

  1. Data pushed to blockchain with transaction 701c7006f26aed8457273a00bbfcc8cea4d75eac958996e07837036ea7e2fdac
  2. { id: '701c7006f26aed8457273a00bbfcc8cea4d75eac958996e07837036ea7e2fdac',
  3. status: '200',
  4. events:
  5. [ { type: 'Contract',
  6. platform: 'Fabric',
  7. instance_id: 'csi-e2ehmfqasth-bcw7tzao2dzeo',
  8. network: '',
  9. id: '',
  10. name: 'event-create-task',
  11. content: 'eyJuY...TAz' }
  12. Data pushed to blockchain with transaction 60a06a189415db587b49cbf91b46467bce1ea16490b19f6dfc8d520aa31240bc
  13. { id: '60a06a189415db587b49cbf91b46467bce1ea16490b19f6dfc8d520aa31240bc',
  14. status: '200',
  15. events:
  16. [ { type: 'Contract',
  17. platform: 'Fabric',
  18. instance_id: 'csi-e2ehmfqasth-bcw7tzao2dzeo',
  19. network: '',
  20. id: '',
  21. name: 'event-task-finished',
  22. content: 'eyJuY...p7In0=' } ],
  23. data: 'eyJu...In0=' }
  24. Data pushed to blockchain with transaction 7810e0496a1c91a16102736b00a6f26da0baa42874a907fff26bff1b7eb3bf27
  25. { id: '7810e0496a1c91a16102736b00a6f26da0baa42874a907fff26bff1b7eb3bf27',
  26. status: '200',
  27. events: [],
  28. data: 'eyJu...nIn0=' }