接受在Golang中使用Cassandra的Set数据类型。

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

Accepting Cassandra's Set datatype in golang

问题

我在Cassandra中有一个列,类型为set。我想使用gocql在Golang代码中接收该列。在接收时,接收整个set的变量应该是什么数据类型?

例如:

err = session.Query("select product_list from category where category_id=?", key).Scan(&productIdList)

Product_list的类型是set。在Golang中,productIdList应该是什么数据类型?

英文:

I have a column in cassandra which is of type set.
Usig gocql I want to accept that column in golang code.So while accepting what should be the datatype of the variable in which the entire set is accepted.

e.g:

err = session.Query("select product_list from category where category_id=?",key).Scan(&productIdList)

Product_list is of type set.So what should be the data type of productIdList in golang?

答案1

得分: 2

看起来gocql默认使用切片或数组来表示Cassandra的set类型。

如果这不是你想要的,它允许你使用gocql.Marshaller和gocql.Unmarshaller接口来定义自己的类型。

例如:

type Set map[string]bool

func (s Set) UnmarshalCQL(info TypeInfo, data []byte) error {
  /* 解析你的set数据到Go map的代码,从CQL结果中 */
}

func (s Set) MarshalCQL(info TypeInfo) ([]byte, error) {
  /* 将你的set数据从Go map序列化为CQL可用的代码 */
}

这意味着你需要自己编写set输出解析的代码。

英文:

It looks like gocql uses a slice or array by default for Cassandra's set type.

If that's not what you want it does let you define one of your own using the gocql.Marshaller and gocql.Unmarshaller interfaces.

So for example:

type Set map[string]bool

func (s Set) UnmarshalCQL(info TypeInfo, data []byte) error {
  /* code to unmarshal your set into a Go map. from cql results */
}

func (s Set) MarshalCQL(info TypeInfo) ([]byte, error) {
  /* code to marshal your set out of a go map for cql to use */
}

This does however mean that you'll have to write the set output parsing yourself.

huangapple
  • 本文由 发表于 2014年3月11日 09:13:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/22314564.html
匿名

发表评论

匿名网友

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

确定