我正在尝试将Go切片插入Cassandra集或列表中,但bind似乎不知道该怎么做。这段代码可以正常工作,但是需要我手动构建查询字符串:
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)
}
我希望改为使用插入和绑定:
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)
}
这将返回 绑定错误:在main.SetTest {ID:“ This is the ID”,myset:[] string {“ set2”,“ set1”,“中找不到名称“ myset” set3“},mylist:[] string {” list2“,” list1“,” list3“}}}和map [string] interface {}(无)
我还尝试绑定该结构的各个字段:
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)
}
这将返回 第0:-1行不匹配的输入”,期望K_VALUES q ='[query statement =“ INSERT INTO test.set_test(id,myset,mylist)VALUES(?,?,?)” values = []一致性= QUORUM]'
为完整起见,这是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';