How to start using Messagepack in my below code?

414 views
Skip to first unread message

Raihan Jamal

unread,
Sep 15, 2013, 1:31:35 AM9/15/13
to msgpa...@googlegroups.com
I am planning to use `Messagepack` in our project. Currently, we are using `JSON` in our project and we are writing serialize JSON document into Cassandra. Now we are thinking to use `Messagepack` which is an efficient binary serialization format.

And I believe using MessagePack in our project is almost a drop-in optimization. Below is my code which is serializing the JSON document using ObjectMapper.

public static void main(String[] args) throws IOException {

final long lmd = System.currentTimeMillis();

Map<String, Object> props = new HashMap<String, Object>();
props.put("site-id", 0);
props.put("price-score", 0.5);
props.put("confidence-score", 0.2);

Map<String, Category> categories = new HashMap<String, Category>();
categories.put("123", new Category("0.4", "0.2"));
categories.put("321", new Category("0.2", "0.5"));
props.put("categories", categories);

AttributeValue av = new AttributeValue(); // using Jackson in this class
av.setProperties(props);
Attribute attr = new Attribute(); // using jackson in this class
attr.instantiateNewListValue();
attr.getListValue().add(av);
attr.setLastModifiedDate(lmd);
System.out.println(attr); 
// serialize it
try {
String jsonStr = new ObjectMapper().writeValueAsString(attr);
// write into database
System.out.println(jsonStr);
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Both of my classes, AttributeValue and Attribute are using Jackson annotations and making a JSON document for me.

Now I am not sure, how to use Messagepack in my above code as I am not able to find clear documentation on it. Can anyone help me with that?

Mitsunori Komatsu

unread,
Sep 15, 2013, 12:13:56 PM9/15/13
to msgpa...@googlegroups.com
Hi Raihan,

How about like this?

@Message
public static class Category {
    String fst;
    String snd;
    public Category() {} // needed by MessagePack(internally Javassist)
    public Category(String fst, String snd) {
        this.fst = fst;
        this.snd = snd;
    }
}

public static void main(String[] args) throws IOException, URISyntaxException, InterruptedException, ExecutionException {
    Map<String, Object> props = new HashMap<String, Object>();
    props.put("site-id", 0);
    props.put("price-score", 0.5);
    props.put("confidence-score", 0.2);

    Map<String, Category> categories = new HashMap<String, Category>();
    categories.put("123", new Category("0.4", "0.2"));
    categories.put("321", new Category("0.2", "0.5"));
    props.put("categories", categories);

    MessagePack msgpack = new JSON();
    BufferPacker packer = msgpack.createBufferPacker();
    packer.write(props);
    byte[] ser = packer.toByteArray();

    // write into database
    System.out.println(new String(ser));
}

Thanks,
Mitsunori
Reply all
Reply to author
Forward
0 new messages