英文:
How to query table with partial key values
问题
我正在使用Hyperledger(v0.6)表格进行工作,结构如下:
var columnDefsTableOne []*shim.ColumnDefinition
columnOneTableOneDef := shim.ColumnDefinition{Name: "RefNum",
Type: shim.ColumnDefinition_STRING, Key: true}
columnTwoTableOneDef := shim.ColumnDefinition{Name: "Amount",
Type: shim.ColumnDefinition_STRING, Key: false}
columnThreeTableOneDef := shim.ColumnDefinition{Name: "Status",
Type: shim.ColumnDefinition_STRING, Key: true}
columnDefsTableOne = append(columnDefsTableOne, &columnOneTableOneDef)
columnDefsTableOne = append(columnDefsTableOne, &columnTwoTableOneDef)
columnDefsTableOne = append(columnDefsTableOne, &columnThreeTableOneDef)
return stub.CreateTable("Recon", columnDefsTableOne)
当我只使用RefNum或同时使用RefNum和Status进行查询时,行会被返回。但是,如果我只基于Status进行查询,没有返回任何内容。有没有办法实现这个?
以下是查询代码:
if len(args) < 1 {
return nil, errors.New("Function failed. Must include at least key values")
}
var columns []shim.Column
col1Val := args[0]
col1 := shim.Column{Value: &shim.Column_String_{String_: col1Val}}
columns = append(columns, col1)
if len(args) > 1 {
col2Val := args[1]
col2 := shim.Column{Value: &shim.Column_String_{String_: col2Val}}
columns = append(columns, col2)
}
rowChannel, err := stub.GetRows("Recon", columns)
if err != nil {
return nil, fmt.Errorf("Operation failed. %s", err)
}
var rows []shim.Row
for {
select {
case row, ok := <-rowChannel:
if !ok {
rowChannel = nil
} else {
rows = append(rows, row)
}
}
if rowChannel == nil {
break
}
}
jsonRows, err := json.Marshal(rows)
if err != nil {
return nil, fmt.Errorf("Operation failed. Error marshaling JSON: %s", err)
}
return jsonRows, nil
英文:
I am working with hyperledger (v0.6) tables and have a structure as follows;
var columnDefsTableOne []*shim.ColumnDefinition
columnOneTableOneDef := shim.ColumnDefinition{Name: "RefNum",
Type: shim.ColumnDefinition_STRING, Key: true}
columnTwoTableOneDef := shim.ColumnDefinition{Name: "Amount",
Type: shim.ColumnDefinition_STRING, Key: false}
columnThreeTableOneDef := shim.ColumnDefinition{Name: "Status",
Type: shim.ColumnDefinition_STRING, Key: true}
columnDefsTableOne = append(columnDefsTableOne, &columnOneTableOneDef)
columnDefsTableOne = append(columnDefsTableOne, &columnTwoTableOneDef)
columnDefsTableOne = append(columnDefsTableOne, &columnThreeTableOneDef)
return stub.CreateTable("Recon", columnDefsTableOne)
When I query the table with RefNum only or both RefNum & Status, the row is returned. But if I try to query on the basis of Status only, nothing is returned. Is there any way to achieve this?
Below is the query code
if len(args) < 1 {
return nil, errors.New("Function failed. Must include at least key values")
}
var columns []shim.Column
col1Val := args[0]
col1 := shim.Column{Value: &shim.Column_String_{String_: col1Val}}
columns = append(columns, col1)
if len(args) > 1 {
col2Val := args[1]
col2 := shim.Column{Value: &shim.Column_String_{String_: col2Val}}
columns = append(columns, col2)
}
rowChannel, err := stub.GetRows("Recon", columns)
if err != nil {
return nil, fmt.Errorf("Operation failed. %s", err)
}
var rows []shim.Row
for {
select {
case row, ok := <-rowChannel:
if !ok {
rowChannel = nil
} else {
rows = append(rows, row)
}
}
if rowChannel == nil {
break
}
}
jsonRows, err := json.Marshal(rows)
if err != nil {
return nil, fmt.Errorf("Operation failed. Error marshaling JSON: %s", err)
}
return jsonRows, nil
答案1
得分: 1
行键在关系型数据库的意义上并不是真正的键。在内部,使用范围查询来对连接的键集进行操作。因此,查询中的键必须按照建模时的顺序提供。
在Fabric v1.0中,链码API已经更新,使行为更加直观。在v1.0中,您可以建模一个复合键,然后按照建模时的顺序对键的子集进行查询。
要查询不同组合的键,您需要对每个想要查询的唯一组合进行建模。将其视为简单的索引。
在v1.0中,您还可以将数据建模为JSON,并将CouchDB用作状态数据库。在这种情况下,您可以直接查询JSON的任何字段。
在这里的marbles02链码中有这两种数据模式的示例:
英文:
The row keys are not true keys in the sense of a relational database. Internally, range queries are utilized against the concatenated set of keys. Therefore, the keys in the query must be provided in the same order as they were modeled.
The chaincode APIs have been updated in Fabric v1.0 to make the behavior more intuitive. In v1.0 you can model a composite key and then query on a subset of the keys (again, in the order that they were modeled).
To query on different combinations of keys, you would have to model each unique combination that you'd like to query on. Think of these as simple indexes.
In v1.0 you can also model your data as JSON and utilize CouchDB as the state database. In this case you can directly query on any field of the JSON.
There is an example of both data patterns in the marbles02 chaincode here:
https://github.com/hyperledger/fabric/blob/release/examples/chaincode/go/marbles02/marbles_chaincode.go.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论