当前位置:首页 » 《休闲阅读》 » 正文

fabric Java sdk 获取链码数据的操作-基于test-network的basic链码实操_龙怪神的博客

0 人参与  2022年02月06日 11:26  分类 : 《休闲阅读》  评论

点击全文阅读


前提需要:已搭建好fabric2.x版本环境,有connection.json文件,实现Java sdk 连接上区块链网络。

1.配置connection.json

{
  "name": "basic-network",
  "version": "1.0.0",
  "client": {
    "organization": "Org1",
    "connection": {
      "timeout": {
        "peer": {
          "endorser": "300"
        },
        "orderer": "300"
      }
    }
  },
  "channels": {
    "mychannel": {
      "orderers": [
        "orderer.example.com"
      ],
      "peers": {
        "peer0.org1.example.com": {
          "endorsingPeer": true,
          "chaincodeQuery": true,
          "ledgerQuery": true,
          "eventSource": true
        },
        "peer0.org2.example.com": {
          "endorsingPeer": true,
          "chaincodeQuery": true,
          "ledgerQuery": true,
          "eventSource": true
        }
      }
    }
  },
  "organizations": {
    "Org1": {
      "mspid": "Org1MSP",
      "peers": [
        "peer0.org1.example.com"
      ],
      "certificateAuthorities": [
        "ca-org1"
      ],
      "adminPrivateKeyPEM": {
        "path": "src/main/resources/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/keystore/priv_sk"
      },
      "signedCertPEM": {
        "path": "src/main/resources/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/signcerts/Admin@org1.example.com-cert.pem"
      }
    },
    "Org2": {
      "mspid": "Org2MSP",
      "peers": [
        "peer0.org2.example.com"
      ],
      "certificateAuthorities": [
        "ca-org2"
      ],
      "adminPrivateKeyPEM": {
        "path": "src/main/resources/crypto-config/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp/keystore/priv_sk"
      },
      "signedCertPEM": {
        "path": "src/main/resources/crypto-config/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp/signcerts/Admin@org2.example.com-cert.pem"
      }
    }
  },
  "orderers": {
    "orderer.example.com": {
      "url": "grpcs://你的虚拟机ip:7050",
      "mspid": "OrdererMSP",
      "grpcOptions": {
        "ssl-target-name-override": "orderer.example.com",
        "hostnameOverride": "orderer.example.com"
      },
      "tlsCACerts": {
        "path": "src/main/resources/crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/tls/ca.crt"
      },
      "adminPrivateKeyPEM": {
        "path": "src/main/resources/crypto-config/ordererOrganizations/example.com/users/Admin@example.com/msp/keystore/priv_sk"
      },
      "signedCertPEM": {
        "path": "src/main/resources/crypto-config/ordererOrganizations/example.com/users/Admin@example.com/msp/signcerts/Admin@example.com-cert.pem"
      }
    }
  },
  "peers": {
    "peer0.org1.example.com": {
      "url": "grpcs://你的虚拟机ip:7051",
      "grpcOptions": {
        "ssl-target-name-override": "peer0.org1.example.com",
        "hostnameOverride": "peer0.org1.example.com",
        "request-timeout": 120001
      },
      "tlsCACerts": {
        "path": "src/main/resources/crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt"
      }
    },
    "peer0.org2.example.com": {
      "url": "grpcs://你的虚拟机ip:9051",
      "grpcOptions": {
        "ssl-target-name-override": "peer0.org2.example.com",
        "hostnameOverride": "peer0.org2.example.com",
        "request-timeout": 120001
      },
      "tlsCACerts": {
        "path": "src/main/resources/crypto-config/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt"
      }
    }
  },
  "certificateAuthorities": {
    "ca-org1": {
      "url": "https://你的虚拟机ip:7054",
      "grpcOptions": {
        "verify": true
      },
      "tlsCACerts": {
        "path": "src/main/resources/crypto-config/peerOrganizations/org1.example.com/ca/ca.org1.example.com-cert.pem"
      },
      "registrar": [
        {
          "enrollId": "admin",
          "enrollSecret": "adminpw"
        }
      ]
    },
    "ca-org2": {
      "url": "https://你的虚拟机ip:8054",
      "grpcOptions": {
        "verify": true
      },
      "tlsCACerts": {
        "path": "src/main/resources/crypto-config/peerOrganizations/org2.example.com/ca/ca.org2.example.com-cert.pem"
      },
      "registrar": [
        {
          "enrollId": "admin",
          "enrollSecret": "adminpw"
        }
      ]
    }
  }
}

