英文:
gocb: bulk Get operation example
问题
最近发布的gocb库(couchbase的官方Golang客户端)提供了执行批量操作(Get、Delete等)的API。我希望能看到一个完整的批量操作示例,可惜的是,我的Go技能有限,网上也没有相关内容。
我想看到一个代码片段,它能够(1)编译通过,(2)执行多个Get操作,最后(3)成功访问从couchbase返回的值。
以下是在线文档中存在的一些内容:
http://developer.couchbase.com/documentation/server/4.0/sdks/go-beta/bulk-operations.html
下面的代码(执行插入操作)是不够的:我想要Get操作(具体来说,如何查看Get操作结果的内容)。
myBucket, _ := myCluster.OpenBucket("default", "")
var items []gocb.BulkOp
items = append(items, &gocb.InsertOp{Key: "document_name_1", Value: "Hello World 1"})
items = append(items, &gocb.InsertOp{Key: "document_name_2", Value: "Hello World 2"})
err := bucket.Do(items)
英文:
The recently released gocb lib (the official golang client for couchbase) offers API for performing bulk operations (Get, Delete, etc). I would love to see a complete example of such an operation, alas - my go skills are lacking and there's nothing online.
I'd like to see a snippet that (1) compiles and (2) performs multi-get and finally (3) manages to access the values returned from couchbase.
here's what little documentation exists online:
http://developer.couchbase.com/documentation/server/4.0/sdks/go-beta/bulk-operations.html
the following code (which performs insert) is not enough: I want Get (and specifically - how one would review the contents of the get-operation results).
myBucket, _ := myCluster.OpenBucket("default", "")
var items []gocb.BulkOp
items = append(items, &gocb.InsertOp{Key: "document_name_1", Value: "Hello World 1"})
items = append(items, &gocb.InsertOp{Key: "document_name_2", Value: "Hello World 2"})
err := bucket.Do(items)
答案1
得分: 1
只需将*InsertOp
的值替换为*GetOp
的值,然后读取它们的Value
字段:
package main
import "fmt"
import "gopkg.in/couchbaselabs/gocb.v1"
func main() {
getKeys()
}
func getKeys() {
myCluster, err := gocb.Connect("couchbase://<couchbase-address>")
if err != nil {
fmt.Println("cluster error:", err)
}
myBucket, err := myCluster.OpenBucket("Test", "") //区分大小写!
if err != nil {
fmt.Println("bucket error:", err)
}
var items []gocb.BulkOp
items = append(items, &gocb.GetOp{Key: "document_name_1"})
items = append(items, &gocb.GetOp{Key: "document_name_2"})
err = myBucket.Do(items)
if err != nil {
fmt.Println("Do error:", err)
panic(err)
}
for _, g := range items {
//将实例“向下转换”为其具体类型 - GetOp
t := g.(*gocb.GetOp)
fmt.Println(t)
}
}
请注意,这是一个Go语言代码示例,用于使用gocb
库从Couchbase中获取文档。
英文:
Just replace the *InsertOp
values with *GetOp
values, then read their Value
field:
package main
import "fmt"
import "gopkg.in/couchbaselabs/gocb.v1"
func main() {
getKeys()
}
func getKeys() {
myCluster, err := gocb.Connect("couchbase://<couchbase-address>")
if err != nil {
fmt.Println("cluster error:", err)
}
myBucket, err := myCluster.OpenBucket("Test", "") //case sensitive!
if err != nil {
fmt.Println("bucket error:", err)
}
var items []gocb.BulkOp
items = append(items, &gocb.GetOp{Key: "document_name_1"})
items = append(items, &gocb.GetOp{Key: "document_name_2"})
err = myBucket.Do(items)
if err != nil {
fmt.Println("Do error:", err)
panic(err)
}
for _, g := range items {
//"downcast" the instance back to its concrete type - GetOp
t := g.(*gocb.GetOp)
fmt.Println(t)
}
}
答案2
得分: 1
这是一个可工作的版本,但我认为它有点冗长。
package main
import (
"fmt"
"gopkg.in/couchbase/gocb.v1"
)
const (
COUCH_OP_COUNT = 3
)
// 与Couchbase存储进行数据交换
type StorageUrl struct {
Url string
}
func storeThings(b *gocb.Bucket) {
var ops []gocb.BulkOp
for i := 0; i < COUCH_OP_COUNT; i++ {
k := fmt.Sprintf("key_%v", i)
ops = append(ops, &gocb.UpsertOp{
Key: k,
Value: StorageUrl{Url: fmt.Sprintf("http://www.i-%v.com", i)},
})
}
b.Do(ops)
}
func fetchThings(b *gocb.Bucket) {
var err error
var ops []gocb.BulkOp
var results []interface{}
for i := 0; i < COUCH_OP_COUNT; i++ {
k := fmt.Sprintf("key_%v", i)
results = append(results, &StorageUrl{})
ops = append(ops, &gocb.GetOp{
Key: k,
Value: results[i],
})
}
err = b.Do(ops)
if err != nil {
fmt.Println(err)
return
}
for _, op := range ops {
getOp := op.(*gocb.GetOp)
v := getOp.Value.(*StorageUrl)
fmt.Println(v)
}
}
func main() {
cluster, err := gocb.Connect("couchbase://127.0.0.1")
if err != nil {
fmt.Println(err)
return
}
bucket, err := cluster.OpenBucket("test", "")
if err != nil {
fmt.Println(err)
return
}
storeThings(bucket)
fetchThings(bucket)
}
希望对你有帮助!
英文:
This is a working version, but I think it's quite verbose
package main
import (
"fmt"
"gopkg.in/couchbase/gocb.v1"
)
const (
COUCH_OP_COUNT = 3
)
// data interchange with Couchbase store
type StorageUrl struct {
Url string
}
func storeThings(b *gocb.Bucket) {
var ops []gocb.BulkOp
for i := 0; i < COUCH_OP_COUNT; i++ {
k := fmt.Sprintf("key_%v", i)
ops = append(ops, &gocb.UpsertOp{
Key: k,
Value: StorageUrl{Url: fmt.Sprintf("http://www.i-%v.com", i)},
})
}
b.Do(ops)
}
func fetchThings(b *gocb.Bucket) {
var err error
var ops []gocb.BulkOp
var results []interface{}
for i := 0; i < COUCH_OP_COUNT; i++ {
k := fmt.Sprintf("key_%v", i)
results = append(results, &StorageUrl{})
ops = append(ops, &gocb.GetOp{
Key: k,
Value: results[i],
})
}
err = b.Do(ops)
if err != nil {
fmt.Println(err)
return
}
for _, op := range ops {
getOp := op.(*gocb.GetOp)
v := getOp.Value.(*StorageUrl)
fmt.Println(v)
}
}
func main() {
cluster, err := gocb.Connect("couchbase://127.0.0.1")
if err != nil {
fmt.Println(err)
return
}
bucket, err := cluster.OpenBucket("test", "")
if err != nil {
fmt.Println(err)
return
}
storeThings(bucket)
fetchThings(bucket)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论