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
The cloud service integration module is installed in the blockchain instance.
Cloud service integration sample code is downloaded.
You hava a database instance with public network access (take RDS for MySQL as an example, version >= 5.6).
Install nodev8 locally (version >=8.17.0).
Procedure
Connect to database instance, create relevant database and table:
Enter into DMS console, login the Database instance;
Open a new SQL window via “SQL Operation -> SQL Window” in upper menu bar;
Copy SQL file
./blockchain2db/taskmgr.sql
to SQL window and execute, make sure all the SQL statements are successful;
According to the step 1 in Initiate sample transactions, install and instantiate the chaincode in the channel.
Create a Database Trigger of Cloud Service Integration in BaaS console:
Choose channel
taskmgr
and smart contracttaskmgr
;Choose
Tx
as event type;Choose MySQL as database type (take MySQL as an example here, you can choose other types);
Input the address, userid, password of the database instance;
Input
octopus
as database name;Input
write_set
as table name.
According to subsequent steps in Initiate sample transactions, generate transactions in the channel.
Then you can check the data in database
octopus
:Check the write_set of transactions in staging table via
SELECT * FROM write_set
Check the approval task details in business table via
SELECT * FROM taskmgr
Initiate sample transactions
Deploy sample chaincode taskmgr to the channel by referring to Chaincode Deployment.
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.Execute
npm install
to install dependent packages andnode 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=' }