1

我有一个关于使用 blob 类型作为分区键的问题。我使用它,因为我需要保存哈希值。(哈希值返回二进制数据。通常为十六进制。)

我尝试使用 gocql 进行选择查询,但是失败并出现以下错误。有没有办法为这种查询获得成功的结果?

非常感谢您的建议!

-- 结果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: 0xd03dde1610d6df87a9061928412ceddadf2615825 /03/22 10:03:17 无法将 blob 解组为 *[20]uint8

-- 选择.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)


}

-- 表定义 --

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

1 回答 1

0

我通过更改变量的类型解决了这个问题:returned_hash。

  • Returned_hash(用于存储返回结果的变量)应该是 []byte。

我理解如下。

  • marshal:将代码中给出的数据转换为 cassandra 可以处理的类型。
  • unmarshal:将 cassandra 返回的数据转换回 golang-code 可以处理的类型。
  • 最初的错误意味着后一种模式并不顺利。所以returned_hash的类型一定是错误的。

如我错了请纠正我。谢谢。

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 \n", returned_hash, column1)

}
于 2018-03-23T07:03:29.300 回答