All Products
Search
Document Center

Blockchain as a Service:Export Blockchain Data to Database

Last Updated:Jul 19, 2023

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, password 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

Data pushed to blockchain with transaction 701c7006f26aed8457273a00bbfcc8cea4d75eac958996e07837036ea7e2fdac
{ id: '701c7006f26aed8457273a00bbfcc8cea4d75eac958996e07837036ea7e2fdac',
  status: '200',
  events:
   [ { type: 'Contract',
       platform: 'Fabric',
       instance_id: 'csi-e2ehmfqasth-bcw7tzao2dzeo',
       network: '',
       id: '',
       name: 'event-create-task',
       content: 'eyJuY...TAz' }
Data pushed to blockchain with transaction 60a06a189415db587b49cbf91b46467bce1ea16490b19f6dfc8d520aa31240bc
{ id: '60a06a189415db587b49cbf91b46467bce1ea16490b19f6dfc8d520aa31240bc',
  status: '200',
  events:
   [ { type: 'Contract',
       platform: 'Fabric',
       instance_id: 'csi-e2ehmfqasth-bcw7tzao2dzeo',
       network: '',
       id: '',
       name: 'event-task-finished',
       content: 'eyJuY...p7In0=' } ],
  data: 'eyJu...In0=' }
Data pushed to blockchain with transaction 7810e0496a1c91a16102736b00a6f26da0baa42874a907fff26bff1b7eb3bf27
{ id: '7810e0496a1c91a16102736b00a6f26da0baa42874a907fff26bff1b7eb3bf27',
  status: '200',
  events: [],
  data: 'eyJu...nIn0=' }