zoukankan      html  css  js  c++  java
  • Elasticsearch批量插入数据

    zh转自:https://blog.csdn.net/qq_34382260/article/details/80483881

    1. 创建本地TransportClient:
        static TransportClient client = null;

    private static Logger logger = LoggerFactory.getLogger(ESUtils.class);

    public final static String HOST = "localhost";

    public final static int PORT = 9300;

    static Settings settings = Settings.builder().put("cluster.name","elasticsearch")
    .put("client.transport.sniff",true)
    .put("transport.type","netty3")
    .put("http.type", "netty3")
    .build();
    static {
    getConnection();
    }
    @SuppressWarnings({ "resource", "unchecked" })
    public static void getConnection() {
    try {
    client = new PreBuiltTransportClient(Settings.EMPTY)
    .addTransportAddress(new TransportAddress(InetAddress.getByName(HOST), PORT));
    } catch (UnknownHostException e) {
    e.printStackTrace();
    }
    logger.debug("Elasticsearch connect info:" + client.toString());
    }
    public void closeClient() {
    if(client != null) {
    client.close();
    }
    }
    public static void main(String[] args) {
    System.out.println(client);
    }
    2. 创建索引:
        public void addIndexByName(String index, String type, TransportClient client) throws IOException {
    XContentBuilder mapper = XContentFactory.jsonBuilder().startObject().startObject("settings")
    .startObject("index")
    .field("number_of_shards",3).field("number_of_replicas",0)
    .endObject().endObject()
    .startObject("mappings").startObject(type)
    .field("dynamic","false")
    .startObject("_all").field("enabled",false)
    .endObject()
    .startObject("properties")
    .startObject("mnc").field("type","keyword").field("ignore_above",128).endObject()
    .startObject("lac").field("type","keyword").field("ignore_above",128).endObject()
    .startObject("ci").field("type","keyword").field("ignore_above",128).endObject()
    .startObject("latitude").field("type","keyword").field("ignore_above",128).endObject()
    .startObject("longtitude").field("type","keyword").field("ignore_above",128).endObject()
    .startObject("uli").field("type","keyword").field("ignore_above",128).endObject()
    .startObject("area_code").field("type","keyword").field("ignore_above",128).endObject()
    .startObject("validity").field("type","keyword").field("ignore_above",128).endObject()
    .startObject("address").field("type","keyword").field("ignore_above",128).endObject()
    .startObject("province").field("type","keyword").field("ignore_above",128).endObject()
    .startObject("city").field("type","keyword").field("ignore_above",128).endObject()
    .startObject("district").field("type","keyword").field("ignore_above",128).endObject()
    .startObject("township").field("type","keyword").field("ignore_above",128).endObject()
    .startObject("acc").field("type","keyword").field("ignore_above",128).endObject()
    .endObject().endObject().endObject().endObject();
    System.out.println(mapper);
    CreateIndexRequestBuilder cirb = client.admin().indices().prepareCreate(index).setSource(mapper);
    CreateIndexResponse reponse = cirb.execute().actionGet();
    mapper.close();
    if(reponse.isAcknowledged()) {
    System.out.println("创建索引成功");
    }else {
    System.out.println("创建索引失败");
    }
    }
    3. Model实体:
    public class Cellinfo {

    private String mnc;
    private String lac;
    private String ci;
    private String latitude;
    private String longtitude;
    private String uli;
    private String area_code;
    private String validity;
    private String address;
    private String province;
    private String city;
    private String district;
    private String township;
    private String acc;
    public Cellinfo() {
    super();
    // TODO Auto-generated constructor stub
    }
    public String getMnc() {
    return mnc;
    }
    public void setMnc(String mnc) {
    this.mnc = mnc;
    }
    ...
    public String getAcc() {
    return acc;
    }
    public void setAcc(String acc) {
    this.acc = acc;
    }

        }

    4. 批量插入数据:
        private void save(List<Cellinfo> list) {
    BulkRequestBuilder bulkRequest = ESUtils.client.prepareBulk();
    int count = 0;
    for(Cellinfo cell : list) {
    JSONObject json = JSONObject.fromObject(cell);
    bulkRequest.add(ESUtils.client.prepareIndex("cellinfo", "nx_type").setSource(json));

    if(++count%1000 == 0) {
    bulkRequest.execute().actionGet();
    bulkRequest = ESUtils.client.prepareBulk();
    System.out.println("插入-->"+1000+"--条!");
    }

    }
    bulkRequest.execute().actionGet();
    System.out.println("插入完毕,共计:"+count+"条...");

    }
    4. maven依赖:
    <!-- https://mvnrepository.com/artifact/org.elasticsearch.client/transport -->
    <dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>transport</artifactId>
    <version>6.2.4</version>
    </dependency>
    <!-- 日志依赖 -->
    <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.21</version>
    <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
    <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.8.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
    <dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.0</version>
    </dependency>
    <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.25</version>
    </dependency>
    <dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.14</version>
    </dependency>
    <dependency>
        <groupId>net.sf.json-lib</groupId>
        <artifactId>json-lib</artifactId>
        <version>2.4</version>
        <classifier>jdk15</classifier>
    </dependency>
    ---------------------
    作者:一品_人生
    来源:CSDN
    原文:https://blog.csdn.net/qq_34382260/article/details/80483881
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    [国家集训队]墨墨的等式(同余最短路)
    [洛谷P2575]高手过招
    [CSP校内集训]rank
    杀人游戏(tarjan思维好题)
    骑士游戏(spfa好题)
    机房模拟测试4:计数类dp+水题+树上计数
    机房测试模拟2:模拟+数学+数位dp
    机房测试11:最小生成树(最小生成树+二分)
    机房测试模拟1(day2):矩阵+树上贪心+bfs+状压
    机房测试16:字符串专题(AC自动机+dp+kmp)
  • 原文地址:https://www.cnblogs.com/sqw8080/p/10943934.html
Copyright © 2011-2022 走看看