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(相当于包名):
![这里写图片描述](https://img-blog.csdn.net/20161212114309400?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbmFwb2F5/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
2.2在pom.xml中添加以下依赖
<dependencies>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>5.1.1</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>
</dependencies>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
添加依赖后maven会自动导包,如下图所示:
至此所有的jar包都导入完成了。
所有的jar包列表如下,如果出现导包错误一定查看一下jar包是否下载完整:
HdrHistogram-2.1.6.jar
commons-codec-1.10.jar
commons-logging-1.1.3.jar
compiler-0.9.3.jar
elasticsearch-5.1.1.jar
hppc-0.7.1.jar
httpasyncclient-4.1.2.jar
httpclient-4.5.2.jar
httpcore-4.4.5.jar
httpcore-nio-4.4.5.jar
jackson-core-2.8.1.jar
jackson-dataformat-cbor-2.8.1.jar
jackson-dataformat-smile-2.8.1.jar
jackson-dataformat-yaml-2.8.1.jar
jna-4.2.2.jar
joda-time-2.9.5.jar
jopt-simple-5.0.2.jar
lang-mustache-client-5.1.1.jar
log4j-api-2.7.jar
log4j-core-2.7.jar
lucene-analyzers-common-6.3.0.jar
lucene-backward-codecs-6.3.0.jar
lucene-core-6.3.0.jar
lucene-grouping-6.3.0.jar
lucene-highlighter-6.3.0.jar
lucene-join-6.3.0.jar
lucene-memory-6.3.0.jar
lucene-misc-6.3.0.jar
lucene-queries-6.3.0.jar
lucene-queryparser-6.3.0.jar
lucene-sandbox-6.3.0.jar
lucene-spatial-6.3.0.jar
lucene-spatial-extras-6.3.0.jar
lucene-spatial3d-6.3.0.jar
lucene-suggest-6.3.0.jar
netty-3.10.6.Final.jar
netty-buffer-4.1.6.Final.jar
netty-codec-4.1.6.Final.jar
netty-codec-http-4.1.6.Final.jar
netty-common-4.1.6.Final.jar
netty-handler-4.1.6.Final.jar
netty-resolver-4.1.6.Final.jar
netty-transport-4.1.6.Final.jar
percolator-client-5.1.1.jar
reindex-client-5.1.1.jar
rest-5.1.1.jar
securesm-1.1.jar
snakeyaml-1.15.jar
t-digest-3.0.jar
transport-5.1.1.jar
transport-netty3-client-5.1.1.jar
transport-netty4-client-5.1.1.jar
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
三.配置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,内容如下:
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("127.0.0.1"), 9300))
//搜索数据
GetResponse response = client.prepareGet("blog", "article", "1").execute().actionGet()
//输出结果
System.out.println(response.getSourceAsString())
//关闭client
client.close()
} catch (Exception e) {
e.printStackTrace()
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
运行结果:
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"}
工程目录以及运行结果的截图如下:
![这里写图片描述](https://img-blog.csdn.net/20161212114413870?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbmFwb2F5/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
五.参考资料
参考资料主要为官网文档:
Maven Repository
Log4j 2 Logger
六、代码下载
2017年4月18日更新。
代码下载地址:
http://download.csdn.net/detail/napoay/9817575
完整jar包在es5/target/dependency目录下。