dongpa5277 2015-10-11 18:18
浏览 101
已采纳

gocb:批量获取操作示例

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)
  • 写回答

2条回答 默认 最新

  • doumen5895 2015-10-12 15:39
    关注

    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)
            }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题