Phani K
Phani K

Reputation: 23

RESP format for HMSET in REDIS

I am trying to mass insert a table data into redis hash using RESP protocol, using the traditional SQL at Oracle side.

I have constructed the RESP protocol format for the data:

table data:

col1-----col2

  v1-----v2

RESP format:

"*6\r\n$5\r\nHMSET\r\n$4\r\nkey1\r\n$4\r\ncol1\r\n$1\r\n1\r\n$4\r\ncol2\r\n$1\r\n2\r\n"

EDIT:

saving this output in a file, to feed it to the REDIS client through piping, for mass insertion."

What am I missing here? When I try to run it in the shell, I get this error:

$ cat test_1.dat | redis-cli --pipe

All data transferred. Waiting for the last reply...

ERR unknown command '*6 $5 HMSET $4 key1 $4 col1 $1 1 $4 col2 $1 2 '

Last reply received from server.

errors: 1, replies: 1

Please help me! Wasted almost a day trying to find the issue in the RESP format but in vain. After reading many web pages, I found that mass insert is the best option (performance-wise) so I would like to stick to that as a solution, using RESP for HMSET.

Upvotes: 2

Views: 850

Answers (1)

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230461

I suspect that line endings are broken in your test1.dat file (that is, they aren't ASCII \r\n). I was able to successfully do this.

Prepare the file (from ruby script)

s = "*6\r\n$5\r\nHMSET\r\n$4\r\nkey3\r\n$4\r\ncol1\r\n$1\r\n1\r\n$4\r\ncol2\r\n$1\r\n2\r\n"

file = File.new("/Users/sergio/redis_test.txt", 'w')
file.write s
file.close

Run it

% cat redis_test.txt | redis-cli --pipe
All data transferred. Waiting for the last reply...
Last reply received from server.
errors: 0, replies: 1

Result:

127.0.0.1:6379> hgetall key3
1) "col1"
2) "1"
3) "col2"
4) "2"

Upvotes: 3

Related Questions