2017-08-07 39 views
0

我試圖指數的目的是elasticsearch,但我有,除了尋找有很多死角的沒有運氣...elasticsearch和爪哇 - 的InvocationTargetException和NoNodeAvailableException

我的應用程序類看起來是這樣的:

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彈簧引導得到:RU n爲: enter image description here

enter image description here

和我Elasticsearch窗口上的錯誤: 基本上說

enter image description here

回答

0

首先 「現有的連接是通過外部主機關閉」,將配置部分與業務邏輯分開將是一件好事。所以配置Bean的elasticsearch應如下(請注意,correct port for communication via TransportClient是,端口9200是休息,我認爲這是主要的問題在這裏,但也許不是唯一的一個):

@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

@Autowired 
private Client client; 

Mai n彈簧靴級:

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

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

}