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条)

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能