gpt4 book ai didi

go - 如果在 gocql 中不存在,如何插入

转载 作者:IT王子 更新时间:2023-10-29 02:24:14 26 4
gpt4 key购买 nike

我一直在阅读 http://godoc.org/github.com/gocql/gocql但是我不明白如何使用 gocql 进行 INSERT -- IF NOT EXIST。
它表示

func (*Query) ScanCAS

func (q *Query) ScanCAS(dest ...interface{}) (applied bool, err error)

ScanCAS executes a lightweight transaction (i.e. an UPDATE or INSERT statement containing an IF clause). If the transaction fails because the existing values did not match, the previous values will be stored in dest.

当我运行时

cluster := gocql.NewCluster("127.0.0.1")
cluster.Keyspace = "example"
cluster.Consistency = gocql.Quorum
session, _ := cluster.CreateSession()
defer session.Close()

var mapQ map[string]interface{}
var inserted bool
var id gocql.UUID
var timeline, text string
// insert a tweet
isTrue, err := session.Query(`INSERT INTO tweet (timeline, id, text) VALUES (?, ?, ?) IF NOT EXIST`,
"hermano", gocql.TimeUUID(), "good night").
ScanCAS(); if err != nil {
log.Println(err)
}
fmt.Println(timeline, id, text)
fmt.Printf("%+v\n", isTrue)
fmt.Printf("%+v\n", inserted)
fmt.Printf("%+v\n", mapQ)

我得到:
解析语句失败:[INSERT INTO tweet (timeline, id, text) VALUES (?, ?, ?) IF NOT EXIST] 原因:ArrayIndexOutOfBoundsException -1

所以我的问题是:
1.如果在gocql中不存在,如何实际插入?你们能举个例子吗?
2.如何做正确的ScanCAS?
3. MapScanCAS 和 ScanCAS 有什么不同?我不明白what column mismatching are the author is talking about
4. gocql 的godoc 页面旁边有没有好的页面解释?

最佳答案

我的理解:

How to actually do INSERT IF NOT EXIST in gocql? Can you guys give me any example?

由于您正在执行的 LWT 需要先从 Cassandra 读取数据并决定是否应该插入/更新该值,ScanCAS 将返回 1) 此 LWT 的更改是否应用,2) 如果不应用,ScanCAS 将返回Cassandra 中的现有值。因此,在 ScanCAS 中,您应该尝试读取与您插入的数据类型相同的数据。

How to do proper ScanCAS?

与问题 1 类似。

What makes MapScanCAS and ScanCAS different? I don't understand what column mismatching are the author is talking about

我认为作者的意思是很容易弄乱扫描返回值/列的顺序,所以你可以使用 MapScanCAS 来避免这个问题

关于go - 如果在 gocql 中不存在,如何插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26847930/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com