繁体   English   中英

elasticsearch和java-InvocationTargetException和NoNodeAvailableException

[英]elasticsearch & java - InvocationTargetException and NoNodeAvailableException

我正在尝试为Elasticsearch索引一个对象,但是除了发现很多死胡同之外,我没有运气...

我的应用程序类如下所示:

import com.google.gson.Gson;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;

import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import static org.elasticsearch.common.xcontent.XContentFactory.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.elasticsearch.client.transport.TransportClient;

@SpringBootApplication
public class Application implements CommandLineRunner {

    @Autowired
    private FilRepo repository;

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9200));

//      repository.deleteAll();
        List<Fil> listFiler = new ArrayList();
        // save a couple of customers
//      repository.save(new Fil("Steffen-1", 5, new Date(), new Date()));
//      repository.save(new Fil("Steffen-2", 10, new Date(), new Date()));

        // fetch all customers
        System.out.println("Filer funder med findAll():");
        System.out.println("-------------------------------");

        for (Fil f : repository.findAll()) {
            listFiler.add(f);
            IndexResponse response = client.prepareIndex("elasticsearch", "fil")
                    .setSource(jsonBuilder()
                            .startObject()
                            .field("fil", f.filNavn)
                            .field("size", f.filStr)
                            .field("created", f.created)
                            .field("modified", f.modified)
                            .endObject()
                    )
                    .get();





                    System.out.println("RESPONSE ER HER: " + response.toString());
        }
        System.out.println(listFiler);
        System.out.println();

        // fetch an individual customer
        System.out.println("Customer found with findByFilNavn('Steffen-1'):");
        System.out.println("--------------------------------");
        System.out.println(repository.findByFilNavn("Steffen-1"));

        client.close();

    }
}

我的Elasticsearch正在运行,已通过转到localhost:9200进行检查,它看起来像这样:

name    "WpUHj5-"
cluster_name    "elasticsearch"
cluster_uuid    "nEgvRKklRveOr1ltZSPbeA"
version 
number  "5.5.0"
build_hash  "260387d"
build_date  "2017-06-30T23:16:05.735Z"
build_snapshot  false
lucene_version  "6.6.0"
tagline "You Know, for Search"

我从mvn spring-boot:run得到的错误日志是: 在此处输入图片说明

在此处输入图片说明

以及我的Elasticsearch窗口上的错误:基本上是“外部主机关闭了现有连接”

在此处输入图片说明

首先,最好将配置部分与业务逻辑分开。 因此,应遵循用于Elasticsearch的配置bean(请注意, 用于通过TransportClient进行通信正确端口9300 ,端口9200是用于REST的,我认为这是这里的主要问题,但可能不是唯一的问题):

@Configuration
public class ElasticsearchConfig {

    private static final Logger LOG = getLogger(ElasticsearchConfig.class); 

    private Client client;

    @SuppressWarnings("resource")
    @Bean
    public Client getClient() throws UnknownHostException {

        Settings settings = Settings.builder()
                .put("cluster.name", "elasticsearch")
                .build();

        client = new PreBuiltTransportClient(settings)
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));

        LOG.info("--ElasticSearch--");
        Map<String, String> asMap = client.settings().getAsMap();
        asMap.forEach((k, v) -> LOG.info(k + " = " + v));
        LOG.info("--ElasticSearch--");

        return client;      
    }

    @PreDestroy
    public void closeClient() {
        client.close();
    }

}

初始化client对象后,出于测试目的,您可以检查您的逻辑(这里打印了客户端的设置),但是如果您可以将逻辑移至单独的服务并在注入以下内容后与client一起使用,则将更加干净:

@Autowired
private Client client;

Spring Boot的主要类:

@SpringBootApplication
@EnableElasticsearchRepositories(basePackages = "pl.jma.es.demo.esrepository")
public class EsDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(EsDemoApplication.class, args);
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM