gpt4 book ai didi

elasticsearch - org.elasticsearch.common.netty.channel.ChannelException : Failed to create a selector

转载 作者:行者123 更新时间:2023-12-02 23:34:05 25 4
gpt4 key购买 nike

我正在尝试使用 API 将一些记录推送到 Elasticsearch 中。基于 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)

最佳答案

这是代码的问题

public void sendStatusHistoryToES(JsonNode node,int count) throws IOException,
ParseException {

String lat = null,lon = null;
TransportClient client = getClient().addTransportAddress(
new InetSocketTransportAddress("localhost", 9300));

对于每个批量请求,都会创建另一个客户端并打开一个新端口。
最终它不再提供端口异常(exception)。

将客户端初始化放在构造函数或其他东西中,事情应该没问题。

将以下内容放入构造函数中 -
    TransportClient client = getClient().addTransportAddress(
new InetSocketTransportAddress("localhost", 9300));

关于elasticsearch - org.elasticsearch.common.netty.channel.ChannelException : Failed to create a selector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33434304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com