es报错:org.elasticsearch.action.ActionRequestValidationException: Validation Failed: 1: no requests ad
org.elasticsearch.action.ActionRequestValidationException: Validation Failed: 1: no requests added;
at org.elasticsearch.action.ValidateActions.addValidationError(ValidateActions.java:29)
at org.elasticsearch.action.bulk.BulkRequest.validate(BulkRequest.java:476)
at org.elasticsearch.action.TransportActionNodeProxy.execute(TransportActionNodeProxy.java:52)
at org.elasticsearch.client.transport.support.InternalTransportClient$1.doWithNode(InternalTransportClient.java:109)
at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:205)
at org.elasticsearch.client.transport.support.InternalTransportClient.execute(InternalTransportClient.java:106)
at org.elasticsearch.client.support.AbstractClient.bulk(AbstractClient.java:167)
at org.elasticsearch.client.transport.TransportClient.bulk(TransportClient.java:370)
at org.elasticsearch.action.bulk.BulkRequestBuilder.doExecute(BulkRequestBuilder.java:166)
at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:91)
at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:65)
at org.elasticsearch.action.ActionRequestBuilder.get(ActionRequestBuilder.java:73)
at com.easypec.dataValidate.util.EsUtil.bulkIndex(EsUtil.java:263)
at com.easypec.dataValidate.client.ValidateLog.addItemToEs(ValidateLog.java:82)
at com.easypec.dataValidate.client.ValidateLog.main(ValidateLog.java:61)
解决办法:
IndexRequest 中需要设置主键 indexRequest.id(UUID.randomUUID()+"");// 设置主键加上这段代码
把请求的放入 BulkRequestBuilder 中 prepareBulk.add(indexRequest);
/**
* 批量入库
* 集合插入到es中
* @param list
*/
public void bulkIndex(List<EsNginxLogEntity> list) { try { logger.info("bulkIndex {} start"); BulkRequestBuilder prepareBulk = getTransportClient().prepareBulk(); //循环集合 for (int i = 0; i < list.size(); i++) { EsNginxLogEntity esn = list.get(i); IndexRequest indexRequest = new IndexRequest(indexName, indexType); HashMap<String, Object> hashMap = new HashMap<String, Object>(); // hashMap.put("domain", esn.getDomain()); String uuid=UUID.randomUUID().toString(); if(esn.getCount()==0){ hashMap.put("count", esn.getCount()); hashMap.put("validate_type",esn.getType()); }else{ hashMap.put("ip", esn.getIp()); hashMap.put("local_time", esn.getLocal_time()); hashMap.put("rawmessage", esn.getRawMessage()); hashMap.put("count", esn.getCount()); hashMap.put("validate_type",esn.getType()); } indexRequest.source(hashMap); indexRequest.id(uuid); prepareBulk.add(indexRequest); if (i % 2000 == 0) { BulkResponse bulkResponse = prepareBulk.get(); check(bulkResponse); } } //批量插入 BulkResponse bulkResponse = prepareBulk.get(); check(bulkResponse); } catch (Exception e) { e.printStackTrace(); } }