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

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

Convert Redigo Pipeline result to strings

问题

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

我的示例代码如下:

// 在端口6379上初始化Redis(Redigo)客户端
// 并使用默认地址127.0.0.1/localhost
client, err := redis.Dial("tcp", ":6379")
if err != nil {
    panic(err)
}
defer client.Close()

// 初始化流水线
client.Send("MULTI")

// Send将命令写入连接的输出缓冲区
client.Send("HGETALL", "post:1") // 其中"post:1"包含"title 'hi'"

client.Send("HGETALL", "post:2") // 其中"post:1"包含"title 'hello'"

// 执行流水线
pipe_prox, err := client.Do("EXEC")

if err != nil {
    panic(err)
}

log.Println(pipe_prox)

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

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

但是我需要的是:

"title" "hi" "title" "hello"

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

result, _ := redis.Strings(pipe_prox, err)

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:

// Initialize Redis (Redigo) client on port 6379 
//  and default address 127.0.0.1/localhost
client, err := redis.Dial("tcp", ":6379")
  if err != nil {
  panic(err)
}
defer client.Close()

// Initialize Pipeline
client.Send("MULTI")

// Send writes the command to the connection's output buffer
client.Send("HGETALL", "post:1") // Where "post:1" contains " title 'hi' "

client.Send("HGETALL", "post:2") // Where "post:1" contains " title 'hello' "

// Execute the Pipeline
pipe_prox, err := client.Do("EXEC")

if err != nil {
  panic(err)
}

log.Println(pipe_prox)

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

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

But what I need is:

"title" "hi" "title" "hello"

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

result, _ := redis.Strings(pipe_prox, err)

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)
}

fmt.Println(s)

}

输出:

[title hi]
[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.

// Execute the Pipeline
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("Not a bulk strings repsonse", err)
	}

	fmt.Println(s)
}

prints:

[title hi]
[title hello]

答案2

得分: 0

你可以这样做:

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

你可以像这样执行。

英文:

you can do it like this:

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

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:

确定