ES之三:Elasticsearch 5.0下Java API使用指南

一.2.X到5.X

Elasticsearch 2.x使用Java api把elasticsearch安装包下的lib文件夹下的jar文件全部加入到工程类路径即可,换到5.x就不适用了.创建Clien的代码:

Settings settings = Settings.builder().put("cluster.name", "elasticsearch").build();

TransportClient client = new PreBuiltTransportClient(settings)
                    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));

只使用安装包下的jar文件会出现PreBuiltTransportClient cannot be resolved to a type的错误,原因是缺少jar包.下面记录一下如何在5.X中使用Java api

二.创建maven工程

2.1Eclipse中新建maven工程

打开eclipse,file->other->maven project: 
 
创建group id(相当于工程名)和artifact id(相当于包名): 

2.2在pom.xml中添加以下依赖

<dependencies>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>5.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.8</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.8</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpasyncclient</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore-nio</artifactId>
            <version>4.4.5</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

添加依赖后maven会自动导包,如下图所示: 

至此所有的jar包都导入完成了。 

三.配置log4j2

在src/main/resources文件夹下新建文件log4j2.properties,加入以下log4 
j2的配置:

    appender.console.type = Console
    appender.console.name = console
    appender.console.layout.type = PatternLayout rootLogger.level = info rootLogger.appenderRef.console.ref = console

四.创建Client并搜索数据

首先启动elasticsearch,我这里使用的是5.1.1,创建一个新的索引:

 curl -XPUT "http://localhost:9200/blog"

添加一条文档

 curl -XPUT "http://localhost:9200/blog/article/1" -d '{            
    "title":"test",
    "content":"test content" }

在 src/main/java/目录下新建TestEsClient.java,内容如下:

package es.test.esclient;

import java.net.InetAddress;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

public class TestEsClient {

    public static void main(String[] args) {

        try {

            //设置集群名称
            Settings settings = Settings.builder().put("cluster.name", "elasticsearch").build();
            //创建client
            TransportClient client = new PreBuiltTransportClient(settings)
                    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("10.202.11.117"), 9300));            //搜索数据
            GetResponse response = client.prepareGet("blog", "article", "1").execute().actionGet();
            //输出结果
            System.out.println(response.getSourceAsString());
            //关闭client
            client.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

运行结果:

no modules loaded
loaded plugin [org.elasticsearch.index.reindex.ReindexPlugin]
loaded plugin [org.elasticsearch.percolator.PercolatorPlugin]
loaded plugin [org.elasticsearch.script.mustache.MustachePlugin]
loaded plugin [org.elasticsearch.transport.Netty3Plugin]
loaded plugin [org.elasticsearch.transport.Netty4Plugin]
{            
    "title":"test",
    "content":"test content"
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值