How to do INSERT IF NOT EXIST in gocql

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

How to do INSERT IF NOT EXIST in gocql

问题

我一直在阅读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执行轻量级事务即包含IF子句的UPDATE或INSERT语句)。如果事务因为现有值不匹配而失败先前的值将存储在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)

我得到以下错误:
Failed parsing statement: [INSERT INTO tweet (timeline, id, text) VALUES (?, ?, ?) IF NOT EXIST] reason: ArrayIndexOutOfBoundsException -1

所以我的问题是:

  1. 如何在gocql中实际执行INSERT IF NOT EXIST操作?你们能给我一个例子吗?
  2. 如何正确使用ScanCAS?
  3. MapScanCAS和ScanCAS有什么区别?我不明白作者在这里所说的列不匹配是什么意思。
  4. 除了godoc页面之外,有没有其他解释gocql的好网页?
英文:

I've been reading http://godoc.org/github.com/gocql/gocql
Yet I don't understand how to do INSERT -- IF NOT EXIST with gocql. <br/>
It stated that <br/>

>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.

When I run

cluster := gocql.NewCluster(&quot;127.0.0.1&quot;)
cluster.Keyspace = &quot;example&quot;
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`,
    &quot;hermano&quot;, gocql.TimeUUID(), &quot;good night&quot;).
    ScanCAS(); if err != nil {
    log.Println(err)
}
fmt.Println(timeline, id, text)
fmt.Printf(&quot;%+v\n&quot;, isTrue)
fmt.Printf(&quot;%+v\n&quot;, inserted)
fmt.Printf(&quot;%+v\n&quot;, mapQ)

I get: <br/>
Failed parsing statement: [INSERT INTO tweet (timeline, id, text) VALUES (?, ?, ?) IF NOT EXIST] reason: ArrayIndexOutOfBoundsException -1

So my question is: <br/>

  1. How to actually do INSERT IF NOT EXIST in gocql? Can you guys give me any example?<br/>
  2. How to do proper ScanCAS?<br/>
  3. What makes MapScanCAS and ScanCAS different? I don't understand what column mismatching are the author is talking about<br/>
  4. Are there any good page that explains gocql beside its godoc page?
    <br/>

答案1

得分: 1

我的理解:

> 如何在gocql中实际执行INSERT IF NOT EXIST?你们能给我一个例子吗?

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

> 如何正确执行ScanCAS?

与问题1类似。

> MapScanCAS和ScanCAS有什么区别?我不明白作者所说的列不匹配是什么意思。

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

英文:

My understanding:

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

Since the LWT you are executing needs to read data from Cassandra first and decide if the value should be inserted/updated, ScanCAS will return 1) whether the change of this LWT is applied, 2) if not applied, ScanCAS will return the existing value(s) in Cassandra. So in ScanCAS you should try to read the same type of data you are inserting.

> How to do proper ScanCAS?

Similar to question 1.

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

I think the author means that it is easy to mess up the order of scanning the returned values/columns, so you could use MapScanCAS to avoid this issue

答案2

得分: 0

看起来是一个拼写错误,你应该使用"IF NOT EXISTS"。

英文:

looks like a typo, you should use
"IF NOT EXISTS"

huangapple
  • 本文由 发表于 2014年11月11日 00:07:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/26847930.html
匿名

发表评论

匿名网友

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

确定