2016-03-02 43 views
2

我無法運行創建Elastic-Search的「Hello World」java客戶端的非常基本的程序。無法運行Elastic-Search的「Hello World」Java客戶端

在這種情況下,文檔非常簡潔。

這裏是我的代碼:

find . -type f 

./pom.xml 
./src/main/java/examples/EsRoutingNodeClient.java 

兩個文件如下所示。

包含代碼

的Java文件:

package examples; 

import org.elasticsearch.action.index.IndexResponse; 
import org.elasticsearch.client.Client; 
import org.elasticsearch.common.settings.Settings; 
import org.elasticsearch.node.NodeBuilder; 

public class EsRoutingNodeClient 
{ 
    private static final String ZEN_DISCOVERY_UNICAST_HOSTS = "[\"10.10.10.10:9200\"]"; // Used an actual ES master node's IP here 
    private static final String ES_PATH_HOME = "/Users/appuser/work/software/elasticsearch/dummy-path-home/"; 
    private static final String ES_CLUSTER_NAME = "my-cluster"; 
    private Client client; 

    private void createEsClient() 
    { 
     Settings settings = Settings.settingsBuilder() 
       .put("http.enabled", false) 
       .put("discovery.zen.ping.multicast.enabled", false) 
       .put("discovery.zen.ping.unicast.hosts", ZEN_DISCOVERY_UNICAST_HOSTS) 
       .put("discovery.zen.minimum_master_nodes", 1) 
       .put("path.home", ES_PATH_HOME) 
       .build(); 
     client = 
       NodeBuilder.nodeBuilder() 
       .settings(settings) 
       .clusterName(ES_CLUSTER_NAME) 
       .data(false) 
       .client(true) 
       .node().client(); 
    } 

    public EsRoutingNodeClient() 
    { 
     createEsClient(); 
    } 

    public static void main (String args[]) 
    { 
     new EsRoutingNodeClient(); 
    } 
} 

的pom.xml:

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>examples</groupId> 
    <artifactId>es-node-client</artifactId> 
    <version>0.0.3-SNAPSHOT</version> 
    <packaging>jar</packaging> 
    <name>es-node-client</name> 
    <properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    </properties> 
    <dependencies> 
    <dependency> 
     <groupId>org.elasticsearch</groupId> 
     <artifactId>elasticsearch</artifactId> 
     <version>2.2.0</version> 
    </dependency> 
    </dependencies> 
    <build> 
    <plugins> 
     <plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-shade-plugin</artifactId> 
     <version>2.4.3</version> 
     <configuration /> 
     <executions> 
      <execution> 
      <phase>package</phase> 
      <goals> 
       <goal>shade</goal> 
      </goals> 
      <configuration> 
       <artifactSet> 
       <excludes> 
        <exclude>org.slf4j:*</exclude> 
        <exlcude>com.esotericsoftware.kryo:kryo:*</exlcude> 
       </excludes> 
       </artifactSet> 
       <transformers> 
       <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" /> 
       <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> 
        <mainClass>examples.EsRoutingNodeClient</mainClass> 
       </transformer> 
       </transformers> 
      </configuration> 
      </execution> 
     </executions> 
     </plugin> 
    </plugins> 
    </build> 
</project> 

最後,我運行它,如下所示:

mvn clean package 

java -jar target/es-node-client-0.0.3-SNAPSHOT.jar 

而且我得到的例外是:

