在使用gocql驱动程序将字节切片存储在Cassandra中。

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

Storing slice of bytes in Cassandra using gocql driver

问题

如何使用gocql驱动程序将字节切片存储为Cassandra的blob类型?

英文:

How to store slice of bytes as cassandra blob type using gocql driver?

答案1

得分: 0

你的问题非常不具体,你没有展示你已经做了什么。

由于我不知道你实际的问题所在,我将只是发布我之前在使用gocql包测试cassandra时编写的WriteRecord函数:

func (cs *cassandra) WriteRecord(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, ", "))
    return cassandraSession.Query(querystring, values...).Exec()
}

如你所见,values 的类型是 interface{}。因此,它们可以是任何类型,包括 []byte 类型。请注意,必须满足以下条件:len(fields) = len(values)

希望这对你成为 golang 专家的旅程有所帮助 在使用gocql驱动程序将字节切片存储在Cassandra中。

英文:

Your question is very unspecific and you do not show us what you have done so far.

Since I don't know where your actual problem lies I will just post my WriteRecord function I wrote a while back when testing cassandra with the gocql package:

func (cs *cassandra) WriteRecord(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, ", "))
	return cassandraSession.Query(querystring, values...).Exec()
}

As you can see the values are of type interface{}. So they can be anything including of type []byte. Note that the following condition must be met: len(fields) = len(values)

I hope this helps you on your journey of becoming a golang expert 在使用gocql驱动程序将字节切片存储在Cassandra中。

huangapple
  • 本文由 发表于 2017年8月8日 16:43:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/45563495.html
匿名

发表评论

匿名网友

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

确定