如何通过使用java客户端从redis读取erlang术语? [英] How to read erlang term from redis by using java client?

查看:155
本文介绍了如何通过使用java客户端从redis读取erlang术语?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

e.g。我保存元组T = {k1,v1,k2,v2}到jedis的redis:

  eredis:q ,[SET,<mykey>> term_to_binary(T)])。 

我正在尝试使用以下代码阅读此erlang术语:

  Jedis j = Redis.pool.getResource(); 
byte [] t = j.get(mykey)。getBytes();
OtpInputStream ois = new OtpInputStream(t);
System.out.println(OtpErlangObject.decode(ois));

错误是:com.ericsson.otp.erlang.OtpErlangDecodeException:Uknown数据类型:239.



那么如何正确地获得erlang术语?






Erlang侧:



term_to_binary({k1,v1,k2,v2})。



< 131,104,4,100,0,2,107,49,100,0,2,118,49,100,0,2,107,50,
100,0,2,118,50 >>



Java侧:



j.get(mykey)。getBytes():



-17 -65 -67 104 4 100 0 2 107 49 100 0 2 118 49 100 0 2 107 50 100 0 2 118 50。



看起来只有前3个字节不同。所以我将它们改为字节(131),
,然后可以使用System.out.println(OtpErlangObject.decode(ois))正确打印。



但是,当这个术语比较复杂的时候,比如在里面列出一个记录,它不会奏效。其他一些字符不仅会出现在数据的头部,还会出现在数据的头部,也会出现在数据的头部,也会出现在结尾和中间。



为什么我保存的数据与我所得到的不同? / p>

解决方案

字节数组开头的负数不是erlang外部术语语法的有效值。



我会假设,因为你已经把这个erlang条款存储在redis这样一段时间,你正在正确地插入它们。



真的只留下一件事:当你调用 getBytes()你的编码关闭时,最有可能使用任何编码设置为系统上的默认值(可能是UTF-8,但我不确定)。真的你想做的是将不同的编码传递给 getBytes(),可能是这样的: getBytes(US-ASCII) #getBytes%28java.lang.String%29rel =nofollow noreferrer>查看文档,可以使用编码。



href =https://stackoverflow.com/questions/5688042/how-to-convert-a-java-string-to-an-ascii-byte-array> SO SO上的链接,显示如何转换一个字符串到ASCII字节数组。


e.g. I save the tuple T = {k1, v1, k2, v2} to the redis by jedis:

 eredis:q(Conn, ["SET", <<"mykey">>, term_to_binary(T)]).

I am trying to use the code below to read this erlang term:

Jedis j =  Redis.pool.getResource();
byte[] t = j.get("mykey").getBytes();
OtpInputStream ois = new OtpInputStream(t);
System.out.println(OtpErlangObject.decode(ois));

The error is: com.ericsson.otp.erlang.OtpErlangDecodeException: Uknown data type: 239.

So how can I get the erlang term correctly?


Erlang side:

term_to_binary({k1, v1, k2, v2}).

<<131,104,4,100,0,2,107,49,100,0,2,118,49,100,0,2,107,50, 100,0,2,118,50>>

Java side:

j.get("mykey").getBytes():

-17 -65 -67 104 4 100 0 2 107 49 100 0 2 118 49 100 0 2 107 50 100 0 2 118 50.

It seems that only the first 3 byte are different. So I change them to byte(131), and then it can be printed correctly with System.out.println(OtpErlangObject.decode(ois)).

But when the term is more complicated, such as for a record with list inside, it wont work. cuz some other characters will appear not only at the head of the data but also the end and the middle.

Why the data I saved is different from what I got?

解决方案

The negative numbers at the beginning of the byte array are not valid values for erlang external term syntax.

I would assume that since you have been storing the erlang terms in redis this way for some time, you are inserting them correctly.

That really only leaves one thing: When you call getBytes() your encoding is off, it is most likely using whatever encoding is set as the default on your system (probably UTF-8, but I'm not sure). Really what you want to do is pass a different encoding to getBytes(), probably like this: getBytes("US-ASCII").

check out the documentation for encodings are available.

Heres a link on SO that shows how to convert a string to an ASCII byte array.

这篇关于如何通过使用java客户端从redis读取erlang术语?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