Elasticsearch5.0 Java Api -- 批量导入索引
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Elasticsearch5.0 Java Api -- 批量导入索引相关的知识,希望对你有一定的参考价值。
将计算机本地的.json格式文件中的数据,批量导入到索引库中
1 package com.juyun.test; 2 3 import java.io.BufferedReader; 4 import java.io.File; 5 import java.io.FileNotFoundException; 6 import java.io.FileReader; 7 import java.io.IOException; 8 import java.net.InetAddress; 9 import java.net.UnknownHostException; 10 import java.util.ArrayList; 11 import java.util.List; 12 13 import org.elasticsearch.action.bulk.BulkRequestBuilder; 14 import org.elasticsearch.client.Client; 15 import org.elasticsearch.client.transport.TransportClient; 16 import org.elasticsearch.common.settings.Settings; 17 import org.elasticsearch.common.transport.InetSocketTransportAddress; 18 import org.elasticsearch.transport.client.PreBuiltTransportClient; 19 20 public class ElasticSearchBulkIn { 21 22 private static Client client; 23 24 /** 25 * 从外部文件批量导入数据 26 * @param args 27 */ 28 public static void main(String[] args) { 29 30 try { 31 32 // 设置集群名称 33 Settings settings = Settings.builder().put("cluster.name", "elasticsearch").build(); 34 // 创建client 35 client = new PreBuiltTransportClient(settings) 36 .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("172.16.0.157"), 9300)); 37 38 //FirstThread ft=new FirstThread(); // 创建FirstThread对象 39 //ft.start(); 40 41 File article = new File("C:/Users/juyun/Desktop/al.json"); 42 FileReader fr=new FileReader(article); 43 BufferedReader bfr=new BufferedReader(fr); 44 String line=null; 45 BulkRequestBuilder bulkRequest=client.prepareBulk(); 46 int count=0; 47 /* while((line=bfr.readLine())!=null){ 48 bulkRequest.add(client.prepareIndex("newindex","newhttp").setSource(line)); // 逐行写入 49 count++; 50 System.out.println(count); 51 bulkRequest.execute().actionGet(); 52 }*/ 53 bulkRequest.execute().actionGet(); 54 bfr.close(); 55 fr.close(); 56 } catch (UnknownHostException e) { 57 e.printStackTrace(); 58 } catch (FileNotFoundException e) { 59 e.printStackTrace(); 60 } catch (IOException e) { 61 e.printStackTrace(); 62 } 63 } 64 65 66 class FirstThread extends Thread{ 67 @Override 68 public void run() { 69 try{ 70 File article = new File("C:/Users/juyun/Desktop/al.json"); 71 FileReader fr=new FileReader(article); 72 BufferedReader bfr=new BufferedReader(fr); 73 String line=null; 74 BulkRequestBuilder bulkRequest=client.prepareBulk(); 75 int count=0; 76 while((line=bfr.readLine())!=null){ 77 bulkRequest.add(client.prepareIndex("newindex","newhttp").setSource(line)); // 逐行写入 78 count++; 79 System.out.println(count); 80 bulkRequest.execute().actionGet(); 81 } 82 bulkRequest.execute().actionGet(); 83 bfr.close(); 84 fr.close(); 85 }catch(Exception e){ 86 } 87 } 88 } 89 }
以上是关于Elasticsearch5.0 Java Api -- 批量导入索引的主要内容,如果未能解决你的问题,请参考以下文章
Elasticsearch5.0 Java Api -- 聚合查询
Elasticsearch5.0 Java Api -- 检索索引
Elasticsearch5.0 Java Api -- 更新索引
Elasticsearch5.0 Java Api -- 常用DSL查询