Query Chaincode Data in the State Database - Amazon Managed Blockchain

Query Chaincode Data in the State Database

The state database in Hyperledger Fabric peer nodes on Managed Blockchain networks created using Hyperledger Fabric 1.4 and later supports two types of peer state databases:

  • CouchDB is a state database in Managed Blockchain that models ledger data as JSON. It is the default peer state database for Managed Blockchain networks running Hyperledger Fabric 1.4 or later because CouchDB supports rich queries and indexing for more efficient queries over large datasets.

  • LevelDB is a state database that stores ledger data as simple key-value pairs. It is the only peer state database available for Managed Blockchain networks running Hyperledger Fabric 1.2. LevelDB supports only key, key range, and composite key queries.

For detailed information about CouchDB implementation and capabilities in Hyperledger Fabric, see CouchDB as the State Database in Hyperledger Fabric documentation.

Specifying and Viewing the State Database Type

You specify the peer database type when you create a peer node for a member using the AWS Management Console, the AWS CLI, or the Managed Blockchain API. For more information, see Create a Peer Node and CreateNode in the Amazon Managed Blockchain API Reference. You can't change the state database type after a node has been created.

The type of state database that a peer node uses is available on the peer node properties page in the AWS Management Console. It is also returned by the AWS CLI managedblockchain get-node command. For more information, see .

Rich Queries With CouchDB

The following examples demonstrate rich queries against a CouchDB state database using the peer chaincode query command of the Hyperledger Fabric CLI in Managed Blockchain. The examples are based on the marbles02 chaincode sample on GitHub. This example includes implementations of rich query functionality within chaincode, including the code that supports the queryMarbles and the queryMarblesWithPagination functions passed as arguments in the examples.

The marbles02 sample chaincode is available when you set up a Hyperledger Fabric client according to the instructions in the getting started tutorial of this guide. Those instructions include a step to clone the samples repository that contains marbles02.

For examples of basic queries that can be run when using either LevelDB or CouchDB, see the query chaincode example in the getting started tutorial of this guide.

Before querying the chaincode, install the chaincode, instantiate it, and invoke the chaincode to add three ledger records as shown in the following example commands. For more information, see the peer chaincode command reference in Hyperledger Fabric documentation.

docker exec cli peer chaincode install \ -n myrichquerycc -v v0 -p github.com/marbles02/go
docker exec cli peer chaincode instantiate \ -o $ORDERER -C mychannel -n myrichquerycc \ -v v0 -c '{"Args":["init"]}' \ --cafile /opt/home/managedblockchain-tls-chain.pem --tls
docker exec cli peer chaincode invoke \ -o $ORDERER -C mychannel -n myrichquerycc \ -c '{"Args":["initMarble","marble1","blue","10","tom"]}' \ --cafile /opt/home/managedblockchain-tls-chain.pem --tls
docker exec cli peer chaincode invoke \ -o $ORDERER -C mychannel -n myrichquerycc \ -c '{"Args":["initMarble","marble2","red","20","tom"]}' \ --cafile /opt/home/managedblockchain-tls-chain.pem --tls
docker exec cli peer chaincode invoke \ -o $ORDERER -C mychannel -n myrichquerycc \ -c '{"Args":["initMarble","marble3","green","30","tom"]}' \ --cafile /opt/home/managedblockchain-tls-chain.pem --tls

Now that the state database is populated with three records, use the rich query function queryMarbles defined within the marbles02 chaincode sample as shown in the following example. The query returns only those marbles owned by tom.

docker exec cli peer chaincode query -o $ORDERER \ -C mychannel -n myrichquerycc \ -c '{"Args":["queryMarbles","{\"selector\":{\"owner\":\"tom\"}}"]}' \ --cafile /opt/home/managedblockchain-tls-chain.pem --tls

The query example below uses the rich query function queryMarblesWithPagination defined within the marbles02 chaincode sample. The query is similar to the previous rich query. However, this function allows you to specify the number of records to return (the page size) and an optional bookmark. The page size specified in the example is 1 and the bookmark argument is empty.

docker exec cli peer chaincode query \ -o $ORDERER -C mychannel -n myrichquerycc \ -c '{"Args":["queryMarblesWithPagination","{\"selector\":{\"owner\":\"tom\"}}","1",""]}' \ --cafile /opt/home/managedblockchain-tls-chain.pem --tls