目录
- 一、创建springboot项目
- 二、pom.xml文件引入相关maven依赖
- 三、创建客户端对象
一、创建springboot项目
- 创建springboot项目步骤参考此博文链接:https://wwwxz.blog.csdn.net/article/details/91977374
二、pom.xml文件引入相关maven依赖
-
引入elasticsearch依赖
<!-- elasticsearch 依赖 --> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>7.8.0</version> </dependency> <!-- elasticsearch 的客户端 --> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.8.0</version> </dependency> <!-- elasticsearch 依赖 2.x 的 log4j --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.8.2</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.8.2</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.9</version> </dependency> <!-- junit 单元测试 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency>
三、创建客户端对象
-
创建客户端对象代码示例
package com.xz.esdemo.day1; import org.apache.http.HttpHost; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; import java.io.IOException; /** * @description: 客户端对象 * @author: xz */ public class EsClient { public static void main(String[] args) throws IOException { // 创建Elasticsearch客户端对象 RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost", 9200, "http")) ); // 关闭 Elasticsearch客户端连接 client.close(); } }
-
执行代码,查看控制台信息,如下图表示创建客户端对象ok
注意:9200 端口为 Elasticsearch 的 Web 通信端口 ,localhost 为启动 Elasticsearch 服务的主机名。