Es添加信息踩坑之--The number of object passed must be even but was [1]

es用1.4版本时

BulkRequestBuilder bulkRequest = getTransportClient().prepareBulk();

IndexRequest request = null;
String json = "{" +
        "\"user\":\"kimchy\"," +
        "\"postDate\":\"2013-01-30\"," +
        "\"message\":\"trying out Elasticsearch\"" +
    "}"
request = getTransportClient()
						.prepareIndex(PCSConstant.ES_INDEX_QUALITY,             
                         PCSConstant.ECS_DATA_QUALITY).setSource(json)
						.request();
//此处setSource()的参数可以是jsong格式的的字符串
bulkRequest.add(request);
BulkResponse bulkResponse = bulkRequest.get();

es 变更版本后,此处代码不适用,会报  The number of object passed must be even but was [1] 这个异常

新的代码

BulkRequestBuilder bulkRequest = getTransportClient().prepareBulk();

IndexRequest request = null;
String json = "{" +
        "\"user\":\"kimchy\"," +
        "\"postDate\":\"2013-01-30\"," +
        "\"message\":\"trying out Elasticsearch\"" +
    "}"
Map map = new com.google.gson.Gson().fromJson(json,map.getClass());
request = getTransportClient()
						.prepareIndex(PCSConstant.ES_INDEX_QUALITY,             
                         PCSConstant.ECS_DATA_QUALITY).setSource(map)
						.request();
//此处setSource()的参数不能是json串了,我把他转化成了map
bulkRequest.add(request);
BulkResponse bulkResponse = bulkRequest.get();

 

你可能感兴趣的:(大数据,Java)