Search code examples
javaserializationelasticsearchapache-stormjsonserializer

Faster object json convertor


I have written a program which inserts in bulk to Elasticsearch in batch of around 3000. The problem is that I need to convert these object to json before executing the bulk insert request. But there is major downside with json convertion and it is becoming a bottle neck of my whole computation.

Can any one suggest a super fast way to convert object to json in java. My code looks like this:

  private String getESValueAsString(ElasticSearchValue elasticSearchValue) throws JsonProcessingException {
    ElasticSearchValue prevValue = null;
    if (stateType == StateType.OPAQUE) {
      prevValue = (ElasticSearchValue) elasticSearchValue.getPrevious();
    }

    elasticSearchValue.setPrevious(null);

    ObjectMapper om = new ObjectMapper();
    Map<String, Object> props = om.convertValue(elasticSearchValue, Map.class);

    if (stateType == stateType.OPAQUE) {
      props.put("previous", prevValue);
    }

    return om.writeValueAsString(props);
  }

Solution

  • Just found the issue, I am creating too many ObjectMapper for each serialization and that is making my whole processing slow. This is a very good guide and it improved my performance 100x

    http://wiki.fasterxml.com/JacksonBestPracticesPerformance