How to have custom mapper keys in your documents with Jongo

Posted: décembre 31st, 2013 | Author: | Filed under: Dev, Java, Jongo, MongoDB, NoSQL, Tutorial | No Comments »

I’m giving a try to Jongo to replace spring-data-mongodb in a project but I have to manage documents with ‘not really java compatible keys’.
For example, a key with a first upper case letter and some ‘-’ inside … (well ok that’s ugly but I can’t change that now).

In spring-data, I’m using MappingMongoConverter, it works but Jongo seems to be easier to use and deploy than spring data.

And yes, it’s very simple to customize the way Jongo manages your beans !
The reason is that Jongo uses Jackson Mapper (by default but you could use your own or favorite one).
So, you just have to create a PropertyNamingStrategy !!!

1 – Create a NamingStrategy

With this naming strategy, we just search in a map if there’s a replacement value for the provided key.


import static com.fasterxml.jackson.databind.PropertyNamingStrategy.PropertyNamingStrategyBase;

import java.util.Map;

public class MapTransformNamingStrategy extends PropertyNamingStrategyBase{

    private static final long serialVersionUID = 1L;

    private Map<String, String> mapping;

    public MapTransformNamingStrategy(Map<String, String> mapping) {
        this.mapping = mapping;

    public String translate(String property) {
        if (mapping.containsKey(property)) {
            return mapping.get(property);

        return property;

2 – Use it with Jongo

As you could see in the Jongo documentation :

Jongo comes with a custom Jackson configuration that can be extended.

So you just have to use addModifier and a MapperModifier implementation to set your naming strategy to the mapper.


import com.fasterxml.jackson.databind.ObjectMapper;
import org.jongo.marshall.jackson.configuration.MapperModifier;

import java.util.HashMap;
import java.util.Map;

public class CustomMapperModifier implements MapperModifier{
    Map<String, String> mapping = new HashMap<String, String>();

    public CustomMapperModifier(){
        mapping.put("myProperty", "custom-key-for-my-property");

    public void modify(ObjectMapper objectMapper) {
        objectMapper.setPropertyNamingStrategy(new MapTransformNamingStrategy(mapping));
Jongo jongo = new Jongo(db, new JacksonMapper.Builder().addModifier(new CustomMapperModifier()).build());

3 – Full example

Here is a DummyObject class to test :


import org.bson.types.ObjectId;

public class DummyObject {

    private ObjectId id;
    private String myProperty;

    public ObjectId getId() {
        return id;

    public void setId(ObjectId id) { = id;

    public String getMyProperty() {
        return myProperty;

    public void setMyProperty(String myProperty) {
        this.myProperty = myProperty;

And a unit test (I’m using an embedded mongo for the test)


import com.mongodb.DB;
import com.mongodb.MongoClient;
import de.flapdoodle.embed.mongo.MongodExecutable;
import de.flapdoodle.embed.mongo.MongodProcess;
import de.flapdoodle.embed.mongo.MongodStarter;
import de.flapdoodle.embed.mongo.config.MongodConfig;
import de.flapdoodle.embed.mongo.distribution.Version;
import de.flapdoodle.embed.process.runtime.Network;
import org.jongo.Jongo;
import org.jongo.MongoCollection;
import org.jongo.marshall.jackson.JacksonMapper;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;


import static org.fest.assertions.Assertions.assertThat;

public class NamingStrategyTest {

    public static MongodExecutable mongodExe;
    public static MongodProcess    mongod;

    public static void runBeforeAllTests() throws Exception {
        MongodStarter runtime = MongodStarter.getDefaultInstance();
        NamingStrategyTest.mongodExe = runtime.prepare(new MongodConfig(Version.Main.V2_0, 12345, Network.localhostIsIPv6()));
        NamingStrategyTest.mongod = NamingStrategyTest.mongodExe.start();

    public static void runAfterAllTests() {

    public void testNaming(){
        DB db = null;
        try {
            db = new MongoClient("localhost", 12345).getDB("myDB");

            Jongo jongo = new Jongo(db, new JacksonMapper.Builder().addModifier(new CustomMapperModifier()).build());

            MongoCollection dum = jongo.getCollection("dummies");

            DummyObject d = new DummyObject();
            String prop = "where is brian ?";


            DummyObject check = dum.findOne("{custom-key-for-my-property: '"+prop+"'}").as(DummyObject.class);

        } catch (UnknownHostException e) {


Compiling and installing Hyperdex from sources with Java and YCSB bindings on Debian

Posted: août 2nd, 2012 | Author: | Filed under: BigData, Dev, Java, Linux, NoSQL, Tutorial | No Comments »

After a lot of hours I finally succeed to build Hyperdex on a Debian with Java and YCSB bindings.

1 – Apt sources

Edit /etc/apt/sources.list and add backports packages

deb squeeze-backports main

If you don’t have contrib and non-free for the main line, add them :

deb squeeze main contrib non-free

Add the Hyperdex repository

wget -O - \
  | apt-key add -

wget -O /etc/apt/sources.list.d/hyperdex.list \


apt-get update

2 – Install dependencies

apt-get install git openjdk-6-jdk build-essential libcityhash-dev libgoogle-glog-dev \
libpopt-dev libpo6-dev libe-dev libbusybee-dev python python-dev python-pyparsing \
python-setuptools python-argparse cython swig2.0 autoconf automake autoconf-archive libtool pkg-config gperf
easy_install sphinx
tar zxf ycsb-0.1.4.tar.gz
export CLASSPATH=`pwd`/ycsb-0.1.4/core/lib/core-0.1.4.jar
git clone git://
git clone git://

cd po6
autoreconf -i; ./configure; make && make install
cd ..

cd e
autoreconf -i; ./configure; make && make install
cd ..

3 – Install Hyperdex

git clone git://

cd Hyperdex
autoreconf -i; ./configure --enable-java-bindings --enable-ycsb; make && make install