我尝试使用基于API
请求的Bulk
将一些记录推送到弹性搜索中。在7000条记录中,只有大约1000多条记录进入elastic
搜索没有任何问题。但这个数字不时变化。 Elastic
搜索没有任何拒绝。我已经使用过的API
和执行相同的异常。
public int getData(String currentDate,String count)
throws ClientProtocolException, IOException, ParseException {
int x=Integer.parseInt(count);
String url= *Some URL*
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);
BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
JsonFactory f = new MappingJsonFactory();
JsonParser jp = f.createJsonParser(rd);
JsonToken current = null;
while (jp.nextToken() != JsonToken.END_OBJECT) {
try {
current = jp.nextToken();
JsonNode node = jp.readValueAsTree();
if (node != null) {
x=x+1;
sendDataToES(node,x);
} else {
break;
}
} catch (Exception e) {
logger.info("ES data push failed");
}
}
return x;
}
public void sendDataToES(JsonNode node,int count) throws IOException,
ParseException {
String lat = null,lon = null;
TransportClient client = getClient().addTransportAddress(
new InetSocketTransportAddress("localhost", 9300));
BulkRequestBuilder bulkRequest = client.prepareBulk();
XContentBuilder jsonObject = XContentFactory.jsonBuilder()
.startObject();
Iterator<Entry<String, JsonNode>> entry = node.fields();
Entry<String, JsonNode> a = null;
while (entry.hasNext()) {
try {
a = entry.next();
if (a.getKey().equals("longitude")) {
lon=a.getValue().asText();
}
if(a.getKey().equals("latitude")){
lat=a.getValue().asText();
}
jsonObject.field(a.getKey()).value(a.getValue().asText());
} catch (Exception e) {
}
}
Map<String, String> map= new HashMap<String, String>();
map.put("lat", lat);
map.put("lon", lon);
jsonObject.field("location")
.value(map)
.endObject();
try
{
bulkRequest.add(client.prepareIndex("bulk_data", "data",
String.valueOf((count))).setSource(jsonObject));
BulkResponse bulkResponse = bulkRequest.execute().actionGet();
logger.info("Data has been posted to elastic search");
if (bulkResponse.hasFailures()) {
logger.info("Got failure while pushing data to ES");
}
}
catch(Exception e)
{
logger.info("Got failure while pushing data to ES");
}
client.close();
}
我收到了以下Exception
,
org.elasticsearch.common.netty.channel.ChannelException: Failed to create a selector.
at org.elasticsearch.common.netty.channel.socket.nio.AbstractNioSelector.openSelector(AbstractNioSelector.java:362)
at org.elasticsearch.common.netty.channel.socket.nio.AbstractNioSelector.<init>(AbstractNioSelector.java:100)
at org.elasticsearch.common.netty.channel.socket.nio.AbstractNioWorker.<init>(AbstractNioWorker.java:52)
at org.elasticsearch.common.netty.channel.socket.nio.NioWorker.<init>(NioWorker.java:45)
at org.elasticsearch.common.netty.channel.socket.nio.NioWorkerPool.newWorker(NioWorkerPool.java:44)
at org.elasticsearch.common.netty.channel.socket.nio.NioWorkerPool.newWorker(NioWorkerPool.java:28)
at org.elasticsearch.common.netty.channel.socket.nio.AbstractNioWorkerPool.init(AbstractNioWorkerPool.java:80)
at org.elasticsearch.common.netty.channel.socket.nio.NioWorkerPool.<init>(NioWorkerPool.java:39)
at org.elasticsearch.common.netty.channel.socket.nio.NioWorkerPool.<init>(NioWorkerPool.java:33)
at org.elasticsearch.transport.netty.NettyTransport.createClientBootstrap(NettyTransport.java:319)
at org.elasticsearch.transport.netty.NettyTransport.doStart(NettyTransport.java:242)
at org.elasticsearch.common.component.AbstractLifecycleComponent.start(AbstractLifecycleComponent.java:85)
at org.elasticsearch.transport.TransportService.doStart(TransportService.java:153)
at org.elasticsearch.common.component.AbstractLifecycleComponent.start(AbstractLifecycleComponent.java:85)
at org.elasticsearch.client.transport.TransportClient.<init>(TransportClient.java:197)
at org.elasticsearch.client.transport.TransportClient.<init>(TransportClient.java:133)
答案 0 :(得分:1)
这是代码
的问题public void sendStatusHistoryToES(JsonNode node,int count) throws IOException,
ParseException {
String lat = null,lon = null;
TransportClient client = getClient().addTransportAddress(
new InetSocketTransportAddress("localhost", 9300));
对于每个批量请求,将创建另一个客户端并打开一个新端口。 最终它不再给出端口例外。
将客户端初始化放在构造函数或其他东西中,事情应该没问题。
将以下内容放在构造函数中 -
TransportClient client = getClient().addTransportAddress(
new InetSocketTransportAddress("localhost", 9300));