Java API 环境准备
ES 本身是由Java 语言开发的,他也为我们提供了Java API调用的接口。这在实践中也是比较常用的。接下来我们用maven 工程来来管理我们的测试项目。 maven依赖如下:
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<es.version>7.8.0</es.version>
<log4j.version>2.8.2</log4j.version>
<jackson.version>2.9.9</jackson.version>
</properties>
<dependencies>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${es.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>${es.version}</version>
</dependency>
<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>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
</dependencies>
项目创建成功之后,在编写一个基本的测试类。项目的结构如下图。 编写测试类:
package com.maomao.elastic.search.api;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import java.io.IOException;
public class APITest {
public static void main(String[] args) {
HttpHost host = new HttpHost("127.0.0.1", 9200, "http");
RestClientBuilder builder = RestClient.builder(host);
RestHighLevelClient client = new RestHighLevelClient(builder);
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
ES 客户端主要的测试客户端就是构建一个REST 客户端。 目前7.8 版本的推荐类为:RestHighLevelClient 。 运行测试如果不报错,则表示项目的环境搭建已经没有问题了。
|