将Redigo Pipeline的结果转换为字符串。

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

Convert Redigo Pipeline result to strings

问题

我成功地将多个HGETALL命令进行了流水线处理,但是我无法将它们转换为字符串。

我的示例代码如下:

  1. // 在端口6379上初始化Redis(Redigo)客户端
  2. // 并使用默认地址127.0.0.1/localhost
  3. client, err := redis.Dial("tcp", ":6379")
  4. if err != nil {
  5. panic(err)
  6. }
  7. defer client.Close()
  8. // 初始化流水线
  9. client.Send("MULTI")
  10. // Send将命令写入连接的输出缓冲区
  11. client.Send("HGETALL", "post:1") // 其中"post:1"包含"title 'hi'"
  12. client.Send("HGETALL", "post:2") // 其中"post:1"包含"title 'hello'"
  13. // 执行流水线
  14. pipe_prox, err := client.Do("EXEC")
  15. if err != nil {
  16. panic(err)
  17. }
  18. log.Println(pipe_prox)

只要您能够接受非字符串的结果,这样就可以了。我得到的结果是:

  1. [[[116 105 116 108 101] [104 105]] [[116 105 116 108 101] [104 101 108 108 111]]]

但是我需要的是:

  1. "title" "hi" "title" "hello"

我尝试了以下方法和其他组合:

  1. result, _ := redis.Strings(pipe_prox, err)
  2. log.Println(pipe_prox)

但是我得到的只是:[]

我应该注意到,它可以处理多个HGET key value命令,但这不是我需要的。

我做错了什么?我应该如何将这个"数字映射"转换为字符串?

感谢您的帮助。

英文:

I managed to Pipeline multiple HGETALL commands, but I can't manage to convert them to strings.

My sample code is this:

  1. // Initialize Redis (Redigo) client on port 6379
  2. // and default address 127.0.0.1/localhost
  3. client, err := redis.Dial("tcp", ":6379")
  4. if err != nil {
  5. panic(err)
  6. }
  7. defer client.Close()
  8. // Initialize Pipeline
  9. client.Send("MULTI")
  10. // Send writes the command to the connection's output buffer
  11. client.Send("HGETALL", "post:1") // Where "post:1" contains " title 'hi' "
  12. client.Send("HGETALL", "post:2") // Where "post:1" contains " title 'hello' "
  13. // Execute the Pipeline
  14. pipe_prox, err := client.Do("EXEC")
  15. if err != nil {
  16. panic(err)
  17. }
  18. log.Println(pipe_prox)

It is fine as long as you're comfortable showing non-string results.. What I'm getting is this:

  1. [[[116 105 116 108 101] [104 105]] [[116 105 116 108 101] [104 101 108 108 111]]]

But what I need is:

  1. "title" "hi" "title" "hello"

I've tried the following and other combinations as well:

  1. result, _ := redis.Strings(pipe_prox, err)
  2. log.Println(pipe_prox)

But all I get is: []

I should note that it works with multiple HGET key value commands, but that's not what I need.

What am I doing wrong? How should I do it to convert the "numerical map" to strings?

Thanks for any help

答案1

得分: 5

每个HGETALL返回自己的一系列值,这些值需要转换为字符串,而管道返回的是这些值的一系列。首先使用通用的redis.Values来分解这个外部结构,然后可以解析内部的切片。

// 执行管道
pipe_prox, err := redis.Values(client.Do("EXEC"))

if err != nil {
panic(err)
}

for _, v := range pipe_prox {
s, err := redis.Strings(v, nil)
if err != nil {
fmt.Println("不是批量字符串响应", err)
}

  1. fmt.Println(s)

}

输出:

  1. [title hi]
  2. [title hello]
英文:

Each HGETALL returns it's own series of values, which need to be converted to strings, and the pipeline is returning a series of those. Use the generic redis.Values to break down this outer structure first then you can parse the inner slices.

  1. // Execute the Pipeline
  2. pipe_prox, err := redis.Values(client.Do("EXEC"))
  3. if err != nil {
  4. panic(err)
  5. }
  6. for _, v := range pipe_prox {
  7. s, err := redis.Strings(v, nil)
  8. if err != nil {
  9. fmt.Println("Not a bulk strings repsonse", err)
  10. }
  11. fmt.Println(s)
  12. }

prints:

  1. [title hi]
  2. [title hello]

答案2

得分: 0

你可以这样做:

  1. pipe_prox, err := redis.Values(client.Do("EXEC"))
  2. for _, v := range pipe_prox.([]interface{}) {
  3. fmt.Println(v)
  4. }

你可以像这样执行。

英文:

you can do it like this:

  1. pipe_prox, err := redis.Values(client.Do("EXEC"))
  2. for _, v := range pipe_prox.([]interface{}) {
  3. fmt.Println(v)
  4. }

huangapple
  • 本文由 发表于 2014年6月5日 22:53:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/24063478.html
匿名

发表评论

匿名网友

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

确定