NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{q6LSZwJwQyqI-cRxYQ9vOQ}{xxx.xxx.xxx.xxx}{xxx.xxx.xxx.xxx:9300}] ]

1. 出现这个问题,看配置elasticsearch.yml是否OK

https://www.cnblogs.com/Uzai/p/11333196.html

a.) 查看elastic是否正常启动 , 加 -d 是后台启动, 不加-d 可以看到一些信息,相当于一个控制台,可以看到一些启动信息,java 客户端操作的时候, 可以看到相关信息。

sh /usr/local/es/elasticsearch-5.5.2/bin/elasticsearch -d 

b.) 

netstat -nltp

  

 

若能看到这些, 就不是配置问题了。

2.检查 java api 客户段链接是否有误:

pom.xml

       <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>5.5.2</version>
        </dependency>

 

连接demo:

import java.net.InetAddress;

import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.google.gson.JsonObject;

public class TestIndex {

    private static String host="公网ip"; // 服务器地址
    private static int port=9300; // 端口
    
    private TransportClient client=null;
    
    @SuppressWarnings({ "resource", "unchecked" })
    @Before
    public void getCient()throws Exception{
        Settings settings = Settings.builder().put("cluster.name", "my-application").build();
        client = new PreBuiltTransportClient(settings)
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host),port));
    }
    
    @After
    public void close(){
        if(client!=null){
            client.close();
        }
    }
    
    /**
     * 创建索引 添加文档
     * @throws Exception
     */
    @Test
    public void testIndex()throws Exception{
        JsonObject jsonObject=new JsonObject();
        jsonObject.addProperty("name", "java编程思想");
        jsonObject.addProperty("publishDate", "2012-11-11");
        jsonObject.addProperty("price", 100);
        
        IndexResponse response=client.prepareIndex("book", "java", "1")
            .setSource(jsonObject.toString(), XContentType.JSON).get();
        System.out.println("索引名称:"+response.getIndex());
        System.out.println("类型:"+response.getType());
        System.out.println("文档ID:"+response.getId());
        System.out.println("当前实例状态:"+response.status());
    }
    
    /**
     * 根据id获取文档
     * @throws Exception
     */
    @Test
    public void testGet()throws Exception{
        GetResponse response=client.prepareGet("book", "java", "1").get();
        System.out.println(response.getSourceAsString());
    }
    
    /**
     * 根据id修改文档
     * @throws Exception
     */
    @Test
    public void testUpdate()throws Exception{
        JsonObject jsonObject=new JsonObject();
        jsonObject.addProperty("name", "java编程思想2");
        jsonObject.addProperty("publishDate", "2012-11-12");
        jsonObject.addProperty("price", 102);
        
        UpdateResponse response=client.prepareUpdate("book", "java", "1").setDoc(jsonObject.toString(), XContentType.JSON).get();
        System.out.println("索引名称:"+response.getIndex());
        System.out.println("类型:"+response.getType());
        System.out.println("文档ID:"+response.getId());
        System.out.println("当前实例状态:"+response.status());
    }
    
    /**
     * 根据id删除文档
     * @throws Exception
     */
    @Test
    public void testDelete()throws Exception{
        DeleteResponse response=client.prepareDelete("book", "java", "1").get();
        System.out.println("索引名称:"+response.getIndex());
        System.out.println("类型:"+response.getType());
        System.out.println("文档ID:"+response.getId());
        System.out.println("当前实例状态:"+response.status());
    }
}

 

posted @ 2019-08-11 00:17  new_boys  阅读(830)  评论(0编辑  收藏  举报