无法运行“Hello World"Elastic-Search的Java客户端 [英] Unable to run "Hello World" Java client of Elastic-Search

查看:31
本文介绍了无法运行“Hello World"Elastic-Search的Java客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法运行一个非常基本的程序来创建 Elastic-Search 的Hello World"java 客户端.

I am unable to run a very basic program of creating a "Hello World" java client of Elastic-Search.

文档非常简洁地说明了在这种情况下该怎么做.

The documentation is extremely terse about what to do in such cases.

这是我的代码:

find . -type f

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

两个文件如下所示.

包含代码的 Java 文件:

Java file containing the code:

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)

我的最终目标是在 Storm 中使用路由节点客户端.

My ultimate aim is to use a routing node client in storm.

非常感谢任何帮助.

谢谢!

推荐答案

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

While upgrading from 1.4 to 2.4 I ran into this same issue. I'm not 100% why, but for me it was something to due with specifying the unicast host. I'm running ES via the zip download with the defaults.

起初我将客户端节点(即我正在开发的应用程序)的端口设置在默认范围之外,认为我需要这样做以避免在同一台机器上运行 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 服务器.

At first I had set the ports for Client Node (ie, the app i'm working on) to be outside the default range thinking I need to do this to avoid conflicts running 2 nodes on the same box. When I did this my client could never find the master. Turns out that the zen discovery only pings 5 local ports in its own range. Before I figured this out (by reading https://github.com/elastic/elasticsearch/blob/master/core/src/main/java/org/elasticsearch/discovery/zen/ping/unicast/UnicastZenPing.java) I added in a unicast.host to get the same issue you are having. After removing both settings my app is now connecting to my ES server.

这是我的配置

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");
}

这篇关于无法运行“Hello World"Elastic-Search的Java客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