You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Tommaso <my...@yahoo.it> on 2013/09/10 10:43:41 UTC

camel-redis strings serialization

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?



--
View this message in context: http://camel.465427.n5.nabble.com/camel-redis-strings-serialization-tp5738994.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: camel-redis strings serialization

Posted by Tommaso <my...@yahoo.it>.
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



--
View this message in context: http://camel.465427.n5.nabble.com/camel-redis-strings-serialization-tp5738994p5739030.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: camel-redis strings serialization

Posted by Bilgin Ibryam <bi...@gmail.com>.
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


On 10 September 2013 14:16, Tommaso <my...@yahoo.it> wrote:

> 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
>
>
> bibryam wrote
> > 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;
>
> > mynos_main@
>
> > &gt; wrote:
> >
> >> 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?
> >>
> >>
> >>
> >> --
> >> View this message in context:
> >>
> 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 &lt;https://twitter.com/bibryam&gt;
> >
> > 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
> ;
>
>
>
>
>
> --
> View this message in context:
> 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>

Re: camel-redis strings serialization

Posted by Tommaso <my...@yahoo.it>.
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


bibryam wrote
> 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;

> mynos_main@

> &gt; wrote:
> 
>> 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?
>>
>>
>>
>> --
>> View this message in context:
>> 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 &lt;https://twitter.com/bibryam&gt;
> 
> 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;





--
View this message in context: http://camel.465427.n5.nabble.com/camel-redis-strings-serialization-tp5738994p5739023.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: camel-redis strings serialization

Posted by Bilgin Ibryam <bi...@gmail.com>.
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 <my...@yahoo.it> wrote:

> 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?
>
>
>
> --
> View this message in context:
> 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>