dongxiong2000 2014-11-10 16:07
浏览 78

如果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.
It stated that

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("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
", isTrue)
fmt.Printf("%+v
", inserted)
fmt.Printf("%+v
", mapQ)

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

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

  • 写回答

2条回答 默认 最新

  • duanhan3067 2016-02-03 04:35
    关注

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

    评论

报告相同问题?

悬赏问题

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