如何为cassandra golang形成动态插入查询
How to form dynamic insert query for cassandra golang
我一直在尝试使用 gocql 驱动程序在 golang 中为 cassandra 创建一个动态查询,这是我目前尝试的方法
func WriteRecord(session gocql.Session, insertstring string, table string, fields []string, values ...interface{}) error {
var placeholder []string
for range fields {
placeholder = append(placeholder, "?")
}
querystring := fmt.Sprintf(insertstring, table, strings.Join(fields, ", "), strings.Join(placeholder, ", "))
fmt.Println(querystring)
return session.Query(querystring, values...).Exec()
}
并在此
中调用此方法
func writeData(session gocql.Session) {
fields := []string{
"id",
"message",
}
for i := 1; i <= 10; i++ {
/*
if err := session.Query(
"INSERT INTO example_keyspace.example_go (id, message) VALUES (?, ?)", i, "Hello from golang!",
).Exec(); err != nil {
log.Fatal(err)
}
*/
insertString := "INSERT INTO example_keyspace.%s(%s,%s) VALUES (%s,%s)"
err := WriteRecord(session, insertString, "kafka", fields, i, "hey kafka")
if err != nil {
log.Fatal(err)
}
}
}
它给了我这个输出
INSERT INTO example_keyspace.kafka(id, message,?, ?) VALUES
(%!s(MISSING),%!s(MISSING))
如何解决这个问题,我不确定我哪里做错了
您几乎是正确的,只是对格式化的 insertstring 进行了一些小的修改,见下文
func WriteRecord(session gocql.Session, insertstring string, table string, fields []string, values ...interface{}) error {
var placeholder []string
for range values {
placeholder = append(placeholder, "?")
}
querystring := fmt.Sprintf(insertstring, table, strings.Join(fields, ", "), strings.Join(placeholder, ", "))
fmt.Println(querystring)
return session.Query(querystring, values...).Exec()
}
func writeData(session gocql.Session) {
fields := []string{
"id",
"message",
}
for i := 1; i <= 10; i++ {
/*
if err := session.Query(
"INSERT INTO example_keyspace.example_go (id, message) VALUES (?, ?)", i, "Hello from golang!",
).Exec(); err != nil {
log.Fatal(err)
}
*/
insertString := "INSERT INTO example_keyspace.%s(%s) VALUES (%s)"
err := WriteRecord(session, insertString, "kafka", fields, i, "hey kafka") // Just remove extra %s as you are joining the string
if err != nil {
log.Fatal(err)
}
}
}
您将获得的最终 insertstring 输出是
INSERT INTO example_keyspace.kafka(id, message) VALUES (?, ?)
并且按照这一行
return session.Query(querystring, values...).Exec() // the values will be passed
希望对您有所帮助
我一直在尝试使用 gocql 驱动程序在 golang 中为 cassandra 创建一个动态查询,这是我目前尝试的方法
func WriteRecord(session gocql.Session, insertstring string, table string, fields []string, values ...interface{}) error {
var placeholder []string
for range fields {
placeholder = append(placeholder, "?")
}
querystring := fmt.Sprintf(insertstring, table, strings.Join(fields, ", "), strings.Join(placeholder, ", "))
fmt.Println(querystring)
return session.Query(querystring, values...).Exec()
}
并在此
中调用此方法func writeData(session gocql.Session) {
fields := []string{
"id",
"message",
}
for i := 1; i <= 10; i++ {
/*
if err := session.Query(
"INSERT INTO example_keyspace.example_go (id, message) VALUES (?, ?)", i, "Hello from golang!",
).Exec(); err != nil {
log.Fatal(err)
}
*/
insertString := "INSERT INTO example_keyspace.%s(%s,%s) VALUES (%s,%s)"
err := WriteRecord(session, insertString, "kafka", fields, i, "hey kafka")
if err != nil {
log.Fatal(err)
}
}
}
它给了我这个输出
INSERT INTO example_keyspace.kafka(id, message,?, ?) VALUES (%!s(MISSING),%!s(MISSING))
如何解决这个问题,我不确定我哪里做错了
您几乎是正确的,只是对格式化的 insertstring 进行了一些小的修改,见下文
func WriteRecord(session gocql.Session, insertstring string, table string, fields []string, values ...interface{}) error {
var placeholder []string
for range values {
placeholder = append(placeholder, "?")
}
querystring := fmt.Sprintf(insertstring, table, strings.Join(fields, ", "), strings.Join(placeholder, ", "))
fmt.Println(querystring)
return session.Query(querystring, values...).Exec()
}
func writeData(session gocql.Session) {
fields := []string{
"id",
"message",
}
for i := 1; i <= 10; i++ {
/*
if err := session.Query(
"INSERT INTO example_keyspace.example_go (id, message) VALUES (?, ?)", i, "Hello from golang!",
).Exec(); err != nil {
log.Fatal(err)
}
*/
insertString := "INSERT INTO example_keyspace.%s(%s) VALUES (%s)"
err := WriteRecord(session, insertString, "kafka", fields, i, "hey kafka") // Just remove extra %s as you are joining the string
if err != nil {
log.Fatal(err)
}
}
}
您将获得的最终 insertstring 输出是
INSERT INTO example_keyspace.kafka(id, message) VALUES (?, ?)
并且按照这一行
return session.Query(querystring, values...).Exec() // the values will be passed
希望对您有所帮助