Discussion:
camel-redis strings serialization
Tommaso
2013-09-10 08:43:41 UTC
Permalink
This is a question on stackoverflow as well:
http://stackoverflow.com/questions/18698802/redis-wrong-serialization-using-the-camel-redis

I'm playing with camel and redis. I have a very short route:

from("timer://foo?period=5s")

.to("http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_day.geojson")
.split().method(SplitFeatures.class,"splitMessage")
.to("spring-redis://localhost:6379?command=SET&serializer=#serializer");

Where splitMessage is the following:

public static List splitMessage(@Body String body) throws IOException {
List answer = new ArrayList();
JsonParser parser=new JsonParser();
JsonObject jo= (JsonObject)parser.parse(body);

// I care only for the features array
JsonArray features=jo.get("features").getAsJsonArray();

for (JsonElement feature: features) {
Message msg=new DefaultMessage();
JsonObject jof=feature.getAsJsonObject();

// get the key
String id=jof.get("id").getAsString().toString();

System.out.print(id);
msg.setHeader(RedisConstants.KEY, id);
msg.setHeader(RedisConstants.VALUE, jof.toString());
answer.add(msg);
}
return answer;
}

Everything runs smoothly, but when I check the redis db I see that the key
is: "\xac\xed\x00\x05t\x00\nci11361338"

and the same prefix "\xac\xed\x00\x05t\x00" is in the value.

Obviously the those printed by the System.out look fine.

As you see I tried to add a serializer, a StringRedisSerializer that I
defined in the Main like this:

Main main = new Main();
main.bind("serializer", new StringRedisSerializer());

But the result is the same (also using GenericToStringSerializer).

Is there something I'm missing?
Bilgin Ibryam
2013-09-10 12:49:09 UTC
Permalink
Hi Tommaso,

what you have seems correct.
Basically you have to use the same serializer for publishing and reading
data.
If you are using camel producer/consumer for publishing and reading data,
you don't have to specify anything, it will just work.

But if you are using a different client for reading data from redis, you
have to find the appropriate serializer from Spring redis project and
speficy that in the URI

HTH
Post by Tommaso
http://stackoverflow.com/questions/18698802/redis-wrong-serialization-using-the-camel-redis
from("timer://foo?period=5s")
.to("
http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_day.geojson")
.split().method(SplitFeatures.class,"splitMessage")
.to("spring-redis://localhost:6379?command=SET&serializer=#serializer");
List answer = new ArrayList();
JsonParser parser=new JsonParser();
JsonObject jo= (JsonObject)parser.parse(body);
// I care only for the features array
JsonArray features=jo.get("features").getAsJsonArray();
for (JsonElement feature: features) {
Message msg=new DefaultMessage();
JsonObject jof=feature.getAsJsonObject();
// get the key
String id=jof.get("id").getAsString().toString();
System.out.print(id);
msg.setHeader(RedisConstants.KEY, id);
msg.setHeader(RedisConstants.VALUE, jof.toString());
answer.add(msg);
}
return answer;
}
Everything runs smoothly, but when I check the redis db I see that the key
is: "\xac\xed\x00\x05t\x00\nci11361338"
and the same prefix "\xac\xed\x00\x05t\x00" is in the value.
Obviously the those printed by the System.out look fine.
As you see I tried to add a serializer, a StringRedisSerializer that I
Main main = new Main();
main.bind("serializer", new StringRedisSerializer());
But the result is the same (also using GenericToStringSerializer).
Is there something I'm missing?
--
http://camel.465427.n5.nabble.com/camel-redis-strings-serialization-tp5738994.html
Sent from the Camel - Users mailing list archive at Nabble.com.
--
Bilgin Ibryam

Apache Camel & Apache OFBiz committer
Blog: ofbizian.com
Twitter: @bibryam <https://twitter.com/bibryam>

Author of Instant Apache Camel Message Routing
packtpub.com/apache-camel-message-routing/book<http://www.packtpub.com/apache-camel-message-routing/book>
Tommaso
2013-09-10 13:16:48 UTC
Permalink
Hi bibryam

I thought to have done exactly what you suggest. From resources gathered
from the net I've found I should use the StringRedisSerializer (instead of
the default JdkSerializer), but still the strings saved in redis are
"dirty". Should I use another serializer to have plain strings in redis?

