码迷,mamicode.com
首页 > 编程语言 > 详细

Elasticsearch5.0 Java Api(六) -- 批量导入索引

时间:2017-05-13 21:00:30      阅读:288      评论:0      收藏:0      [点我收藏+]

标签:readline   code   line   http   null   over   elastic   .com   java   

  将计算机本地的.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 }
ElasticSearchBulkIn

 

Elasticsearch5.0 Java Api(六) -- 批量导入索引

标签:readline   code   line   http   null   over   elastic   .com   java   

原文地址:http://www.cnblogs.com/ShawnYuki/p/6850242.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!