194) Error injecting constructor, java.lang.IllegalStateException: This is a proxy used to support circular references involving constructors. The object we're proxying is not constructed yet. Please wait until after injection has completed to use this object. 
    at org.elasticsearch.node.service.NodeService.<init>(Unknown Source) 
    while locating org.elasticsearch.node.service.NodeService 
    for parameter 5 at org.elasticsearch.action.admin.cluster.node.stats.TransportNodesStatsAction.<init>(Unknown Source) 
    while locating org.elasticsearch.action.admin.cluster.node.stats.TransportNodesStatsAction 
    for parameter 2 at org.elasticsearch.cluster.InternalClusterInfoService.<init>(Unknown Source) 
    while locating org.elasticsearch.cluster.InternalClusterInfoService 
    while locating org.elasticsearch.cluster.ClusterInfoService 
    for parameter 3 at org.elasticsearch.cluster.routing.allocation.AllocationService.<init>(Unknown Source) 
    while locating org.elasticsearch.cluster.routing.allocation.AllocationService 
    for parameter 3 at org.elasticsearch.cluster.metadata.MetaDataCreateIndexService.<init>(Unknown Source) 
    while locating org.elasticsearch.cluster.metadata.MetaDataCreateIndexService 
    for parameter 5 at org.elasticsearch.snapshots.RestoreService.<init>(Unknown Source) 
    while locating org.elasticsearch.snapshots.RestoreService 
Caused by: java.lang.IllegalStateException: This is a proxy used to support circular references involving constructors. The object we're proxying is not constructed yet. Please wait until after injection has completed to use this object. 
    at org.elasticsearch.common.inject.internal.ConstructionContext$DelegatingInvocationHandler.invoke(ConstructionContext.java:103) 
    at com.sun.proxy.$Proxy11.setNodeService(Unknown Source) 
    at org.elasticsearch.node.service.NodeService.<init>(NodeService.java:77) 
    at sun.reflect.GeneratedConstructorAccessor4.newInstance(Unknown Source) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422) 
    at org.elasticsearch.common.inject.DefaultConstructionProxyFactory$1.newInstance(DefaultConstructionProxyFactory.java:50) 
    at org.elasticsearch.common.inject.ConstructorInjector.construct(ConstructorInjector.java:86) 
    at org.elasticsearch.common.inject.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:104) 
    at org.elasticsearch.common.inject.ProviderToInternalFactoryAdapter$1.call(ProviderToInternalFactoryAdapter.java:47) 
    at org.elasticsearch.common.inject.InjectorImpl.callInContext(InjectorImpl.java:887) 
    at org.elasticsearch.common.inject.ProviderToInternalFactoryAdapter.get(ProviderToInternalFactoryAdapter.java:43) 
    at org.elasticsearch.common.inject.Scopes$1$1.get(Scopes.java:59) 
    at org.elasticsearch.common.inject.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:46) 
    at org.elasticsearch.common.inject.SingleParameterInjector.inject(SingleParameterInjector.java:42) 
    ... more such lines 
    at org.elasticsearch.common.inject.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:46) 
    at org.elasticsearch.common.inject.InjectorBuilder$1.call(InjectorBuilder.java:201) 
    at org.elasticsearch.common.inject.InjectorBuilder$1.call(InjectorBuilder.java:193) 
    at org.elasticsearch.common.inject.InjectorImpl.callInContext(InjectorImpl.java:880) 
    at org.elasticsearch.common.inject.InjectorBuilder.loadEagerSingletons(InjectorBuilder.java:193) 
    at org.elasticsearch.common.inject.InjectorBuilder.injectDynamically(InjectorBuilder.java:175) 
    at org.elasticsearch.common.inject.InjectorBuilder.build(InjectorBuilder.java:110) 
    at org.elasticsearch.common.inject.Guice.createInjector(Guice.java:93) 
    at org.elasticsearch.common.inject.Guice.createInjector(Guice.java:70) 
    at org.elasticsearch.common.inject.ModulesBuilder.createInjector(ModulesBuilder.java:46) 
    at org.elasticsearch.node.Node.<init>(Node.java:200) 
    at org.elasticsearch.node.Node.<init>(Node.java:128) 
    at org.elasticsearch.node.NodeBuilder.build(NodeBuilder.java:145) 
    at org.elasticsearch.node.NodeBuilder.node(NodeBuilder.java:152) 
    at examples.EsRoutingNodeClient.createEsClient(EsRoutingNodeClient.java:30) 
    at examples.EsRoutingNodeClient.<init>(EsRoutingNodeClient.java:46) 
    at examples.EsRoutingNodeClient.main(EsRoutingNodeClient.java:51) 