查看虚拟机ip使用命令ifconfig,选择enw33的ip地址

2.Java sdk demo

// Load an existing wallet holding identities used to access the network.
        Path walletDirectory = Paths.get("wallet");
        Wallet wallet = Wallets.newFileSystemWallet(walletDirectory);

        // Path to a common connection profile describing the network.
        Path networkConfigFile = Paths.get("connection.json");

        // Configure the gateway connection used to access the network.
        Gateway.Builder builder = Gateway.createBuilder()
                .identity(wallet, "user1")
                .networkConfig(networkConfigFile);

        // Create a gateway connection
        try (Gateway gateway = builder.connect()) {

            // Obtain a smart contract deployed on the network.
            Network network = gateway.getNetwork("mychannel");
            Contract contract = network.getContract("basic");

注意我这里安装的链码是basic,还有一种是fabcar,可根据自己的默认安装选择,操作一样,改一下参数名即可。

3.调用链码,获取数据

 byte[] queryAllAssets = contract.evaluateTransaction("GetAllAssets");
System.out.println("--------------");
System.out.println("所有数据:"+new String(queryAllAssets,StandardCharsets.UTF_8));
System.out.println("--------------");

结果显示如下:
在这里插入图片描述
如图,是一整串字符串,接下来我们要想办法把他封装成类的形式
1.先创建一个实体类

@Data
public class Basic {
    private String ID;
    private String color;
    private String size;
    private String owner;
    private String appraisedValue;
}

2.创建下列方法:

 private static void ByteToClassArray(byte[] bytes){
        String str = new String(bytes, StandardCharsets.UTF_8);
        JSONArray jsonArray = JSONArray.parseArray(str);
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject object = jsonArray.getJSONObject(i);
            Basic basic1 = JSONObject.parseObject(object.toJSONString(),Basic.class);
            System.out.println(basic1.toString());
        }
    }

3.使用方法并展示结果:

 byte[] queryAllAssets = contract.evaluateTransaction("GetAllAssets");
System.out.println("--------------");
System.out.println("所有数据:"+new String(queryAllAssets,StandardCharsets.UTF_8));
System.out.println("--------------");
System.out.println("-----将数据处理后的结果");
ByteToClassArray(queryAllAssets);

结果如下:
在这里插入图片描述
如此便可实现对链码数据的处理

3.获取单个数据的处理:

 byte[] queryOneAsset = contract.createTransaction("ReadAsset")
                    .submit("asset6");
System.out.println("读取asset6的内容:"+new String(queryOneAsset, StandardCharsets.UTF_8));
System.out.println("----------------");
System.out.println("将string 封装成 对象");
JSONObject jsonObject = JSON.parseObject(new String(queryOneAsset,StandardCharsets.UTF_8));
            
basic.setAppraisedValue(jsonObject.getString("appraisedValue"));
basic.setColor(jsonObject.getString("color"));
basic.setID(jsonObject.getString("id"));
basic.setOwner(jsonObject.getString("owner"));
basic.setSize(jsonObject.getString("size"));
System.out.println("------------------");
System.out.println("封装之后的单体basic对象:"+basic.toString()+"\t 获得单个属性color: "+basic.getColor());
System.out.println("------------------");

如果如下图:
在这里插入图片描述


点击全文阅读


本文链接:http://zhangshiyu.com/post/34318.html

虚拟机  数据  封装  
<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1