ElasticSearch:设置传输客户端

时间:2017-04-05 13:05:34

标签: java eclipse java-ee elasticsearch maven-3

我是使用ElasticSearch Java API的新手,我想创建与我的实例的连接。

我按照这里的说明操作: getting started

所以我得到以下代码(就像例子一样简单!):

TransportClient mClient = new PreBuiltTransportClient(Settings.EMPTY)
            .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host1"), 
                    9300));

但是我遇到了一个关于导入类的问题:Eclipse无法找到的TransportClient和Settings ......

这是maven导入:

 <dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>5.3.0</version>
</dependency>
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>transport</artifactId>
    <version>5.3.0</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.7</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.7</version>
</dependency>

我试图搜索回合此问题,但未找到解决方案...... Eclipse发现PreBuiltTransportClient没有任何问题。

这是mvn依赖的结果:tee

[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ poc ---
[INFO] fr.consortnt:poc:war:0.0.1-SNAPSHOT
[INFO] +- junit:junit:jar:3.8.1:test
[INFO] +- javax:javaee-web-api:jar:6.0:provided
[INFO] +- org.elasticsearch.client:transport:jar:5.3.0:compile
[INFO] |  +- org.elasticsearch:elasticsearch:jar:5.3.0:compile
[INFO] |  |  +- org.apache.lucene:lucene-core:jar:6.4.1:compile
[INFO] |  |  +- org.apache.lucene:lucene-analyzers-common:jar:6.4.1:compile
[INFO] |  |  +- org.apache.lucene:lucene-backward-codecs:jar:6.4.1:compile
[INFO] |  |  +- org.apache.lucene:lucene-grouping:jar:6.4.1:compile
[INFO] |  |  +- org.apache.lucene:lucene-highlighter:jar:6.4.1:compile
[INFO] |  |  +- org.apache.lucene:lucene-join:jar:6.4.1:compile
[INFO] |  |  +- org.apache.lucene:lucene-memory:jar:6.4.1:compile
[INFO] |  |  +- org.apache.lucene:lucene-misc:jar:6.4.1:compile
[INFO] |  |  +- org.apache.lucene:lucene-queries:jar:6.4.1:compile
[INFO] |  |  +- org.apache.lucene:lucene-queryparser:jar:6.4.1:compile
[INFO] |  |  +- org.apache.lucene:lucene-sandbox:jar:6.4.1:compile
[INFO] |  |  +- org.apache.lucene:lucene-spatial:jar:6.4.1:compile
[INFO] |  |  +- org.apache.lucene:lucene-spatial-extras:jar:6.4.1:compile
[INFO] |  |  +- org.apache.lucene:lucene-spatial3d:jar:6.4.1:compile
[INFO] |  |  +- org.apache.lucene:lucene-suggest:jar:6.4.1:compile
[INFO] |  |  +- org.elasticsearch:securesm:jar:1.1:compile
[INFO] |  |  +- net.sf.jopt-simple:jopt-simple:jar:5.0.2:compile
[INFO] |  |  +- com.carrotsearch:hppc:jar:0.7.1:compile
[INFO] |  |  +- joda-time:joda-time:jar:2.9.5:compile
[INFO] |  |  +- org.yaml:snakeyaml:jar:1.15:compile
[INFO] |  |  +- com.fasterxml.jackson.core:jackson-core:jar:2.8.6:compile
[INFO] |  |  +- com.fasterxml.jackson.dataformat:jackson-dataformat-smile:jar:2.8.6:compile
[INFO] |  |  +- com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:jar:2.8.6:compile
[INFO] |  |  +- com.fasterxml.jackson.dataformat:jackson-dataformat-cbor:jar:2.8.6:compile
[INFO] |  |  +- com.tdunning:t-digest:jar:3.0:compile
[INFO] |  |  +- org.hdrhistogram:HdrHistogram:jar:2.1.6:compile
[INFO] |  |  \- net.java.dev.jna:jna:jar:4.2.2:compile
[INFO] |  +- org.elasticsearch.plugin:transport-netty3-client:jar:5.3.0:compile
[INFO] |  |  \- io.netty:netty:jar:3.10.6.Final:compile
[INFO] |  +- org.elasticsearch.plugin:transport-netty4-client:jar:5.3.0:compile
[INFO] |  |  +- io.netty:netty-buffer:jar:4.1.7.Final:compile
[INFO] |  |  +- io.netty:netty-codec:jar:4.1.7.Final:compile
[INFO] |  |  +- io.netty:netty-codec-http:jar:4.1.7.Final:compile
[INFO] |  |  +- io.netty:netty-common:jar:4.1.7.Final:compile
[INFO] |  |  +- io.netty:netty-handler:jar:4.1.7.Final:compile
[INFO] |  |  +- io.netty:netty-resolver:jar:4.1.7.Final:compile
[INFO] |  |  \- io.netty:netty-transport:jar:4.1.7.Final:compile
[INFO] |  +- org.elasticsearch.plugin:reindex-client:jar:5.3.0:compile
[INFO] |  |  \- org.elasticsearch.client:rest:jar:5.3.0:compile
[INFO] |  |     +- org.apache.httpcomponents:httpclient:jar:4.5.2:compile
[INFO] |  |     +- org.apache.httpcomponents:httpcore:jar:4.4.5:compile
[INFO] |  |     +- org.apache.httpcomponents:httpasyncclient:jar:4.1.2:compile
[INFO] |  |     +- org.apache.httpcomponents:httpcore-nio:jar:4.4.5:compile
[INFO] |  |     +- commons-codec:commons-codec:jar:1.10:compile
[INFO] |  |     \- commons-logging:commons-logging:jar:1.1.3:compile
[INFO] |  +- org.elasticsearch.plugin:lang-mustache-client:jar:5.3.0:compile
[INFO] |  |  \- com.github.spullara.mustache.java:compiler:jar:0.9.3:compile
[INFO] |  \- org.elasticsearch.plugin:percolator-client:jar:5.3.0:compile
[INFO] +- org.apache.logging.log4j:log4j-api:jar:2.7:compile
[INFO] +- org.apache.logging.log4j:log4j-core:jar:2.7:compile
[INFO] +- taglibs:standard:jar:1.1.2:compile
[INFO] +- jstl:jstl:jar:1.2:runtime
[INFO] +- org.postgresql:postgresql:jar:9.4.1212:compile
[INFO] \- org.hibernate:hibernate-entitymanager:jar:4.2.15.Final:compile
[INFO]    +- org.jboss.logging:jboss-logging:jar:3.1.0.GA:compile
[INFO]    +- dom4j:dom4j:jar:1.6.1:compile
[INFO]    +- org.javassist:javassist:jar:3.18.1-GA:compile
[INFO]    +- org.jboss.spec.javax.transaction:jboss-transaction-api_1.1_spec:jar:1.0.1.Final:compile
[INFO]    +- org.hibernate.javax.persistence:hibernate-jpa-2.0-api:jar:1.0.1.Final:compile
[INFO]    +- org.hibernate.common:hibernate-commons-annotations:jar:4.0.2.Final:compile
[INFO]    \- org.hibernate:hibernate-core:jar:4.2.15.Final:compile
[INFO]       \- antlr:antlr:jar:2.7.7:compile

关于会发生什么的任何想法? 我很确定maven配置不是问题,因为我可以从elasticsearch导入一些需要的类......

提前致谢!

1 个答案:

答案 0 :(得分:0)

我几年来不是eclipse用户,但这看起来像是eclipse / maven配置问题,所以我建议你查看eclipse configuration with maven

我说这是配置问题,因为我有一个应用程序使用以下maven依赖关系配置:

    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>transport</artifactId>
        <version>${elasticsearch.version}</version>
    </dependency>
    <!-- Log Dependencies required by elasticsearch 5 -->
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>${log4j.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>${log4j.version}</version>
    </dependency>

请注意,您不需要添加 org.elasticsearch (列表中的第一个)依赖项,因为这是在运行elasticsearch embedded which is not supported anymore时使用的,但它是&#39 ;如果这是你的情况,那么可能会留在那里,如果你只是建立一个你不需要第一个依赖的客户。

P.S - 如果您的eclipse / maven配置正确,那么您可能在运行应用程序之前忘记 mvn clean install 了吗?