文章目录
使用Java transportclient API 操作Elasticsearch-6.8.4
1. 确定elasticsearch是否已经在本机运行
在浏览器中输入http://localhost:9200/
查看elasticsearch是否成功运行
如果出现如上图结果,则说明elasticsearch成功运行在本机,如果没有出现上图结果,可以先检查elsaticsearch开发环境。
2. 搭建maven工程,添加依赖
pom.xml文件配置如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>EHRelasticsearch</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<encoding>UTF-8</encoding>
</properties>
<dependencies>
<!-- elasticsearch的客户端 -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>6.8.4</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.8.4</version>
</dependency>
<!-- elasticsearch依赖2.x的log4j -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.13.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.13.2</version>
</dependency>
<!-- junit单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
其中elasticsearch
的版本为6.8.4
junit
依赖:
Junit是xUnit的一个子集,在c++,paython,java语言中测试框架的名字都不相同,xUnit是一套基于测试驱动开发的测试框架。
其中的断言机制:将程序预期的结果与程序运行的最终结果进行比对,确保对结果的可预知性。
https://blog.csdn.net/fulishafulisha/article/details/80158392
直接引用csdn博主fulisha_la的总结:
修饰符及其作用:
- @BeforeClass
修饰的方法会在所有方法被调用前执行,且该方法时静态的,所以当测试类被加载后就接着运行它,而且在内存中他只会存在一份实例,他比较适合加载配置文件(针对所有测试,只执行一次- @AfterClass
所修饰的方法通常用来对资源管理,如关闭数据库连接(针对所有测试,只执行一次 )- @Before和@After 会在每个测试方法前后各执行一次
- @Test:测试方法,在这里可以测试期望异常和超时时间
- @Ignore:忽略的测试方法
3. Java操作elasticsearch
直接上代码,连接es后再查找一条数据
package es;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import java.net.InetAddress;
/**
* @program: EHRelasticsearch
* @description: 使用java连接elasicsearch
* @author: huanshi2
* @create: 2020-04-30 18:00
* @email: 1557679224@qq.com
*/
public class ConnectEs {
public static void main(String[] args) {
try {
// 设置集群名称elasticsearch-cluster,Settings设置es的集群名称,使用的设计模式,链式设计模式、build设计模式。
Settings settings = Settings.builder().put("cluster.name", "elasticsearch-cluster").build();
// 读取es集群中的数据,创建client。
@SuppressWarnings("resource")
TransportClient client = new PreBuiltTransportClient(settings).addTransportAddresses(
new TransportAddress(InetAddress.getByName("localhost"), 9300));
// 搜索数据(.actionGet()方法是同步的,没有返回就等待)
// 方式是先去索引里面查询出索引数据,再去文档里面查询出数据。
GetResponse response = client.prepareGet("emr", "patient", "1").execute