dsgixl5195 2019-03-19 17:56
浏览 52

GoCQL是否支持Cassandra集和列表?

I'm trying to insert a Go slice into a Cassandra set or list but bind doesn't seem to understand how to do it. This code works fine, but requires me to build the query string manually:

package main

import (
    "github.com/gocql/gocql"
    "github.com/scylladb/gocqlx"
    "github.com/scylladb/gocqlx/qb"
)

// Database setup code not shown

type SetTest struct {
    ID     string   `cql:"id"`
    myset  []string `cql:"myset"`
    mylist []string `cql:"mylist"`
}

var st SetTest

st.ID = "This is the ID"
st.myset = append(st.myset, "set2", "set1", "set3")
st.mylist = append(st.mylist, "list2", "list1", "list3")

if err := cqlSession.Query("insert into test.set_test (id, myset, mylist) values ('This is the ID', {'set2', 'set1', 'set3'}, ['mylist2', 'mylist1', 'mylist3'])").Exec(); err != nil {
    log.Fatal("set_test query values failed:", err)
}

I was hoping to use insert and bind instead:

    stmt, names := qb.Insert("test.set_test").Columns("id", "myset", "mylist").ToCql()
q := gocqlx.Query(cqlSession.Query(stmt), names).BindStruct(st)

if err := q.ExecRelease(); err != nil {
    fmt.Println("set_test insert failed:", err)
}

This returns bind error: could not find name "myset" in main.SetTest{ID:"This is the ID", myset:[]string{"set2", "set1", "set3"}, mylist:[]string{"list2", "list1", "list3"}} and map[string]interface {}(nil)

I also tried binding the individual fields of the struct:

    if err := cqlSession.Query("insert into set_test (id, myset, mylist)").Bind(st.ID, st.myset, st.mylist).Exec(); err != nil {
    fmt.Println("set_test query bind(st.id) failed:", err)
}

This returns line 0:-1 mismatched input '' expecting K_VALUES q = '[query statement="INSERT INTO test.set_test (id,myset,mylist) VALUES (?,?,?) " values=[] consistency=QUORUM]'

For completeness, here is the table description from cqlsh:

CREATE TABLE test.set_test (
id text PRIMARY KEY,
mylist list<text>,
myset set<text>
) WITH bloom_filter_fp_chance = 0.01
AND caching = {'keys': 'ALL', 'rows_per_partition': 'NONE'}
AND comment = ''
AND compaction = {'class':'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy', 'max_threshold': '32', 'min_threshold': '4'}
AND compression = {'chunk_length_in_kb': '64', 'class': 'org.apache.cassandra.io.compress.LZ4Compressor'}
AND crc_check_chance = 1.0
AND dclocal_read_repair_chance = 0.1
AND default_time_to_live = 0
AND gc_grace_seconds = 864000
AND max_index_interval = 2048
AND memtable_flush_period_in_ms = 0
AND min_index_interval = 128
AND read_repair_chance = 0.0
AND speculative_retry = '99PERCENTILE';
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 测距传感器数据手册i2c
    • ¥15 RPA正常跑,cmd输入cookies跑不出来
    • ¥15 求帮我调试一下freefem代码
    • ¥15 matlab代码解决,怎么运行
    • ¥15 R语言Rstudio突然无法启动
    • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
    • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
    • ¥15 用windows做服务的同志有吗
    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法