194 errors 
    at org.elasticsearch.common.inject.internal.Errors.throwCreationExceptionIfErrorsExist(Errors.java:360) 
    at org.elasticsearch.common.inject.InjectorBuilder.injectDynamically(InjectorBuilder.java:178) 
    at org.elasticsearch.common.inject.InjectorBuilder.build(InjectorBuilder.java:110) 
    at org.elasticsearch.common.inject.Guice.createInjector(Guice.java:93) 
    at org.elasticsearch.common.inject.Guice.createInjector(Guice.java:70) 
    at org.elasticsearch.common.inject.ModulesBuilder.createInjector(ModulesBuilder.java:46) 
    at org.elasticsearch.node.Node.<init>(Node.java:200) 
    at org.elasticsearch.node.Node.<init>(Node.java:128) 
    at org.elasticsearch.node.NodeBuilder.build(NodeBuilder.java:145) 
    at org.elasticsearch.node.NodeBuilder.node(NodeBuilder.java:152) 
    at examples.EsRoutingNodeClient.createEsClient(EsRoutingNodeClient.java:30) 
    at examples.EsRoutingNodeClient.<init>(EsRoutingNodeClient.java:46) 
    at examples.EsRoutingNodeClient.main(EsRoutingNodeClient.java:51) 

我的最終目標是在風暴中使用路由節點客戶端。

任何幫助將是非常讚賞。

謝謝!

+0

似乎是一個大的pom.xml的一個HelloWorld –

+0

減少pom.xml中 – user2250246

+0

你需要一個節點客戶端的設置僅是'HTTP .enabled',所有其他設置不是用於ES客戶端,而是用於ES服務器。 – Val

回答

2

從1.4升級到2.4時,我遇到了同樣的問題。我不是100%的原因,但對我來說,這是指定單播主機的原因。我使用默認值通過zip下載運行ES。

起初,我爲客戶端節點(即我正在開發的應用程序)設置了端口以外的默認範圍,我認爲需要這樣做以避免衝突在同一個盒子上運行2個節點。當我這樣做時,我的客戶永遠找不到主人。事實證明,禪宗發現只在本地範圍內擁有5個本地端口。在我弄明白之前(通過閱讀https://github.com/elastic/elasticsearch/blob/master/core/src/main/java/org/elasticsearch/discovery/zen/ping/unicast/UnicastZenPing.java),我在unicast.host中添加了一個問題,以解決您遇到的問題。刪除這兩個設置後,我的應用程序現在連接到我的ES服務器。

這裏是我的配置

cluster.name=elasticsearch 

node.name=local-dev 
node.master=false 
node.data=false 

path.home=${project.build.directory}/es 
path.data=${project.build.directory}/es/data 
path.logs=${project.build.directory}/es/logs 

network.host=127.0.0.1 

discovery.zen.minimum_master_nodes=1 

代碼

@Bean 
public Node elasticSearchNode() throws Exception { 
    Settings settings = Settings.settingsBuilder().put(getProps()).build(); 
    return nodeBuilder() 
      .settings(settings) 
      .client(true) 
      .node(); 
} 


@Bean 
public Client elasticSearchClient(Node node) throws Exception { 
    return node.client(); 
} 

private Properties getProps() { 
    try { 
     String profile = getProfile(); 
     log.info("Loading ES properties for env: {}", profile); 
     ClassPathResource resource = new ClassPathResource("es/es."+profile+".properties"); 

     return PropertiesLoaderUtils.loadProperties(resource); 

    } catch (IOException e) { 
     log.error("Can not read property file"); 
     throw new RuntimeException(e); 
    } 
} 

private String getProfile() { 
    return System.getProperty("env", "local"); 
}