`
gaojingsong
  • 浏览: 1154712 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

【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;

   }

}

 



 

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



 

 

 



 

 
  • 大小: 48.2 KB
  • 大小: 53 KB
  • 大小: 56.8 KB
  • 大小: 58.1 KB
0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics