gocb: 批量获取操作示例

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

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 &quot;fmt&quot;
import &quot;gopkg.in/couchbaselabs/gocb.v1&quot;

func main() {
        getKeys()
}

func getKeys() {
        myCluster, err := gocb.Connect(&quot;couchbase://&lt;couchbase-address&gt;&quot;)
        if err != nil {
                fmt.Println(&quot;cluster error:&quot;, err)
        }
        myBucket, err := myCluster.OpenBucket(&quot;Test&quot;, &quot;&quot;) //case sensitive!
        if err != nil {
                fmt.Println(&quot;bucket error:&quot;, err)
        }

        var items []gocb.BulkOp
        items = append(items, &amp;gocb.GetOp{Key: &quot;document_name_1&quot;})
        items = append(items, &amp;gocb.GetOp{Key: &quot;document_name_2&quot;})

        err = myBucket.Do(items)

        if err != nil {
                fmt.Println(&quot;Do error:&quot;, err)
                panic(err)
        }

        for _, g := range items {
                //&quot;downcast&quot; 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 (
&quot;fmt&quot;
&quot;gopkg.in/couchbase/gocb.v1&quot;
)
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 &lt; COUCH_OP_COUNT; i++ {
k := fmt.Sprintf(&quot;key_%v&quot;, i)
ops = append(ops, &amp;gocb.UpsertOp{
Key:   k,
Value: StorageUrl{Url: fmt.Sprintf(&quot;http://www.i-%v.com&quot;, i)},
})
}
b.Do(ops)
}
func fetchThings(b *gocb.Bucket) {
var err error
var ops []gocb.BulkOp
var results []interface{}
for i := 0; i &lt; COUCH_OP_COUNT; i++ {
k := fmt.Sprintf(&quot;key_%v&quot;, i)
results = append(results, &amp;StorageUrl{})
ops = append(ops, &amp;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(&quot;couchbase://127.0.0.1&quot;)
if err != nil {
fmt.Println(err)
return
}
bucket, err := cluster.OpenBucket(&quot;test&quot;, &quot;&quot;)
if err != nil {
fmt.Println(err)
return
}
storeThings(bucket)
fetchThings(bucket)
}

huangapple
  • 本文由 发表于 2015年10月12日 02:18:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/33068478.html
匿名

发表评论

匿名网友

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

确定