ElasticSearch 数据的批量索引

  ElasticSearch 一般用于检索百万级别以上的数据,因此建立索引都是批量建立的,当然也支持单量索引。

  ElasticSearch 以json数据格式作为数据插入格式,而Solr是以文档形式作为基本格式,因此在建立索引之前,首先得把数据封装成我们需要的格式:

  可以用字符串,然后转成json:

String json = "{" +
         "\"poi_index\":\"1\"," +
         "\"poi_title\":\"XXXX大学\"," +
         "\"poi_address\":\"XX省XX市XX区XX号\"," +
         "\"poi_lng\":\"126.545454\"," +
         "\"poi_lat\":\"23.121212\"," +
         "\"poi_phone\":\"15988888888\"," +
         "\"poi_tags\":\"学校,教育\"" +
    "}";
JSONObject json2 = JSONObject.fromObject(json);

  可以用JsonObject:

                jsonObject = new JSONObject();
                jsonObject.put("poi_index","23");
                jsonObject.put("poi_title", "xx大学");
                jsonObject.put("poi_address","xx路xx号");
                jsonObject.put("poi_lng", "123.321");
                jsonObject.put("poi_lat", ".23.32");
                jsonObject.put("poi_phone", "123456768");
                jsonObject.put("poi_tags", "学校");

  也可以使用ElasicSearch附带的帮助类:

import static org.elasticsearch.common.xcontent.XContentFactory.*;

XContentBuilder builder = jsonBuilder()
    .startObject()
        .field("user", "kimchy")
        .field("postDate", new Date())
        .field("message", "trying out Elasticsearch")
    .endObject()

 String json = builder.string();

 

  通过BulkRequestBuilder,将批量数据添加到request协议栈缓冲区:

BulkRequestBuilder bulkRequest = client.prepareBulk();
bulkRequest.add(client.prepareIndex("pois", "cxyword")
                        .setSource(jsonObject));

  执行 get() 就能将数据插入创建的索引库,最后用BulkResponse判断是否插入失败:

            BulkResponse bulkResponse = bulkRequest.get();
            if (bulkResponse.hasFailures())
            {
                       System.out.println("failed")
            }   

 

posted @ 2017-08-05 10:55  Forever-Road  阅读(2877)  评论(0编辑  收藏  举报