2
public class MsgPackInRedis {
    private String ip;
    private int port;
    private String session;
    private String protocol;
}
MsgPackInRedis msgPackStringInRedis = new MsgPackInRedis();

I encode a java object msgPackStringInRedis of class MsgPackInRedis with msgpack, then store in redis. And I want to decode that in lua, which runs in redis, how can I get "session" ? Can I do like this below, get session by index 3?

local msgPackObject = cmsgpack.unpack(msgPackStringInRedis)
local session = msgPackObject[3]
  • What's the msgpack-encoded form of your object? – Itamar Haber Jun 12 '17 at 14:23
  • `MessagePack pack = new MessagePack();` `MsgPackInRedis msgPackStringInRedis = new MsgPackInRedis();` `byte[] msgBytes = pack.write(msgPackStringInRedis);` I do like this, and get msgpack-encoded byte[], which will be stored in redis. Maybe the "byte[] msgBytes" is a value...value form, not a key-value...key-value form, I guess. Need your help~ – Reborn Tong Jun 13 '17 at 03:13
  • 1
    Please attach to your question (use the edit link) the actual byte[] contents of `msgBytes`. If you do that I'll be able show you how you can access the contents from Redis' Lua. Basically it is either by index as you tried or by name, i.e. `msgPackObject['session'])`, but that depends on what your packed message actually contains. – Itamar Haber Jun 13 '17 at 11:33
  • @Itamar Harber Thanks for your help! `msgPackStringInRedis.setSession("ABCD");` I just put **session** value in **msgPackStringInRedis**, and pack **msgPackStringInRedis** use msgpack,and I get the **msgBytes** result: `0x94 0xc0 0x00 0xa4 0x41 0x42 0x43 0x44 0xc0` Sorry for my poor English, and I have tried in redis just now, as a result, `msgPackObject[3]` works. But I don't know why this work, please give me some ideal~ – Reborn Tong Jun 13 '17 at 13:41

2 Answers2

2

MessagePack is an encoding - think non-easily-readable JSON. In fact, this website does a back and forth translation between the two: http://kawanet.github.io/msgpack-lite/

Feeding your (0x94 0xc0 0x00 0xa4 0x41 0x42 0x43 0x44 0xc0) to the above website, you can see the JSON representation which looks like:

[
  null,
  0,
  "ABCD",
  null
]

You can test that in Redis' Lua as well, e.g. (note that Lua 5.1 accepts decimal byte representation, hence the different representation of the same payload in the example):

$ redis-cli EVAL "return(cmsgpack.unpack('\148\192\00\164\65\66\67\68\192')[3])" 0
"ABCD"

So frankly, I see no issue with your code. What is the problem that you are experiencing exactly?

Itamar Haber
  • 47,336
  • 7
  • 91
  • 117
  • Thank you so much! As I was not clear what's behind msgpack, then confused by the problem. And now with your answer I Suddenly I See it. – Reborn Tong Jun 14 '17 at 03:06
  • Can you explain why this is not working `redis-cli EVAL "return cjson.encode(cmsgpack.unpack(ARGV[1]))" 0 '\148\192\00\164\65\66\67\68\192'`, but `redis-cli EVAL "return cjson.encode(cmsgpack.unpack('\148\192\00\164\65\66\67\68\192'))" 0` do works – felixmosh Jan 31 '22 at 13:10
0

Assuming that your MessagePack-ed data is stored in the String key called foo, this would do your bidding:

EVAL "return cmsgpack.unpack(redis.call('GET', KEYS[1]))" 1 foo

Note : the above assumes that the data is serialized as arrays. Returning an object will not work as Redis' protocol doesn't support that.

LuFFy
  • 8,799
  • 10
  • 41
  • 59
  • 1
    Maybe I did not express my problem clearly, which is I want to just get **session** from the object, and now is solved. Thank you all the same! – Reborn Tong Jun 14 '17 at 03:11
  • @RebornTong, Also you should keep habit of upvoting the answers if you find them helpful. – LuFFy Jun 14 '17 at 04:32
  • 1
    Yes, I did. But is useless. As StackOverFlow feedback to me: "Thanks for the feedback! Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score." – Reborn Tong Jun 14 '17 at 09:31
  • Ohh okay got it. I will upvote your question tomorrow as I have already reached my daily limit. – LuFFy Jun 14 '17 at 09:34
  • Upvoted. But still you need to ask more question or answer more questions to earn more reputations. :) – LuFFy Jun 15 '17 at 05:12
  • 1
    Yes, StackOverFlow is a nice website, and I am glad to communicate with you all. – Reborn Tong Jun 16 '17 at 04:03