dtmjl4427 2018-03-23 06:42
浏览 53
已采纳

gocql无法将blob解组为* [20] uint8

I have a question related to using blob type as partition key. I use it, as I need to save hash value. (hash value returns binary data. usually as hexadecimal.)

I tried a select query with gocql, however it failed with following error. Is there any way to get a successful result for this kind of query?

Your advice highly appreciated!!

-- result hash_value: [208 61 222 22 16 214 223 135 169 6 25 65 44 237 166 229 50 5 40 221] / hash_value: ?=??߇?A,???2(? / hash_value: 0xd03dde1610d6df87a90619412ceda6e5320528dd string 2018/03/22 10:03:17 can not unmarshal blob into *[20]uint8

-- select.go

package main


import (
  "fmt"
  "log"
  "crypto/sha1"
  "reflect"
  "github.com/gocql/gocql"
)


func main() {
  cluster := gocql.NewCluster("10.0.0.1")
  cluster.Keyspace = "ks"
  cluster.Consistency = gocql.Quorum
  cluster.ProtoVersion = 4
  cluster.Authenticator = gocql.PasswordAuthenticator{
    Username: "cassandra",
    Password: "cassandra",
  }


  session, _ := cluster.CreateSession()
  defer session.Close()


  text := "text before hashed"
  data := []byte(text)
  hash_value := sha1.Sum(data)
  hexa_string := fmt.Sprintf("0x%x", hash_value)


  fmt.Println("hash_value: ", hash_value)
  fmt.Println(" / string: ", string(hash_value[:]))
  fmt.Println(" / column1: ", hexa_string)
  fmt.Println(reflect.TypeOf(hexa_string))


  // *** select  ***
  var column1 int
  returned_hash := sha1.Sum(data)


  //if err := session.Query(`SELECT hash_value, column1 FROM sample WHERE hash_value= ? LIMIT 1`,
  //  hexa_string).Consistency(gocql.One).Scan(&returned_hash, &column1); err != nil {
  if err := session.Query(`SELECT hash_value, column1 FROM sample WHERE hash_value=0xd03dde1610d6df87a90619412ceda6e5320528dd`).Consistency(gocql.One).Scan(&returned_hash, &column1); err != nil {
      //fmt.Println(err)
      log.Fatal(err)
  }
  fmt.Println("comment: ", returned_hash, user_id)


}

-- table definition --

CREATE TABLE IF NOT EXISTS ks.samle (
  hash_value blob,
  column1 int,
  ...
  PRIMARY KEY((hash_value), column1)
) WITH CLUSTERING ORDER BY (column1 DESC);
  • 写回答

1条回答 默认 最新

  • doumengbai2031 2018-03-23 07:03
    关注

    I fixed the problem by changing the type of variable: returned_hash.

    • returned_hash (var to store returned result) should be []byte.

    I understands as follows.

    • marshal: convert the data given in the code to a type cassandra can handle.
    • unmarshal: convert the data cassandra returned back to a type golang-code can handle.
    • the original error means latter pattern doesn't go well. so the type of returned_hash must be wrong.

    Correct me if I'm wrong. thanks.

    package main
    
    import (
      "fmt"
      "log"
      "crypto/sha1"
      "reflect"
      "github.com/gocql/gocql"
    )
    
    func main() {
      cluster := gocql.NewCluster("127.0.0.1")
      cluster.Keyspace = "browser"
      cluster.Consistency = gocql.Quorum
      //cluster.ProtoVersion = 4
      //cluster.Authenticator = gocql.PasswordAuthenticator{
      //  Username: "cassandra",
      //  Password: "cassandra",
      //}
    
      session, _ := cluster.CreateSession()
      defer session.Close()
    
      text := "text before hashed"
      data := []byte(text)
      hash_value := sha1.Sum(data)
      hexa_string := fmt.Sprintf("0x%x", hash_value)
    
      fmt.Println("hash_value: ", hash_value)
      fmt.Println(" / string(hash_value): ", string(hash_value[:]))
      fmt.Println(" / hexa(hash_value): ", hexa_string)
      fmt.Println(reflect.TypeOf(hexa_string))
    
      // *** select  ***
      var column1 int
      //returned_hash := sha1.Sum(data)
      //var returned_hash *[20]uint8
      var returned_hash []byte
    
      if err := session.Query(`SELECT hash_value, column1 FROM sample WHERE hash_value=? LIMIT 1`,
      hash_value[:]).Consistency(gocql.One).Scan(&returned_hash, &column1); err != nil {
      //if err := session.Query(`SELECT hash_value, column1 FROM sample WHERE hash_value=0xd03dde1610d6df87a90619412ceda6e5320528dd`).Consistency(gocql.One).Scan(&returned_hash, &column1); err != nil {
          log.Fatal(err)
      }
      fmt.Printf("Returned: %#x %d 
    ", returned_hash, column1)
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 matlab求解平差
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 vue2登录调用后端接口如何实现
  • ¥85 永磁型步进电机PID算法