Elasticsearch 5.X下JAVA API使用指南
阿新 • • 發佈:2018-12-26
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.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>
新增依賴後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
三.配置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();
}
}
}
執行結果:
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"}
工程目錄以及執行結果的截圖如下:
五.參考資料
參考資料主要為官網文件: