Java Code Examples for org.elasticsearch.client.transport.TransportClient#prepareBulk()

The following examples show how to use org.elasticsearch.client.transport.TransportClient#prepareBulk() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BaseDemo.java    From Elasticsearch-Tutorial-zh-CN with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 批量删除
 *
 * @param transportClient
 */
private static void batchDelete(TransportClient transportClient) throws IOException {
	BulkRequestBuilder bulkRequestBuilder = transportClient.prepareBulk();

	DeleteRequestBuilder deleteRequestBuilder1 = transportClient.prepareDelete("product_index", "product", "1");
	DeleteRequestBuilder deleteRequestBuilder2 = transportClient.prepareDelete("product_index", "product", "2");
	DeleteRequestBuilder deleteRequestBuilder3 = transportClient.prepareDelete("product_index", "product", "3");

	bulkRequestBuilder.add(deleteRequestBuilder1);
	bulkRequestBuilder.add(deleteRequestBuilder2);
	bulkRequestBuilder.add(deleteRequestBuilder3);

	BulkResponse bulkResponse = bulkRequestBuilder.get();
	for (BulkItemResponse bulkItemResponse : bulkResponse.getItems()) {
		logger.info("--------------------------------version= " + bulkItemResponse.getVersion());
	}

}
 
Example 2
Source File: Elasticsearch6Sink.java    From sylph with Apache License 2.0 5 votes vote down vote up
@Override
public boolean open(long partitionId, long version)
        throws Exception
{
    TransportClient client = clientFactory.createClient();
    for (String ip : config.getHosts().split(",")) {
        client.addTransportAddress(
                new TransportAddress(InetAddress.getByName(ip.split(":")[0]),
                        Integer.parseInt(ip.split(":")[1])));
    }
    this.client = client;
    this.bulkBuilder = client.prepareBulk();
    return true;
}
 
Example 3
Source File: Elasticsearch5Sink.java    From sylph with Apache License 2.0 5 votes vote down vote up
@Override
public boolean open(long partitionId, long version)
        throws Exception
{
    TransportClient client = clientFactory.createClient();
    for (String ip : config.getHosts().split(",")) {
        client.addTransportAddress(new InetSocketTransportAddress(
                InetAddress.getByName(ip.split(":")[0]), Integer.valueOf(ip.split(":")[1])));
    }
    this.client = client;
    this.bulkBuilder = client.prepareBulk();
    return true;
}
 
Example 4
Source File: BaseDemo.java    From Elasticsearch-Tutorial-zh-CN with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 批量更新
 *
 * @param transportClient
 */
private static void batchUpdate(TransportClient transportClient) throws IOException {
	BulkRequestBuilder bulkRequestBuilder = transportClient.prepareBulk();

	UpdateRequestBuilder updateRequestBuilder1 = transportClient.prepareUpdate("product_index", "product", "1")
			.setDoc(XContentFactory.jsonBuilder()
					.startObject()
					.field("product_name", "更新后的商品名称1")
					.endObject());

	UpdateRequestBuilder updateRequestBuilder2 = transportClient.prepareUpdate("product_index", "product", "2")
			.setDoc(XContentFactory.jsonBuilder()
					.startObject()
					.field("product_name", "更新后的商品名称2")
					.endObject());

	UpdateRequestBuilder updateRequestBuilder3 = transportClient.prepareUpdate("product_index", "product", "3")
			.setDoc(XContentFactory.jsonBuilder()
					.startObject()
					.field("product_name", "更新后的商品名称3")
					.endObject());


	bulkRequestBuilder.add(updateRequestBuilder1);
	bulkRequestBuilder.add(updateRequestBuilder2);
	bulkRequestBuilder.add(updateRequestBuilder3);

	BulkResponse bulkResponse = bulkRequestBuilder.get();
	for (BulkItemResponse bulkItemResponse : bulkResponse.getItems()) {
		logger.info("--------------------------------version= " + bulkItemResponse.getVersion());
	}
}
 
Example 5
Source File: EsDoc.java    From AsuraFramework with Apache License 2.0 5 votes vote down vote up
/**
 * 批量save
 * @param indexName
 * @param indexType
 * @param list
 * @return
 */
private  BulkResponse bulkSaveOrUpdate(String indexName, String indexType, List<DocPo> list){
    TransportClient client = esClientFactory.getClient();
    BulkResponse bulkResponse=null;
    BulkRequestBuilder bulkRequest = client.prepareBulk();
    for(int i=0;i<list.size();i++){
        bulkRequest.add(new IndexRequest(esClientFactory.getIndexs(indexName), indexType,list.get(i).getId()).source(JSON.toJSONString(list.get(i).getObj())));
    }
    bulkResponse = bulkRequest.get();
    // 处理错误信息
    handBulkResponseException(bulkResponse);
    return bulkResponse;
}
 
Example 6
Source File: EsDoc.java    From AsuraFramework with Apache License 2.0 5 votes vote down vote up
/**
 * 批量del
 * @param indexName
 * @param indexType
 * @param
 * @return
 */
private  BulkResponse bulkDelete(String indexName, String indexType, List<String> ids){
    TransportClient client = esClientFactory.getClient();
    BulkResponse bulkResponse=null;
    BulkRequestBuilder bulkRequest = client.prepareBulk();
    for(int i=0;i<ids.size();i++){
        bulkRequest.add(new DeleteRequest(esClientFactory.getIndexs(indexName), indexType,ids.get(i) ));
    }
    bulkResponse = bulkRequest.get();
    // 处理错误信息
    handBulkResponseException(bulkResponse);
    return bulkResponse;
}
 
Example 7
Source File: SearchItemServiceImpl.java    From BigDataPlatform with GNU General Public License v3.0 4 votes vote down vote up
@Override
public int importAllItems() {

	try{
		Settings settings = Settings.builder()
				.put("cluster.name", ES_CLUSTER_NAME).build();
		TransportClient client = new PreBuiltTransportClient(settings)
				.addTransportAddress(new TransportAddress(InetAddress.getByName(ES_CONNECT_IP), 9300));

		//批量添加
		BulkRequestBuilder bulkRequest = client.prepareBulk();

		//查询商品列表
		List<SearchItem> itemList = itemMapper.getItemList();

		//遍历商品列表
		for (SearchItem searchItem : itemList) {
			String image=searchItem.getProductImageBig();
			if (image != null && !"".equals(image)) {
				String[] strings = image.split(",");
				image=strings[0];
			}else{
				image="";
			}
			searchItem.setProductImageBig(image);
			bulkRequest.add(client.prepareIndex(ITEM_INDEX, ITEM_TYPE, String.valueOf(searchItem.getProductId()))
					.setSource(jsonBuilder()
							.startObject()
							.field("productId", searchItem.getProductId())
							.field("salePrice", searchItem.getSalePrice())
							.field("productName", searchItem.getProductName())
							.field("subTitle", searchItem.getSubTitle())
							.field("productImageBig", searchItem.getProductImageBig())
							.field("categoryName", searchItem.getCategoryName())
							.field("cid", searchItem.getCid())
							.endObject()
					)
			);
		}

		BulkResponse bulkResponse = bulkRequest.get();

		log.info("更新索引成功");

		client.close();
	}catch (Exception e){
		e.printStackTrace();
		throw new XmallException("导入ES索引库出错,请再次尝试");
	}

	return 1;
}
 
Example 8
Source File: ESTemplate.java    From canal-1.1.3 with Apache License 2.0 4 votes vote down vote up
public ESTemplate(TransportClient transportClient){
    this.transportClient = transportClient;
    this.bulkRequestBuilder = transportClient.prepareBulk();
}
 
Example 9
Source File: ElasticsearchGis.java    From Elasticsearch-Tutorial-zh-CN with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 批量创建
 * 资料:http://blog.csdn.net/loveisnull/article/details/45914115
 * 纬度在前,经度在后
 *
 * @param transportClient
 */
