當前位置: 首頁>>代碼示例>>Java>>正文


Java Client.prepareBulk方法代碼示例

本文整理匯總了Java中org.elasticsearch.client.Client.prepareBulk方法的典型用法代碼示例。如果您正苦於以下問題:Java Client.prepareBulk方法的具體用法?Java Client.prepareBulk怎麽用?Java Client.prepareBulk使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.elasticsearch.client.Client的用法示例。


在下文中一共展示了Client.prepareBulk方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: BulkIndex

import org.elasticsearch.client.Client; //導入方法依賴的package包/類
private static void BulkIndex(Client client) throws IOException {

        BulkRequestBuilder requestBuilder = client.prepareBulk();

        requestBuilder.add(client.prepareIndex("twitter","tweet","4")
                .setSource(jsonBuilder()
                        .startObject()
                        .field("user","niekaijie")
                        .field("school","beiyou")
                        .field("address","haidianqu")
                        .endObject())
        );
        requestBuilder.add(client.prepareIndex("twitter","tweet","3")
                .setSource(jsonBuilder()
                        .startObject()
                        .field("user","林誌穎aa")
                        .field("school","台灣大學")
                        .field("address","台灣")
                        .endObject())
        );
        BulkResponse response = requestBuilder.get();


    }
 
開發者ID:hs-web,項目名稱:hsweb-learning,代碼行數:25,代碼來源:ElasticSearch.java

示例2: createBulkRequest

import org.elasticsearch.client.Client; //導入方法依賴的package包/類
public static BulkRequestBuilder createBulkRequest(final Client client, final StreamInput streamInput, final String index)
        throws IOException {
    final BulkRequestBuilder builder = client.prepareBulk();
    final BulkRequest request = builder.request();
    request.readFrom(streamInput);
    if (index != null) {
        request.requests().stream().forEach(req -> {
            if (req instanceof DeleteRequest) {
                ((DeleteRequest) req).index(index);
            } else if (req instanceof DeleteByQueryRequest) {
                ((DeleteByQueryRequest) req).indices(index);
            } else if (req instanceof IndexRequest) {
                ((IndexRequest) req).index(index);
            } else if (req instanceof UpdateRequest) {
                ((UpdateRequest) req).index(index);
            } else if (req instanceof UpdateByQueryRequest) {
                ((UpdateByQueryRequest) req).indices(index);
            } else {
                throw new ElasticsearchException("Unsupported request in bulk: " + req);
            }
        });
    }
    return builder;
}
 
開發者ID:codelibs,項目名稱:elasticsearch-indexing-proxy,代碼行數:25,代碼來源:RequestUtils.java

示例3: deleteES

import org.elasticsearch.client.Client; //導入方法依賴的package包/類
private static void deleteES(Client client) {
  BulkRequestBuilder bulkRequest = client.prepareBulk();
  SearchResponse response = client.prepareSearch(index).setTypes(type)
      .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
      .setQuery(QueryBuilders.matchAllQuery())
      .setFrom(0).setSize(20).setExplain(true).execute().actionGet();
  System.out.println("length: " + response.getHits().getHits().length);
  if (response.getHits().getHits().length != 0) {
    for (SearchHit hit : response.getHits()) {
      String id = hit.getId();
      System.out.println("id: " + id);
      bulkRequest.add(client.prepareDelete(index, type, id).request());
    }
    BulkResponse bulkResponse = bulkRequest.get();
    if (bulkResponse.hasFailures()) {
      for (BulkItemResponse item : bulkResponse.getItems()) {
        System.out.println(item.getFailureMessage());
      }
    } else {
      System.out.println("delete ok");
    }
  } else {
    System.out.println("delete ok");
  }
}
 
開發者ID:MoneZhao,項目名稱:elasticsearch,代碼行數:26,代碼來源:App.java

示例4: restoreDataFromFile

import org.elasticsearch.client.Client; //導入方法依賴的package包/類
public void restoreDataFromFile(Client client, String indexname, File datafile) throws IOException {

		if (datafile.getName().endsWith(".data")) {
			JsonUtilities jsonutil = new JsonUtilities();
			Map<String, Object> map;

			reader = new BufferedReader(new FileReader(datafile));
			BulkRequestBuilder bulkRequest = client.prepareBulk();

			String tempString = null;
			// 一次讀一行,讀入null時文件結束
			while ((tempString = reader.readLine()) != null) {
				map = jsonutil.JsonToMap(tempString);

				bulkRequest.add(client.prepareIndex(indexname, (String) map.get("_type"), (String) map.get("_id"))
						.setSource(jsonutil.MapToJson((Map<String, Object>) map.get("_source")), XContentType.JSON));

			}
			bulkRequest.execute().actionGet();
			client.admin().indices().prepareRefresh(indexname).get();

			logmsg.clear();
			logmsg.put("Action", "Restore data");
			logmsg.put("Index", indexname);
			logmsg.put("FileName", datafile.getAbsolutePath());
			logger.info(jsonutil.MapToJson(logmsg));
		}

	}
 
