elasticsearch bulk 批量加载索引的实例

来源:互联网 发布:java获取map中的泛型 编辑:程序博客网 时间:2024/06/07 03:08

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

public class ElasticSearchBulkIn {

//java -jar shuzilmJob-1.0-SNAPSHOT-jar-with-dependencies.jar ElasticSearchBulkInpublic static void main(String[] args) {    try {        Settings setting = Settings.builder().put("cluster.name", "du-es").build();        TransportClient client = new PreBuiltTransportClient(setting)                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("du-es-1"), 9311));        //Add transport addresses and do something with the client...        File article = new File("/home/srvadmin/lw/data/bulk.txt");        FileReader fr = new FileReader(article);        BufferedReader bfr = new BufferedReader(fr);        String line = null;        BulkRequestBuilder bulkRequest = client.prepareBulk();        int count = 0;        while ((line = bfr.readLine()) != null) {            bulkRequest.add(client.prepareIndex("cell_geo_index", "bds_cell_geo_m").setSource(line));            count++;            if (count % 100 == 0) {                bulkRequest.execute().actionGet();                bulkRequest.request().requests().clear();                System.out.println(count);            }        }        bulkRequest.execute().actionGet();        bfr.close();        fr.close();        client.close();    } catch (UnknownHostException e) {        e.printStackTrace();    } catch (FileNotFoundException e) {        e.printStackTrace();    } catch (IOException e) {        e.printStackTrace();    }}

}

索引的创建:
curl -XPUT ip:9211/cell_geo_index -d ‘{
“settings”: {
“number_of_shards” : 10,
“number_of_replicas” : 0
},
“mappings” : {
“bds_cell_geo_m” : {
“dynamic” : true,
“properties” : {
“cell_id” : {
“type” : “string”,
“index”: “not_analyzed”
},
“lac_id”:{
“type” : “string”,
“index”: “not_analyzed”
},
“opt_id” : {
“type” : “string”,
“index”: “not_analyzed”
},
“lng_center” : {
“type” : “string”,
“index”: “not_analyzed”
},
“lat_center” : {
“type” : “string”,
“index”: “not_analyzed”
}
}
}
}
}’
加载的文件bulk.txt具体内容如下:
{“cell_id”:”20833037”,”lac_id”:”4394”,”opt_id”:”46000”,”lng_center”:”116.573174”,”lat_center”:”39.861816”}
{“cell_id”:”3449199”,”lac_id”:”4134”,”opt_id”:”46000”,”lng_center”:”116.418603”,”lat_center”:”40.068716”}

原创粉丝点击