【elasticsearch之批量提交Bulk】

提交之前

 

提交之后



 数据查看



 

ES批量提交代码如下:

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.entity.StringEntity;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.util.EntityUtils;

 

import com.alibaba.fastjson.JSONObject;

 

public class ImportDB2ES {

 

/**

* @param args

 

The REST API endpoint is /_bulk, and it expects the following JSON structure:

action_and_meta_data\n

optional_source\n

action_and_meta_data\n

optional_source\n

....

action_and_meta_data\n

optional_source\n

 

POST _bulk

{ "index" : { "_index" : "test", "_type" : "type1", "_id" : "1" } }

{ "field1" : "value1" }

{ "delete" : { "_index" : "test", "_type" : "type1", "_id" : "2" } }

{ "create" : { "_index" : "test", "_type" : "type1", "_id" : "3" } }

{ "field1" : "value3" }

{ "update" : {"_id" : "1", "_type" : "type1", "_index" : "test"} }

{ "doc" : {"field2" : "value2"} }

@author http://gaojingsong.iteye.com/

*/

public static void main(String[] args) {

 

String url = "http://192.168.1.104:9200/_bulk";

List<Student> list = new ArrayList<Student>();

//1、准备数据阶段

list.add(new Student("1","zhangsan1",13));

list.add(new Student("2","zhangsan2",23));

list.add(new Student("3","zhangsan3",33));

list.add(new Student("4","zhangsan4",43));

//2、构造Json阶段

String json  = toBatchJSon(list,"test_bulk");

System.out.println(json);

//3、批量提交阶段

try {

httpPostWithJSON(url,json);

} catch (Exception e) {

e.printStackTrace();

}

}

 

public static String toBatchJSon(List list,String indexname) {

StringBuffer sb = new StringBuffer();

int i=0;

for (@SuppressWarnings("rawtypes")

Iterator iterator = list.iterator(); iterator.hasNext();) {

JSONObject json = (JSONObject) JSONObject.toJSON(iterator.next());

++i;

String str="{ \"create\" : { \"_index\" : \""+indexname+"\", \"_type\" : \"type1\", \"_id\" : \""+i+"\" } }";

sb.append(str).append("\n");

sb.append(json).append("\n");

}

return sb.toString();

 

}

 

public static String httpPostWithJSON(String url,String json) throws Exception {

     

       HttpPost httpPost = new HttpPost(url);

       CloseableHttpClient client = HttpClients.createDefault();

       String respContent = null;

       StringEntity entity = new StringEntity(json,"utf-8");//解决中文乱码问题    

       entity.setContentEncoding("UTF-8");    

       entity.setContentType("application/json");    

       httpPost.setEntity(entity);

       System.out.println();

       

       HttpResponse resp = client.execute(httpPost);

       if(resp.getStatusLine().getStatusCode() == 200) {

           HttpEntity he = resp.getEntity();

           respContent = EntityUtils.toString(he,"UTF-8");

       }

       return respContent;

   }

}

 



 

 原创不易,欢迎打赏,请认准正确地址,谨防假冒



 

 

 



 

 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值