为 Hyperledger 使用无模式数据 Fabric/Composer?
using schemaless data for Hyperledger Fabric/Composer?
我需要将来自 json 个文件的数据存储在 hyperledger 中。数据是模式。 Hyperledger Fabric 使用存储无模式数据的 Couchdb。
稍后需要查询数据
但是我不确定 Fabric 是否可以处理无模式数据,因为示例项目中的资产定义(在大理石项目中)
// ----- Marbles ----- //
type Marble struct {
ObjectType string `json:"docType"` //field for couchdb
Id string `json:"id"` //the fieldtags are needed to keep case from bouncing around
Color string `json:"color"`
Size int `json:"size"` //size in mm of marble
Owner OwnerRelation `json:"owner"`
}
我知道在 Composer 中你不能存储无模式数据,因为你必须使用资产。
好的,所以建模语言为业务网络提供了一种结构(资产、参与者和交易的建模结构 类 等),因为它们是业务 network/smart 合同的重要特征。 Composer 查询语言提供 JSON 数据的检索,这可以是一种搜索模式,包括包含无模式结构(它只是一天结束时的数据) - 下面的示例。尽管业务网络使用模型驱动结构(出于所述原因),但这并不意味着您不能存储无模式 JSON(如您上面所问)并稍后在 Composer 中查询它 - 从某种意义上说,您可以存储 JSON 资产中的文档(在 Composer 中)- 示例如下所示,它可能不是也可能不是您想要的 - 但它可以处理您可以检索的无模式数据。
假设我有一个名为 SampleAsset
的资产,它被定义为:
asset SampleAsset identified by assetId {
o String assetId
o String[] JSONattributes
}
以及以下资产:
资产 1:
{
"$class": "org.example.basic.SampleAsset",
"assetId": "1",
"JSONattributes": [
"element1:'/node/attribute/address/DE/NRW/Aeechen/samplestreet/100/1/1','urls':'http://localhost:/web/path1','control_int_array': '[1,2, 3, 4]':'array_of_paths': ['http://localhost:5001', 'http://localhost:5002]'",
"element2:'/node/attribute/address/DE/NRW/boeblingen/samplestreet/100/1/1','urls':'http://localhost:/web/path2','control_int_array': '[4,5, 6, 7]','array_of_paths': ['http://localhost:5002', 'http://localhost:5003]'",
"\"element3\":\"/node/attribute/address/DE/NRW/bielefeld/samplestreet/100/1/1\",\"urls\":\"http://localhost:/web/path3\",\"control_int_array\": \"[9,10, 11, 12]\",\"array_of_paths\": \"['http://localhost:9002', 'http://localhost:10003]\" "
]
}
资产 2:
{
"$class": "org.example.basic.SampleAsset",
"assetId": "22",
"JSONattributes": [
"element",
"element1",
"element2:'/node/attribute/address/DE/NRW/boeblingen/samplestreet/100/1/1','urls':'http://localhost:/web/path2','control_int_array': '[4,5, 6, 7]','array_of_paths': ['http://localhost:5002', 'http://localhost:5003]'",
"'element3':'/node/attribute/address/DE/NRW/Aachen/samplestreet/100/1/1','urls':'http://localhost:/web/path1','control_int_array': '[1,2, 3, 4]':'array_of_paths': ['http://localhost:5001', 'http://localhost:5002]'"
]
}
你可以看到我没有定义任何真正的结构来存储这个 JSON 元数据。
如果我有一个查询:
query containsJSONsgl {
description: "test singular"
statement:
SELECT org.example.basic.SampleAsset
WHERE ( JSONattributes CONTAINS "element2:'/node/attribute/address/DE/NRW/boeblingen/samplestreet/100/1/1','urls':'http://localhost:/web/path2','control_int_array': '[4,5, 6, 7]','array_of_paths': ['http://localhost:5002', 'http://localhost:5003]'" )
}
或
query containsJSONmulti {
description: "test multi"
statement:
SELECT org.example.basic.SampleAsset
WHERE ( JSONattributes CONTAINS ["element2:'/node/attribute/address/DE/NRW/boeblingen/samplestreet/100/1/1','urls':'http://localhost:/web/path2','control_int_array': '[4,5, 6, 7]','array_of_paths': ['http://localhost:5002', 'http://localhost:5003]'", "element1:'/node/attribute/address/DE/NRW/Aeechen/samplestreet/100/1/1','urls':'http://localhost:/web/path1','control_int_array': '[1,2, 3, 4]':'array_of_paths': ['http://localhost:5001', 'http://localhost:5002]'"] )
}
我可以 运行 如下查询以获得我想要的结果 - 显然我在 WHERE 子句中使用了上面的文字,但你明白了
let result = await query('containsJSONsgl');
for (var n = 0; n < result.length; n++) {
console.log("Identifier is " + result[n].getIdentifier() );
console.log("JSON data is " + result[n].JSONattributes);
}
当然,您还可以使用通常的 JSON 方法(字符串化、解析等)。
我需要将来自 json 个文件的数据存储在 hyperledger 中。数据是模式。 Hyperledger Fabric 使用存储无模式数据的 Couchdb。 稍后需要查询数据
但是我不确定 Fabric 是否可以处理无模式数据,因为示例项目中的资产定义(在大理石项目中)
// ----- Marbles ----- //
type Marble struct {
ObjectType string `json:"docType"` //field for couchdb
Id string `json:"id"` //the fieldtags are needed to keep case from bouncing around
Color string `json:"color"`
Size int `json:"size"` //size in mm of marble
Owner OwnerRelation `json:"owner"`
}
我知道在 Composer 中你不能存储无模式数据,因为你必须使用资产。
好的,所以建模语言为业务网络提供了一种结构(资产、参与者和交易的建模结构 类 等),因为它们是业务 network/smart 合同的重要特征。 Composer 查询语言提供 JSON 数据的检索,这可以是一种搜索模式,包括包含无模式结构(它只是一天结束时的数据) - 下面的示例。尽管业务网络使用模型驱动结构(出于所述原因),但这并不意味着您不能存储无模式 JSON(如您上面所问)并稍后在 Composer 中查询它 - 从某种意义上说,您可以存储 JSON 资产中的文档(在 Composer 中)- 示例如下所示,它可能不是也可能不是您想要的 - 但它可以处理您可以检索的无模式数据。
假设我有一个名为 SampleAsset
的资产,它被定义为:
asset SampleAsset identified by assetId {
o String assetId
o String[] JSONattributes
}
以及以下资产:
资产 1:
{
"$class": "org.example.basic.SampleAsset",
"assetId": "1",
"JSONattributes": [
"element1:'/node/attribute/address/DE/NRW/Aeechen/samplestreet/100/1/1','urls':'http://localhost:/web/path1','control_int_array': '[1,2, 3, 4]':'array_of_paths': ['http://localhost:5001', 'http://localhost:5002]'",
"element2:'/node/attribute/address/DE/NRW/boeblingen/samplestreet/100/1/1','urls':'http://localhost:/web/path2','control_int_array': '[4,5, 6, 7]','array_of_paths': ['http://localhost:5002', 'http://localhost:5003]'",
"\"element3\":\"/node/attribute/address/DE/NRW/bielefeld/samplestreet/100/1/1\",\"urls\":\"http://localhost:/web/path3\",\"control_int_array\": \"[9,10, 11, 12]\",\"array_of_paths\": \"['http://localhost:9002', 'http://localhost:10003]\" "
]
}
资产 2:
{
"$class": "org.example.basic.SampleAsset",
"assetId": "22",
"JSONattributes": [
"element",
"element1",
"element2:'/node/attribute/address/DE/NRW/boeblingen/samplestreet/100/1/1','urls':'http://localhost:/web/path2','control_int_array': '[4,5, 6, 7]','array_of_paths': ['http://localhost:5002', 'http://localhost:5003]'",
"'element3':'/node/attribute/address/DE/NRW/Aachen/samplestreet/100/1/1','urls':'http://localhost:/web/path1','control_int_array': '[1,2, 3, 4]':'array_of_paths': ['http://localhost:5001', 'http://localhost:5002]'"
]
}
你可以看到我没有定义任何真正的结构来存储这个 JSON 元数据。
如果我有一个查询:
query containsJSONsgl {
description: "test singular"
statement:
SELECT org.example.basic.SampleAsset
WHERE ( JSONattributes CONTAINS "element2:'/node/attribute/address/DE/NRW/boeblingen/samplestreet/100/1/1','urls':'http://localhost:/web/path2','control_int_array': '[4,5, 6, 7]','array_of_paths': ['http://localhost:5002', 'http://localhost:5003]'" )
}
或
query containsJSONmulti {
description: "test multi"
statement:
SELECT org.example.basic.SampleAsset
WHERE ( JSONattributes CONTAINS ["element2:'/node/attribute/address/DE/NRW/boeblingen/samplestreet/100/1/1','urls':'http://localhost:/web/path2','control_int_array': '[4,5, 6, 7]','array_of_paths': ['http://localhost:5002', 'http://localhost:5003]'", "element1:'/node/attribute/address/DE/NRW/Aeechen/samplestreet/100/1/1','urls':'http://localhost:/web/path1','control_int_array': '[1,2, 3, 4]':'array_of_paths': ['http://localhost:5001', 'http://localhost:5002]'"] )
}
我可以 运行 如下查询以获得我想要的结果 - 显然我在 WHERE 子句中使用了上面的文字,但你明白了
let result = await query('containsJSONsgl');
for (var n = 0; n < result.length; n++) {
console.log("Identifier is " + result[n].getIdentifier() );
console.log("JSON data is " + result[n].JSONattributes);
}
当然,您还可以使用通常的 JSON 方法(字符串化、解析等)。