All Products
Search
Document Center

Blockchain as a Service:Export Blockchain Data to Database

Last Updated:Mar 31, 2026

Export blockchain transaction data to an external relational database for business analytics, reporting, or downstream system integration — without affecting on-chain performance. The cloud service integration module streams write sets from your blockchain channel to a staging table, then a SQL trigger automatically propagates the data to your business table.

This topic covers:

  • Setting up the database schema (staging table and business table)

  • Deploying and instantiating the sample chaincode

  • Configuring a Database Trigger in the BaaS console

  • Generating sample transactions and verifying data export

Prerequisites

Before you begin, ensure that you have:

How it works

Each blockchain transaction on Hyperledger Fabric produces a write set — a record of the key-value state changes committed to the ledger. The cloud service integration module captures these write sets from the Tx event stream and inserts them into a staging table (write_set) in your database. A SQL trigger then reads from the staging table and writes the parsed task data into a business table (taskmgr).

This two-table design separates raw ledger data from your application data, making it straightforward to extend the transformation logic without touching the chaincode.

Set up the database schema

  1. Log in to the DMS console and connect to your database instance.

  2. Open a SQL window: in the upper menu bar, click SQL Operation > SQL Window.

  3. Copy the contents of ./blockchain2db/taskmgr.sql from the sample code into the SQL window and run the script. Confirm that all statements execute successfully. The script creates the octopus database, the write_set staging table, the taskmgr business table, and the trigger that transfers records between them.

Deploy the sample chaincode

Deploy the taskmgr chaincode to your channel by following Chaincode Deployment. Install and instantiate the chaincode before proceeding.

Create a Database Trigger

In the BaaS console, navigate to the cloud service integration section and create a Database Trigger with the following settings:

SettingValue
Channeltaskmgr
Smart contracttaskmgr
Event typeTx
Database typeMySQL (or another supported type)
Database addressYour database instance endpoint
User IDYour database username
PasswordYour database password
Database nameoctopus
Table namewrite_set

Generate sample transactions

  1. Go to the blockchain2sms directory in the sample code.

  2. Open main.js and fill in the following parameters based on the inline comments:

    • REST API address

    • Refresh token

    • Channel name

    • Smart contract name

    For more information on obtaining these values, see Use REST APIs.

  3. Install dependencies and run the script:

    Note: This sample script is for demonstration purposes only. The content and data fields in the output are base64-encoded payloads representing the write set of each transaction. A production implementation should add error handling and adapt the transaction submission logic to your application's requirements.

    npm install
    node main.js

    A successful run outputs a transaction ID and status for each submitted transaction:

    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=' }

Verify data export

After the transactions complete, query both tables to confirm the data was exported correctly.

Check the staging table for raw write set records:

SELECT * FROM write_set
write_set

Check the business table for parsed task records:

SELECT * FROM taskmgr
taskmgr

Records appearing in both tables confirm that the cloud service integration module successfully captured the blockchain transactions and the SQL trigger propagated them to the business table.

What's next

  • Modify the SQL trigger logic in taskmgr.sql to match your own business table schema.

  • Replace the sample main.js with your application's transaction-submission logic using the REST APIs.

  • To integrate with a different database type, select the appropriate database type when creating the Database Trigger in the BaaS console.