1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| public class Input {
/** * 文件保存路径 */ private static final String filePath = "F:\\数据文件\\p1\\p1_10.json";
/** * 索引名称 */ private static final String indexName = "dwd-p1";
/** * 类型名称 */ private static final String typeName = "dwdata";
public static void main(String[] args) throws UnknownHostException {
Settings settings = Settings.settingsBuilder().put("cluster.name", "hinge-es").build(); TransportClient client = TransportClient.builder().settings(settings).build() .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.1.1"), 9300)) .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.1.2"), 9300));
BufferedReader br = null; try { br = new BufferedReader(new FileReader(new File(filePath)));
String json;
int count = 0; long total = 0;
BulkRequestBuilder bulkRequest = client.prepareBulk();
while ((json = br.readLine()) != null) { count++; total++;
bulkRequest.add(client.prepareIndex(indexName, typeName).setSource(json));
if (count == 500) { BulkResponse bulkResponse = bulkRequest.get(); if (bulkResponse.hasFailures()) { System.err.println("############ 出错了!!!!!"); }
bulkRequest = client.prepareBulk(); count = 0; System.out.println("已经导入:" + total); }
}
if (count != 0) { BulkResponse bulkResponse = bulkRequest.get(); if (bulkResponse.hasFailures()) { System.err.println("############ 出错了!!!!!"); }
System.out.println("已经导入:" + total);
}
System.out.println("导入结束");
} catch (Exception e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } }
}
}
|