0

약 3000의 일괄 처리로 Elasticsearch에 대량으로 삽입하는 프로그램을 작성했습니다. 문제는이 개체를 대량 삽입 요청을 실행하기 전에 json으로 변환해야한다는 것입니다. 그러나 json 변환에 대한 주요 단점이 있으며, 전체 계산의 병목이되고 있습니다.빠른 개체 json 변환기

자바에서 객체를 json으로 변환하는 초고속 방법을 제안 할 수 있습니까? 내 코드는 다음과 같습니다

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); 
    } 
+0

을 개선? – Chiron

+0

@Chiron : 문제를 발견했습니다. 그것은 각각의 모든 객체가 새로운 ObjectMapper를 생성했기 때문입니다. –

답변

1

방금 ​​문제가 발견되어 각 직렬화에 너무 많은 ObjectMapper가 생성되어 전체 처리 속도가 느려지고 있습니다. 이 아주 좋은 안내서이며 내 성능 100 배 귀하의 질문에 폭풍 부분입니다

http://wiki.fasterxml.com/JacksonBestPracticesPerformance

0

이유는 바로이

Client client = new TransportClient().addTransportAddress(new InetSocketTransportAddress("localhost", 9300)); 
BulkRequestBuilder bulk = client.prepareBulk(); 
..... 
bulk.add(client.prepareIndex(<your index>, <your type>) 
    .setSource(<your object>.toJson()); 
.... 

처럼, 처음에 BulkRequestBuilder JSON 기록에 무언가를 삽입하고 <your object> class

이 같은 Gson를 만들 수 없습니다 :

Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); 

및 방법 :

public String toJson(){ 
    return gson.toJson(this, <you class>.class); 
}