如何使用部分键值查询表格

huangapple go评论84阅读模式
英文:

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或同时使用RefNumStatus进行查询时,行会被返回。但是,如果我只基于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: &quot;RefNum&quot;,
	Type: shim.ColumnDefinition_STRING, Key: true}
columnTwoTableOneDef := shim.ColumnDefinition{Name: &quot;Amount&quot;,
	Type: shim.ColumnDefinition_STRING, Key: false}
columnThreeTableOneDef := shim.ColumnDefinition{Name: &quot;Status&quot;,
	Type: shim.ColumnDefinition_STRING, Key: true}
columnDefsTableOne = append(columnDefsTableOne, &amp;columnOneTableOneDef)
columnDefsTableOne = append(columnDefsTableOne, &amp;columnTwoTableOneDef)
columnDefsTableOne = append(columnDefsTableOne, &amp;columnThreeTableOneDef)
	return stub.CreateTable(&quot;Recon&quot;, 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) &lt; 1 {
	return nil, errors.New(&quot;Function failed. Must include at least key values&quot;)
}

var columns []shim.Column

col1Val := args[0]
col1 := shim.Column{Value: &amp;shim.Column_String_{String_: col1Val}}
columns = append(columns, col1)

if len(args) &gt; 1 {
	col2Val := args[1]
	col2 := shim.Column{Value: &amp;shim.Column_String_{String_: col2Val}}
	columns = append(columns, col2)
}

rowChannel, err := stub.GetRows(&quot;Recon&quot;, columns)
if err != nil {
	return nil, fmt.Errorf(&quot;Operation failed. %s&quot;, err)
}

var rows []shim.Row
for {
	select {
	case row, ok := &lt;-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(&quot;Operation failed. Error marshaling JSON: %s&quot;, err)
}

return jsonRows, nil

答案1

得分: 1

行键在关系型数据库的意义上并不是真正的键。在内部,使用范围查询来对连接的键集进行操作。因此,查询中的键必须按照建模时的顺序提供。

在Fabric v1.0中,链码API已经更新,使行为更加直观。在v1.0中,您可以建模一个复合键,然后按照建模时的顺序对键的子集进行查询。

要查询不同组合的键,您需要对每个想要查询的唯一组合进行建模。将其视为简单的索引。

在v1.0中,您还可以将数据建模为JSON,并将CouchDB用作状态数据库。在这种情况下,您可以直接查询JSON的任何字段。

在这里的marbles02链码中有这两种数据模式的示例:

https://github.com/hyperledger/fabric/blob/release/examples/chaincode/go/marbles02/marbles_chaincode.go

英文:

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.

huangapple
  • 本文由 发表于 2017年3月9日 15:54:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/42689667.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定