private static void batchCreate(TransportClient transportClient) throws IOException {


	BulkRequestBuilder bulkRequestBuilder = transportClient.prepareBulk();

	IndexRequestBuilder indexRequestBuilder1 = transportClient.prepareIndex("shop_index", "shop", "1")
			.setSource(XContentFactory.jsonBuilder()
					.startObject()
					.field("shop_name", "麻辣香锅1")
					.latlon("location", 40.12, -71.34)
					.field("created_date_time", new SimpleDateFormat("yyyyMMdd'T'HHmmss.SSSZ").format(new Date()))
					.field("last_modified_date_time", new SimpleDateFormat("yyyyMMdd'T'HHmmss.SSSZ").format(new Date()))
					.field("version", 1)
					.endObject());

	IndexRequestBuilder indexRequestBuilder2 = transportClient.prepareIndex("shop_index", "shop", "2")
			.setSource(XContentFactory.jsonBuilder()
					.startObject()
					.field("shop_name", "麻辣香锅2")
					.latlon("location", 40.12, -72.34)
					.field("created_date_time", new SimpleDateFormat("yyyyMMdd'T'HHmmss.SSSZ").format(new Date()))
					.field("last_modified_date_time", new SimpleDateFormat("yyyyMMdd'T'HHmmss.SSSZ").format(new Date()))
					.field("version", 1)
					.endObject());

	IndexRequestBuilder indexRequestBuilder3 = transportClient.prepareIndex("shop_index", "shop", "3")
			.setSource(XContentFactory.jsonBuilder()
					.startObject()
					.field("shop_name", "麻辣香锅3")
					.latlon("location", 40.12, -73.34)
					.field("created_date_time", new SimpleDateFormat("yyyyMMdd'T'HHmmss.SSSZ").format(new Date()))
					.field("last_modified_date_time", new SimpleDateFormat("yyyyMMdd'T'HHmmss.SSSZ").format(new Date()))
					.field("version", 1)
					.endObject());


	bulkRequestBuilder.add(indexRequestBuilder1);
	bulkRequestBuilder.add(indexRequestBuilder2);
	bulkRequestBuilder.add(indexRequestBuilder3);

	BulkResponse bulkResponse = bulkRequestBuilder.get();
	for (BulkItemResponse bulkItemResponse : bulkResponse.getItems()) {
		logger.info("--------------------------------version= " + bulkItemResponse.getVersion());
	}
}
 
Example 10
Source File: BaseDemo.java    From Elasticsearch-Tutorial-zh-CN with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 批量创建
 *
 * @param transportClient
 */
private static void batchCreate(TransportClient transportClient) throws IOException {
	BulkRequestBuilder bulkRequestBuilder = transportClient.prepareBulk();

	IndexRequestBuilder indexRequestBuilder1 = transportClient.prepareIndex("product_index", "product", "1")
			.setSource(XContentFactory.jsonBuilder()
					.startObject()
					.field("product_name", "飞利浦电动牙刷 HX6700-1")
					.field("product_desc", "前 1000 名赠刷头,6 月 1 日 0 点火爆开抢,618 开门红巅峰 48 小时")
					.field("price", 399.00)
					.field("created_date_time", new SimpleDateFormat("yyyyMMdd'T'HHmmss.SSSZ").format(new Date()))
					.field("last_modified_date_time", new SimpleDateFormat("yyyyMMdd'T'HHmmss.SSSZ").format(new Date()))
					.field("version", 1)
					.endObject());

	IndexRequestBuilder indexRequestBuilder2 = transportClient.prepareIndex("product_index", "product", "2")
			.setSource(XContentFactory.jsonBuilder()
					.startObject()
					.field("product_name", "飞利浦电动牙刷 HX6700-2")
					.field("product_desc", "前 1000 名赠刷头,6 月 1 日 0 点火爆开抢,618 开门红巅峰 48 小时")
					.field("price", 399.00)
					.field("created_date_time", new SimpleDateFormat("yyyyMMdd'T'HHmmss.SSSZ").format(new Date()))
					.field("last_modified_date_time", new SimpleDateFormat("yyyyMMdd'T'HHmmss.SSSZ").format(new Date()))
					.field("version", 1)
					.endObject());

	IndexRequestBuilder indexRequestBuilder3 = transportClient.prepareIndex("product_index", "product", "3")
			.setSource(XContentFactory.jsonBuilder()
					.startObject()
					.field("product_name", "飞利浦电动牙刷 HX6700-3")
					.field("product_desc", "前 1000 名赠刷头,6 月 1 日 0 点火爆开抢,618 开门红巅峰 48 小时")
					.field("price", 399.00)
					.field("created_date_time", new SimpleDateFormat("yyyyMMdd'T'HHmmss.SSSZ").format(new Date()))
					.field("last_modified_date_time", new SimpleDateFormat("yyyyMMdd'T'HHmmss.SSSZ").format(new Date()))
					.field("version", 1)
					.endObject());


	bulkRequestBuilder.add(indexRequestBuilder1);
	bulkRequestBuilder.add(indexRequestBuilder2);
	bulkRequestBuilder.add(indexRequestBuilder3);

	BulkResponse bulkResponse = bulkRequestBuilder.get();
	for (BulkItemResponse bulkItemResponse : bulkResponse.getItems()) {
		logger.info("--------------------------------version= " + bulkItemResponse.getVersion());
	}
}
 
