elasticsearch插入数据
2017-01-19 15:11
267 查看
1、单条插入(推荐设定主键id防止重复)
public static String addIndex(String index,String type,HashMap<String, Object> hashMap){
hashMap.put("id", "3"); //这里如果不指定id的话elasticsearch会自动创建主键id,
hashMap.put("title","双宿双飞从");
hashMap.put("describe", "测试123");
hashMap.put("author", "测试doc");
TransportClient client=EsClientPool.getInstance().getClient();
try {
IndexResponse response = client.prepareIndex(index, type,hashMap.get("id").toString())
.setSource(hashMap).execute().actionGet();
System.out.println(response.getId());
return response.getId(); //返回主键
} catch (Exception e) {
// TODO: handle exception
return null;
}finally{
client.close();//关闭连接
}
}
2、批量插入
public static void addAllIndex(String index,String type,HashMap<String, Object> hashMap){
hashMap.put("title","双宿双飞从");
hashMap.put("describe", "测试123");
hashMap.put("author", "测试doc");
TransportClient client=EsClientPool.getInstance().getClient();
try {
BulkRequestBuilder bulkRequest = client.prepareBulk();
for (int i = 0; i < 10000; i++) {
bulkRequest.add(client.prepareIndex(index, type).setSource(hashMap));
// 每1000条提交一次
if (i % 10000 == 0) {
bulkRequest.execute().actionGet();
}
}
} catch (Exception e) {
}finally{
client.close();
}
}
public static String addIndex(String index,String type,HashMap<String, Object> hashMap){
hashMap.put("id", "3"); //这里如果不指定id的话elasticsearch会自动创建主键id,
hashMap.put("title","双宿双飞从");
hashMap.put("describe", "测试123");
hashMap.put("author", "测试doc");
TransportClient client=EsClientPool.getInstance().getClient();
try {
IndexResponse response = client.prepareIndex(index, type,hashMap.get("id").toString())
.setSource(hashMap).execute().actionGet();
System.out.println(response.getId());
return response.getId(); //返回主键
} catch (Exception e) {
// TODO: handle exception
return null;
}finally{
client.close();//关闭连接
}
}
2、批量插入
public static void addAllIndex(String index,String type,HashMap<String, Object> hashMap){
hashMap.put("title","双宿双飞从");
hashMap.put("describe", "测试123");
hashMap.put("author", "测试doc");
TransportClient client=EsClientPool.getInstance().getClient();
try {
BulkRequestBuilder bulkRequest = client.prepareBulk();
for (int i = 0; i < 10000; i++) {
bulkRequest.add(client.prepareIndex(index, type).setSource(hashMap));
// 每1000条提交一次
if (i % 10000 == 0) {
bulkRequest.execute().actionGet();
}
}
} catch (Exception e) {
}finally{
client.close();
}
}
相关文章推荐
- 使用Python往Elasticsearch插入数据
- ElasticSearch插入数据优化
- 实时数据之python操作elasticsearch监控数据插入图表分析
- Python中elasticsearch插入和更新数据的实现方法
- Python中elasticsearch如何插入和更新数据
- 提高ElasticSearch插入数据性能
- elasticsearch系列(一):创建mapping和插入数据
- ElasticSearch插入数据问题
- 使用Python往Elasticsearch插入数据
- elasticsearch 创建type及插入数据
- Elasticsearch教程(七) elasticsearch Insert 插入数据(Java)
- (五)ElasticSearch插入数据
- elasticsearch使用restclient接口插入数据
- elasticsearch使用java api批量插入数据
- Elasticsearch java api 插入数据
- ElasticSearch插入数据
- elasticsearch 插入数据
- elasticsearch Insert 插入数据和delete 删除数据(Java)
- java操作nsq数据插入elasticsearch
- 求救!用Access数据库插入数据,INSERT INTO语句总是出现错误,但同样的语句格式,用SQL Server 2000数据库,不出现错误!