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

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

问题描述

我无法运行一个非常基本的创建Hello WorldJava客户端的弹性搜索的程序。



该文档非常简洁,在这种情况下。



这是我的代码:

  find。 -type f 

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

这两个文件如下所示。



包含代码的Java文件:

 包示例; 

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\]; //在这里使用实际的ES主节点的IP
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 void createEsClient()
{
设置设置= 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.0encoding =UTF-8?>< project xmlns =http://maven.apache。 org / POM / 4.0.0xmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexsi: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>
<属性>
< project.build.sourceEncoding> UTF-8< /project.build.sourceEncoding>
< / properties>
<依赖关系>
<依赖关系>
< 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 />
<执行>
< execution>
< phase> package< / phase>
< goals>
< goal> shade< / goal>
< / goals>
< configuration>
< artifactSet>
< excludes>
< exclude> org.slf4j:*< / exclude>
< exlcude> com.esotericsoftware.kryo:kryo:*< / exlcude>
< / excludes>
< / artifactSet>
<变压器>
< 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 )错误注入构造函数,java.lang.IllegalStateException:这是一个用于支持涉及构造函数的循环引用的代理。我们代理的对象尚未构建。请等待注射完成后才能使用此对象。 
在org.elasticsearch.node.service.NodeService。< init>(未知来源)
,同时在org.elasticsearch上为参数5定位org.elasticsearch.node.service.NodeService
。 action.admin.cluster.node.stats.TransportNodesStatsAction。< init>(未知源)
同时定位org.elasticsearch.action.admin.cluster.node.stats.TransportNodesStatsAction
参数2在org .elasticsearch.cluster.InternalClusterInfoService。< init>(未知源)
同时查找org.elasticsearch.cluster.InternalClusterInfoService
,同时查找org上的参数3的org.elasticsearch.cluster.ClusterInfoService
.elasticsearch.cluster.routing.allocation.AllocationService。< init>(未知源)
,同时在org.elasticsearch.cluster上查找参数3的org.elasticsearch.cluster.routing.allocation.AllocationService
。 metadata.MetaDataCreateIndexService。< init>(未知来源)
同时查找org.elasticsearch.cluste org.elasticsearch.snapshots.RestoreService
导致的参数:java.bat .lang.IllegalStateException:这是一个用于支持涉及构造函数的循环引用的代理。我们代理的对象尚未构建。请等待注射完成后才能使用此对象。
在org.elasticsearch.common.inject.internal.ConstructionContext $ DelegatingInvocationHandler.invoke(ConstructionContext.java:103)
在com.sun.proxy $ Proxy11.setNodeService(未知来源)
在org.elasticsearch.node.service.NodeService。< init>(NodeService.java:77)
at sun.reflect.GeneratedConstructorAccessor4.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance (DelegatingConstructorAccessorImpl.java:45)
在java.lang.reflect.Constructor.newInstance(Constructor.java:422)
在org.elasticsearch.common.inject.DefaultConstructionProxyFactory $ 1.newInstance(DefaultConstructionProxyFactory.java: 50)
在org.elasticsearch.common.inject.ConstructorInjector.construct(ConstructorInjector.java:86)
在org.elasticsearch.common.inject.ConstructorBindingImpl $ Factory.get(ConstructorBindingImpl.java:104)
在org.elasticsearch.common.inject.ProviderToInternalFactoryAdapter $ 1.call(Provid erToInternalFactoryAdapter.java:47)
在org.elasticsearch.common.inject.InjectorImpl.callInContext(InjectorImpl.java:887)
在org.elasticsearch.common.inject.ProviderToInternalFactoryAdapter.get(ProviderToInternalFactoryAdapter.java: 43)
在org.elasticsearch.common.inject.Scopes $ 1 $ 1.get(Scopes.java:59)
在org.elasticsearch.common.inject.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:46)
在org.elasticsearch.common.inject.SingleParameterInjector.inject(SingleParameterInjector.java:42)
...更多这样的行
在org.elasticsearch.common.inject.InternalFactoryToProviderAdapter.get( InternalFactoryToProviderAdapter.java:46)
在org.elasticsearch.common.inject.InjectorBuilder $ 1.call(InjectorBuilder.java:201)
在org.elasticsearch.common.inject.InjectorBuilder $ 1.call(InjectorBuilder。 java:193)
在org.elasticsearch.common.inject.InjectorImpl.callInContext(InjectorImpl.java:880)
在org.elasticsearch.common.inject.InjectorBuilder.loadEagerSingletons(InjectorBuilder.java:193)
在org.elasticsearch.common.inject.InjectorBuilder.injectDynamically(InjectorBuilder.java:175)
在org.elasticsearch .common.inject.InjectorBuilder.build(InjectorBuilder.java:110)
在org.elasticsearch.common.inject.Guice.createInjector(Guice.java:93)
在org.elasticsearch.common.inject .Guice.createInjector(Guice.java:70)
在org.elasticsearch.common.inject.ModulesBuilder.createInjector(ModulesBuilder.java:46)
在org.elasticsearch.node.Node。< init> ;(Node.java:200)
在org.elasticsearch.node.Node。< init>(Node.java:128)
在org.elasticsearch.node.NodeBuilder.build(NodeBuilder.java :145)
在org.elasticsearch.node.NodeBuilder.node(NodeBuilder.java:152)
在examples.EsRoutingNodeClient.createEsClient(EsRoutingNodeClient.java:30)
在examples.EsRoutingNodeClient。 < INIT>(EsRoutingNodeClient.java: 46)
在examples.EsRoutingNodeClient.main(EsRoutingNodeClient.java:51)

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

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



谢谢!

解决方案

p>从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。目录} / es / logs

network.host = 127.0.0.1

discovery.zen.minimum_master_nodes = 1

代码

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


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

private属性getProps(){
try {
String profile = getProfile();
log.info(加载ES属性为env:{},配置文件);
ClassPathResource resource = new ClassPathResource(es / es。+ profile +。properties);

return PropertiesLoaderUtils.loadProperties(resource);

} catch(IOException e){
log.error(Can not read property file);
抛出新的RuntimeException(e);
}
}

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


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.

Here is my code:

find . -type f

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

Both files are shown below.

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>

Finally I run it as follows:

mvn clean package

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

And the exception I get is:

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)

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

Any help would be very much appreciated.

Thanks!

解决方案

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.

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.

here's my config

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

code

@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”弹性搜索的Java客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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