本文整理汇总了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();
}
示例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;
}
示例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");
}
}
示例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));
}
}
示例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();
}
}
示例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();
}
}
示例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("插入完毕");
}
}