是时候用 ES 拯救发际线啦

使用Java查询 elasticsearch提示远程连接关闭

Elasticsearch | 作者 zhangshuo24 | 发布于2016年10月17日 | 阅读数:9375

 
总是查询不成功,求讲解
es日志的信息是:[2016-10-17 19:23:48,029][WARN ][transport.netty          ] [node-1] exception caught on transport layer [[id: 0x1fce887e, /10.20.20.13:65479 => /10.20.20.13:9300]], closing connection
java.io.IOException: 远程主机强迫关闭了一个现有的连接。
    at sun.nio.ch.SocketDispatcher.read0(Native Method)
    at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:43)
    at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223)
    at sun.nio.ch.IOUtil.read(IOUtil.java:192)
    at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:380)
    at org.jboss.netty.channel.socket.nio.NioWorker.read(NioWorker.java:64)
    at org.jboss.netty.channel.socket.nio.AbstractNioWorker.process(AbstractNioWorker.java:108)
    at org.jboss.netty.channel.socket.nio.AbstractNioSelector.run(AbstractNioSelector.java:337)
    at org.jboss.netty.channel.socket.nio.AbstractNioWorker.run(AbstractNioWorker.java:89)
    at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:178)
    at org.jboss.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:108)
    at org.jboss.netty.util.internal.DeadLockProofWorker$1.run(DeadLockProofWorker.java:42)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
 
错误信息:Exception in thread "main" NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{10.20.20.13}{10.20.20.13:9300}]]
    at org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:290)
    at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:207)
    at org.elasticsearch.client.transport.support.TransportProxyClient.execute(TransportProxyClient.java:55)
    at org.elasticsearch.client.transport.TransportClient.doExecute(TransportClient.java:288)
    at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:359)
    at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:86)
    at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:56)
    at elasticsearch.com.CreateIndex.main(CreateIndex.java:37)
 
Java调用代码
    public static void main(String[] args) throws Exception {
        Settings settings = Settings.settingsBuilder().put("cluster.name", "my-application")
                .build();
        client = TransportClient.builder().settings(settings).build();
        client = TransportClient.builder().build()
                  .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("10.20.20.13"), 9300));
        MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery("queryName", "\"张硕\"");
        //WildcardQueryBuilder queryBuilder = QueryBuilders.wildcardQuery("empname", "*emp*");        
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        String json = searchSourceBuilder.query(queryBuilder).toString();
        
        SearchResponse response = client.prepareSearch("dept")
                .setTypes("employee")
                .setQuery(queryBuilder)
                .setFrom(0).setSize(2)
                .execute()
                .actionGet();    
        for (SearchHit hit : response.getHits().getHits()) {
            System.out.print(hit.getId() + "~");
            System.out.println(hit.getSourceAsString());
        }
        client.close();
    }
es配置文件的信息,# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
#       Before you set out to tweak and tune the configuration, make sure you
#       understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please see the documentation for further information on configuration options:
# <http://www.elastic.co/guide/en ... gt%3B
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
  cluster.name: my-application
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
  node.name: node-1
#
# Add custom attributes to the node:
#
  node.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
# path.data: /path/to/data
#
# Path to log files:
#
# path.logs: /path/to/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
# bootstrap.mlockall: true
#
# Make sure that the `ES_HEAP_SIZE` environment variable is set to about half the memory
# available on the system and that the owner of the process is allowed to use this limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
  network.host: 10.20.20.13
#
# Set a custom port for HTTP:
#
  http.port: 9200
  
  transport.tcp.port: 9300
  
  transport.tcp.compress: true
  
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en ... gt%3B
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when new node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
# discovery.zen.ping.unicast.hosts: ["host1", "host2"]
#
# Prevent the "split brain" by configuring the majority of nodes (total number of nodes / 2 + 1):
#
# discovery.zen.minimum_master_nodes: 3
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en ... gt%3B
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
# gateway.recover_after_nodes: 3
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en ... gt%3B
#
# ---------------------------------- Various -----------------------------------
#
# Disable starting multiple nodes on a single system:
#
# node.max_local_storage_nodes: 1
#
# Require explicit names when deleting indices:
#
# action.destructive_requires_name: true
已邀请:

medcl - 今晚打老虎。

赞同来自: zhangshuo24

13ip 稳定么,如果有多个节点,可以设置 sniff 参数来感知其他节点,

Settings settings = Settings.settingsBuilder() .put("client.transport.sniff", true).build();
https://www.elastic.co/guide/e ... .html

要回复问题请先登录注册