開發者ID:jiashiwen,項目名稱:elasticbak,代碼行數:30,代碼來源:RestoreEsIndex.java

示例5: CopyIndex

import org.elasticsearch.client.Client; //導入方法依賴的package包/類
public void CopyIndex(Client sourceclient, String sourceindex, Client targetclient, String targetindex) {
	// TODO Auto-generated method stub
	count = 0;
	QueryBuilder qb = QueryBuilders.matchAllQuery();

	try {

		SearchResponse scrollResp = sourceclient.prepareSearch(sourceindex)
				.addSort(FieldSortBuilder.DOC_FIELD_NAME, SortOrder.ASC).setScroll(new TimeValue(60000))
				.setQuery(qb).setSize(500).execute().actionGet();

		while (true) {
			final BulkRequestBuilder bulkRequest = targetclient.prepareBulk();
			for (SearchHit hit : scrollResp.getHits().getHits()) {
				bulkRequest.add(targetclient.prepareIndex(targetindex, hit.getType().toString(), hit.getId())
						.setSource(hit.getSourceAsString()));
				count++;
			}

			bulkRequest.execute();
			scrollResp = sourceclient.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(60000))
					.execute().actionGet();

			logger.info(count + " documents putted!!");

			if (scrollResp.getHits().getHits().length == 0) {
				break;
			}
		}

		logger.info("copy index " + sourceindex + " " + count + " documents" + " to " + targetindex + " complete");
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}
 
開發者ID:jiashiwen,項目名稱:elasticbak,代碼行數:37,代碼來源:ElasticsearchCopyIndex.java

示例6: CopyIndexByQueryDsl

import org.elasticsearch.client.Client; //導入方法依賴的package包/類
public void CopyIndexByQueryDsl(Client sourceclient, String sourceindex, Client targetclient, String targetindex,
		String DSL) {
	count = 0;
	// 單線程scroll,bulk寫入
	WrapperQueryBuilder wrapper = new WrapperQueryBuilder(DSL);

	try {
		SearchResponse scrollResp = sourceclient.prepareSearch(sourceindex)
				.addSort(FieldSortBuilder.DOC_FIELD_NAME, SortOrder.ASC).setScroll(new TimeValue(60000))
				.setQuery(wrapper).setSize(500).execute().actionGet();

		while (true) {
			final BulkRequestBuilder bulkRequest = targetclient.prepareBulk();
			for (SearchHit hit : scrollResp.getHits().getHits()) {
				bulkRequest.add(targetclient.prepareIndex(targetindex, hit.getType().toString(), hit.getId())
						.setSource(hit.getSourceAsString()));
				count++;
			}

			bulkRequest.execute();

			scrollResp = sourceclient.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(60000))
					.execute().actionGet();
			logger.info(count + " documents putted!!");

			if (scrollResp.getHits().getHits().length == 0) {
				break;
			}
		}
		
		logger.info("copy index " + sourceindex + " " + count + " documents" + " to " + targetindex + " complete");
		
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
開發者ID:jiashiwen,項目名稱:elasticbak,代碼行數:37,代碼來源:ElasticsearchCopyIndex.java

示例7: importES

import org.elasticsearch.client.Client; //導入方法依賴的package包/類
private static void importES(Client client) throws IOException {
  String filePath = "es.txt";
  File file = new File(filePath);
  if (!file.exists()) {
    System.out.println("文件不存在");
    return;
  }
  try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
    //讀取剛才導出的ES數據

    String string;
    int count = 0;
    //開啟批量插入
    BulkRequestBuilder bulkRequest = client.prepareBulk();
    while ((string = br.readLine()) != null) {
      if (Objects.equals(string, "")) continue;
      JsonObject jsonObject = new JsonObject(string);
      System.out.println(jsonObject);
      String ESindex;
      if (jsonObject.containsKey("ESindex")) {
        ESindex = (String) jsonObject.remove("ESindex");
      } else {
        ESindex = index;
      }
      String EStype;
      if (jsonObject.containsKey("EStype")) {
        EStype = (String) jsonObject.remove("EStype");
      } else {
        EStype = type;
      }
      ++count;
      bulkRequest.add(client.prepareIndex(ESindex, EStype, String.valueOf(count)).setSource(jsonObject.toString()));
      //每一千條提交一次
      if (count % 1000 == 0) {
        bulkRequest.execute().actionGet();
        System.out.println("提交了:" + count);
      }
    }
    bulkRequest.execute().actionGet();
    System.out.println("插入完畢");
  }
}
 
開發者ID:MoneZhao,項目名稱:elasticsearch,代碼行數:43,代碼來源:App.java


注:本文中的org.elasticsearch.client.Client.prepareBulk方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。