无法运行" Hello World" Elastic-Search的Java客户端

时间:2016-03-02 22:55:53

标签: java maven elasticsearch apache-storm

我无法运行一个非常基本的程序来创建一个" Hello World" Elastic-Search的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)

我的最终目标是在风暴中使用路由节点客户端。

非常感谢任何帮助。

谢谢!

1 个答案:

答案 0 :(得分:2)

从1.4升级到2.4时,我遇到了同样的问题。我不是100%的原因,但对我来说,这是指定单播主机的原因。我通过zip下载默认运行ES。

首先,我设置了客户端节点的端口(即,我正在处理的应用程序)超出默认范围,我认为我需要这样做以避免在同一个盒子上运行2个节点的冲突。当我这样做时,我的客户永远找不到主人。事实证明,zen发现仅在其自己的范围内ping 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");
}