Example 11
Source File: DoTestTransportHookProxy.java    From uavstack with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public static void main(String[] args) throws IOException {
   
    ConsoleLogger cl = new ConsoleLogger("test");
    
    cl.setDebugable(true);
    
    UAVServer.instance().setLog(cl);
    
    UAVServer.instance().putServerInfo(CaptureConstants.INFO_APPSERVER_VENDOR, ServerVendor.TOMCAT);
    MOFAgent.mofContext.put("org.uavstack.mof.ext.clsloader", Thread.currentThread().getContextClassLoader());
    
    TransportHookProxy p = new TransportHookProxy("test", Collections.emptyMap());
    
    p.doProxyInstall(null, "testApp");
           
    String[] esAddrs = {"127.0.0.1:9300"}; 
    String clusterName = "";
    String index = "esindex";
    String type = "String";
    String alias = "alias";
    Boolean result;
    
    Settings settings = Settings.EMPTY;

    if (!StringHelper.isEmpty(clusterName)) {
            settings = Settings.builder().put("cluster.name", clusterName).build();
    }

    TransportClient client = new PreBuiltTransportClient(settings);

    for (String esAddr : esAddrs) {
        String[] ipport = esAddr.split(":");
        client.addTransportAddress(new InetSocketTransportAddress(
                new InetSocketAddress(ipport[0], DataConvertHelper.toInt(ipport[1], 9300))));
    }
    
    result = client.admin().indices().exists(new IndicesExistsRequest(index)).actionGet().isExists();
    if(result) {
        result = client.admin().indices().delete(new DeleteIndexRequest(index)).actionGet().isAcknowledged();
    }
    
    client.admin().indices().create(new CreateIndexRequest(index)).actionGet().isAcknowledged();
    
    client.admin().indices().typesExists(new TypesExistsRequest(new String[] { index }, type)).actionGet().isExists();
    
    client.admin().indices().prepareAliases().addAlias(index, alias).get().isAcknowledged();
    
    client.admin().indices().prepareAliases().removeAlias(index, alias).get().isAcknowledged();
    
    client.prepareSearch(index).setSearchType(SearchType.DFS_QUERY_THEN_FETCH).get(TimeValue.timeValueMillis(15000));

    Map<String, Object> m = new HashMap<String, Object>();
    m.put("user", "kimchy");
    m.put("postDate", new Date());
    m.put("message", "trying out Elasticsearch");
    
    BulkRequestBuilder bulkRequest = client.prepareBulk();
    bulkRequest.add(client.prepareIndex("twitter", "tweet", "1").setSource(m));
    BulkResponse bulkResponse = bulkRequest.get();
    if (bulkResponse.hasFailures()) {
        System.out.println("Failed");
    }
    
    client.close();   
}