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 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