使用java api批量插入数据,主要参考官网的api,主要是client和Document APIs-Index API部分。
pom依赖org.elasticsearch elasticsearch 2.4.0
主要代码
Settings settings = Settings.settingsBuilder().put("cluster.name", "es1").build();TransportClient client = TransportClient.builder().settings(settings).build();client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("bigdata2"), 9300)); Long count = 100000L;String index = "bigdata";String type = "student1";BulkRequestBuilder bulkRequest = client.prepareBulk();for (int i = 0; i < count; i++) { Mapret = new HashMap (); ret.put("recordtime", 11); ret.put("area", 22); ret.put("usertype", 33); ret.put("count", 44); bulkRequest.add(client.prepareIndex(index, type).setSource(ret)); // 每10000条提交一次 if (i % 10000 == 0) { bulkRequest.execute().actionGet(); bulkRequest = client.prepareBulk(); } bulkRequest.execute().actionGet();}
参考: