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 Lingo线性规划模型怎么搭建
  • ¥15 关于#python#的问题,请各位专家解答!区间型正向化
  • ¥15 unity从3D升级到urp管线,打包ab包后,材质全部变紫色
  • ¥50 comsol温度场仿真无法模拟微米级激光光斑
  • ¥15 上传图片时提交的存储类型
  • ¥15 VB.NET如何绘制倾斜的椭圆
  • ¥15 arbotix没有/cmd_vel话题
  • ¥15 odoo17的分包重新供应路线如何设置?可从销售订单中实时直接触发采购订单或相关单据
  • ¥15 用C语言怎么判断字符串的输入是否符合设定?