@@ -649,6 +649,165 @@ run!
649
649
650
650
rm -rf /var/hyperledger/*
651
651
652
+ Using CouchDB
653
+ -------------
654
+
655
+ The state database can be switched from the default (goleveldb) to CouchDB.
656
+ The same chaincode functions are available with CouchDB, however, there is the
657
+ added ability to perform rich and complex queries against the state database
658
+ data content contingent upon the chaincode data being modeled as JSON.
659
+
660
+ To use CouchDB instead of the default database (goleveldb), follow the same
661
+ procedure in the **Prerequisites ** section, and additionally perform the
662
+ following two steps to enable the CouchDB containers and associate each peer
663
+ container with a CouchDB container:
664
+
665
+ - Make the CouchDB image.
666
+
667
+ .. code :: bash
668
+
669
+ # make sure you are in the /fabric directory
670
+ make couchdb
671
+
672
+ - Open the ``fabric/examples/e2e_cli/docker-compose.yaml `` and un-comment
673
+ all commented statements relating to CouchDB containers and peer container
674
+ use of CouchDB. These instructions are are also outlined in the
675
+ same ``docker-compose.yaml `` file. Search the file for 'couchdb' (case insensitive) references.
676
+
677
+ *chaincode_example02 * should now work using CouchDB underneath.
678
+
679
+ ***Note* **: If you choose to implement mapping of the fabric-couchdb container
680
+ port to a host port, please make sure you are aware of the security
681
+ implications. Mapping of the port in a development environment allows the
682
+ visualization of the database via the CouchDB web interface (Fauxton).
683
+ Production environments would likely refrain from implementing port mapping in
684
+ order to restrict outside access to the CouchDB containers.
685
+
686
+ You can use *chaincode_example02 * chaincode against the CouchDB state database
687
+ using the steps outlined above, however in order to exercise the query
688
+ capabilities you will need to use a chaincode that has data modeled as JSON,
689
+ (e.g. *marbles02 *). You can locate the *marbles02 * chaincode in the
690
+ ``fabric/examples/chaincode/go `` directory.
691
+
692
+ Install, instantiate, invoke, and query *marbles02 * chaincode by following the
693
+ same general steps outlined above for *chaincode_example02 * in the **Manually
694
+ create the channel and join peers through CLI ** section . After the **Join
695
+ channel ** step, use the following steps to interact with the *marbles02 *
696
+ chaincode:
697
+
698
+ - Install and instantiate the chaincode in ``peer0 `` (replace ``$ORDERER_IP ``
699
+ with the IP address of the orderer. One way to find the address is with the
700
+ command ``docker inspect orderer | grep \"IPAddress\" ``):
701
+
702
+ .. code :: bash
703
+
704
+ peer chaincode install -o $ORDERER_IP :7050 -n marbles -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/marbles02
705
+ peer chaincode instantiate -o $ORDERER_IP :7050 -C $mychannel -n marbles -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/marbles02 -c ' {"Args":["init"]}' -P " OR ('Org0MSP.member','Org1MSP.member')"
706
+
707
+ - Create some marbles and move them around:
708
+
709
+ .. code :: bash
710
+
711
+ peer chaincode invoke -o $ORDERER_IP :7050 -C mychannel -n marbles -c ' {"Args":["initMarble","marble1","blue","35","tom"]}'
712
+ peer chaincode invoke -o $ORDERER_IP :7050 -C mychannel -n marbles -c ' {"Args":["initMarble","marble2","red","50","tom"]}'
713
+ peer chaincode invoke -o $ORDERER_IP :7050 -C mychannel -n marbles -c ' {"Args":["initMarble","marble3","blue","70","tom"]}'
714
+ peer chaincode invoke -o $ORDERER_IP :7050 -C mychannel -n marbles -c ' {"Args":["transferMarble","marble2","jerry"]}'
715
+ peer chaincode invoke -o $ORDERER_IP :7050 -C mychannel -n marbles -c ' {"Args":["transferMarblesBasedOnColor","blue","jerry"]}'
716
+ peer chaincode invoke -o $ORDERER_IP :7050 -C mychannel -n marbles -c ' {"Args":["delete","marble1"]}'
717
+
718
+
719
+
720
+ - If you chose to activate port mapping, you can now view the state database
721
+ through the CouchDB web interface (Fauxton) by opening a browser and
722
+ navigating to one of the two URLs below.
723
+
724
+ For containers running in a vagrant environment:
725
+
726
+ ```http://localhost:15984/_utils` ``
727
+
728
+ For non-vagrant environment, use the port address that was mapped in CouchDB
729
+ container specification:
730
+
731
+ ```http://localhost:5984/_utils` ``
732
+
733
+ You should see a database named ``mychannel `` and the documents
734
+ inside it.
735
+
736
+ - You can run regular queries from the `cli ` (e.g. reading ``marble2 ``):
737
+
738
+ .. code :: bash
739
+
740
+ peer chaincode query -o $ORDERER_IP :7050 -C mychannel -n marbles -c ' {"Args":["readMarble","marble2"]}'
741
+
742
+
743
+ You should see the details of ``marble2 ``:
744
+
745
+ .. code :: bash
746
+
747
+ Query Result: {" color" :" red" ," docType" :" marble" ," name" :" marble2" ," owner" :" jerry" ," size" :50}
748
+
749
+
750
+ Retrieve the history of ``marble1 ``:
751
+
752
+ .. code :: bash
753
+
754
+ peer chaincode query -o $ORDERER_IP :7050 -C mychannel -n marbles -c ' {"Args":["getHistoryForMarble","marble1"]}'
755
+
756
+ You should see the transactions on ``marble1 ``:
757
+
758
+ .. code :: bash
759
+
760
+ Query Result: [{" TxId" :" 1c3d3caf124c89f91a4c0f353723ac736c58155325f02890adebaa15e16e6464" , " Value" :{" docType" :" marble" ," name" :" marble1" ," color" :" blue" ," size" :35," owner" :" tom" }},{" TxId" :" 755d55c281889eaeebf405586f9e25d71d36eb3d35420af833a20a2f53a3eefd" , " Value" :{" docType" :" marble" ," name" :" marble1" ," color" :" blue" ," size" :35," owner" :" jerry" }},{" TxId" :" 819451032d813dde6247f85e56a89262555e04f14788ee33e28b232eef36d98f" , " Value" :}]
761
+
762
+
763
+
764
+ - You can also perform rich queries on the data content, such as querying marble fields by owner ``jerry ``:
765
+
766
+ .. code :: bash
767
+
768
+ peer chaincode query -o $ORDERER_IP :7050 -C myc1 -n marbles -c ' {"Args":["queryMarblesByOwner","jerry"]}'
769
+
770
+ The output should display the two marbles owned by ``jerry ``:
771
+
772
+ .. code :: bash
773
+
774
+ Query Result: [{" Key" :" marble2" , " Record" :{" color" :" red" ," docType" :" marble" ," name" :" marble2" ," owner" :" jerry" ," size" :50}},{" Key" :" marble3" , " Record" :{" color" :" blue" ," docType" :" marble" ," name" :" marble3" ," owner" :" jerry" ," size" :70}}]
775
+
776
+ Query by field ``owner `` where the value is ``jerry ``:
777
+
778
+ .. code :: bash
779
+
780
+ peer chaincode query -o $ORDERER_IP :7050 -C myc1 -n marbles -c ' {"Args":["queryMarbles","{\"selector\":{\"owner\":\"jerry\"}}"]}'
781
+
782
+ The output should display:
783
+
784
+ .. code :: bash
785
+
786
+ Query Result: [{" Key" :" marble2" , " Record" :{" color" :" red" ," docType" :" marble" ," name" :" marble2" ," owner" :" jerry" ," size" :50}},{" Key" :" marble3" , " Record" :{" color" :" blue" ," docType" :" marble" ," name" :" marble3" ," owner" :" jerry" ," size" :70}}]
787
+
788
+ A Note on Data Persistence
789
+ --------------------------
790
+
791
+ If data persistence is desired on the peer container or the CouchDB container,
792
+ one option is to mount a directory in the docker-host into a relevant directory
793
+ in the container. For example, you may add the following two lines in
794
+ the peer container specification in the ``docker-compose.yaml `` file:
795
+
796
+ .. code :: bash
797
+
798
+ volumes:
799
+ - /var/hyperledger/peer0:/var/hyperledger/production
800
+
801
+
802
+ For the CouchDB container, you may add the following two lines in the CouchDB
803
+ container specification:
804
+
805
+ .. code :: bash
806
+
807
+ volumes:
808
+ - /var/hyperledger/couchdb0:/opt/couchdb/data
809
+
810
+
652
811
Troubleshooting
653
812
---------------
654
813
0 commit comments