Ciao.
--Tommaso
Post by Bilgin Ibryam
Hi Tommaso,
what you have seems correct.
Basically you have to use the same serializer for publishing and reading
data.
If you are using camel producer/consumer for publishing and reading data,
you don't have to specify anything, it will just work.
But if you are using a different client for reading data from redis, you
have to find the appropriate serializer from Spring redis project and
speficy that in the URI
HTH
On 10 September 2013 09:43, Tommaso &lt;
Post by Tommaso
http://stackoverflow.com/questions/18698802/redis-wrong-serialization-using-the-camel-redis
from("timer://foo?period=5s")
.to("
http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_day.geojson")
.split().method(SplitFeatures.class,"splitMessage")
.to("spring-redis://localhost:6379?command=SET&serializer=#serializer");
List answer = new ArrayList();
JsonParser parser=new JsonParser();
JsonObject jo= (JsonObject)parser.parse(body);
// I care only for the features array
JsonArray features=jo.get("features").getAsJsonArray();
for (JsonElement feature: features) {
Message msg=new DefaultMessage();
JsonObject jof=feature.getAsJsonObject();
// get the key
String id=jof.get("id").getAsString().toString();
System.out.print(id);
msg.setHeader(RedisConstants.KEY, id);
msg.setHeader(RedisConstants.VALUE, jof.toString());
answer.add(msg);
}
return answer;
}
Everything runs smoothly, but when I check the redis db I see that the key
is: "\xac\xed\x00\x05t\x00\nci11361338"
and the same prefix "\xac\xed\x00\x05t\x00" is in the value.
Obviously the those printed by the System.out look fine.
As you see I tried to add a serializer, a StringRedisSerializer that I
Main main = new Main();
main.bind("serializer", new StringRedisSerializer());
But the result is the same (also using GenericToStringSerializer).
Is there something I'm missing?
--
http://camel.465427.n5.nabble.com/camel-redis-strings-serialization-tp5738994.html
Sent from the Camel - Users mailing list archive at Nabble.com.
--
Bilgin Ibryam
Apache Camel & Apache OFBiz committer
Blog: ofbizian.com
Author of Instant Apache Camel Message Routing
Bilgin Ibryam
2013-09-10 13:34:35 UTC
Permalink
Tommaso,

I had a look at the source code and it seems that the custom serializer you
are specifying is set only for consumers and not for producers. So the the
serializer you created is not used in your case. You can create a jira
ticket for it.

In the mean time, what you can do is create a RedisTemplate in the registry
and set the serializer you want to it. Then let Camel use that
RedisTemplate. That should configure Camel producer with the serializer you
want.

HTH
Post by Tommaso
Hi bibryam
I thought to have done exactly what you suggest. From resources gathered
from the net I've found I should use the StringRedisSerializer (instead of
the default JdkSerializer), but still the strings saved in redis are
"dirty". Should I use another serializer to have plain strings in redis?
Ciao.
--Tommaso
Post by Bilgin Ibryam
Hi Tommaso,
what you have seems correct.
Basically you have to use the same serializer for publishing and reading
data.
If you are using camel producer/consumer for publishing and reading data,
you don't have to specify anything, it will just work.
But if you are using a different client for reading data from redis, you
have to find the appropriate serializer from Spring redis project and
speficy that in the URI
HTH
On 10 September 2013 09:43, Tommaso &lt;
http://stackoverflow.com/questions/18698802/redis-wrong-serialization-using-the-camel-redis
Post by Bilgin Ibryam
Post by Tommaso
from("timer://foo?period=5s")
.to("
http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_day.geojson")
Post by Bilgin Ibryam
Post by Tommaso
.split().method(SplitFeatures.class,"splitMessage")
.to("spring-redis://localhost:6379?command=SET&serializer=#serializer");
{
Post by Bilgin Ibryam
Post by Tommaso
List answer = new ArrayList();
JsonParser parser=new JsonParser();
JsonObject jo= (JsonObject)parser.parse(body);
// I care only for the features array
JsonArray features=jo.get("features").getAsJsonArray();
for (JsonElement feature: features) {
Message msg=new DefaultMessage();
JsonObject jof=feature.getAsJsonObject();
// get the key
String id=jof.get("id").getAsString().toString();
System.out.print(id);
msg.setHeader(RedisConstants.KEY, id);
msg.setHeader(RedisConstants.VALUE, jof.toString());
answer.add(msg);
}
return answer;
}
Everything runs smoothly, but when I check the redis db I see that the key
is: "\xac\xed\x00\x05t\x00\nci11361338"
and the same prefix "\xac\xed\x00\x05t\x00" is in the value.
Obviously the those printed by the System.out look fine.
As you see I tried to add a serializer, a StringRedisSerializer that I
Main main = new Main();
main.bind("serializer", new StringRedisSerializer());
But the result is the same (also using GenericToStringSerializer).
Is there something I'm missing?
--
http://camel.465427.n5.nabble.com/camel-redis-strings-serialization-tp5738994.html
Post by Bilgin Ibryam
Post by Tommaso
Sent from the Camel - Users mailing list archive at Nabble.com.
--
Bilgin Ibryam
Apache Camel & Apache OFBiz committer
Blog: ofbizian.com
Author of Instant Apache Camel Message Routing
packtpub.com/apache-camel-message-routing/book&lt;http://www.packtpub.com/apache-camel-message-routing/book&gt
;
--
http://camel.465427.n5.nabble.com/camel-redis-strings-serialization-tp5738994p5739023.html
Sent from the Camel - Users mailing list archive at Nabble.com.
--
Bilgin Ibryam

Apache Camel & Apache OFBiz committer
Blog: ofbizian.com
Twitter: @bibryam <https://twitter.com/bibryam>

Author of Instant Apache Camel Message Routing
packtpub.com/apache-camel-message-routing/book<http://www.packtpub.com/apache-camel-message-routing/book>
Tommaso
2013-09-10 14:29:23 UTC
Permalink
Thank you!

If you have are on Stackoverflow and want to answer the question I will be
happy to accept your answer.

thanks again.
--Tommaso

Loading...