2014-11-21 65 views
7

有谁知道什么是Java的配置等价的:春数据ElasticSearch TransportClient Java的配置

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
http://www.springframework.org/schema/data/elasticsearch 
http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd"> 
    <elasticsearch:transport-client id="client" cluster-nodes="localhost:9300,someip:9300" 
/> 
</beans> 

我特别想用nodeBuilder()做到这一点。

回答

11

看看到Spring数据文件以了解ElasticSearch:

@Configuration 
    @EnableElasticsearchRepositories(basePackages = "org/springframework/data/elasticsearch/repositories") 
     static class Config { 

     @Value("${esearch.port}") int port; 
     @Value("${esearch.host}") String hostname; 

     @Bean 
     public ElasticsearchOperations elasticsearchTemplate() { 
     return new ElasticsearchTemplate(client()); 
     } 

     @Bean 
     public Client client(){ 
      TransportClient client= new TransportClient(); 
      TransportAddress address = new InetSocketTransportAddress(hostname, port); 
      client.addTransportAddress(address); 
      return client; 
     } 
    } 

Elasticsearch Repositories 2.1.2基于注解配置

春季数据Elasticsearch库的支持不仅可以 通过XML激活命名空间,还通过JavaConfig使用注释 。

+0

以上代码不创建传输客户端,而不是它创建了一个节点的客户端和是Java的配置等价以下xml配置 的 2014-11-21 09:33:48

+0

请参阅使用TransportClient更新,您可以使用该构造函数调用ElasticsearchTemplate – 2014-11-21 10:23:15

+2

还可以将Settings settings = ImmutableSettings.settingsBuilder()。put(“”,“”)。build()传递给TransportClient的构造函数以配置它... – 2014-11-21 10:25:49