引入依赖

引入的依赖版本必须和正在运行的Elasticsearch版本一致

1
2
3
4
5
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>5.5.2</version>
</dependency>

实现

Java代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.ledao;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.net.InetAddress;

/**
* @author LeDao
* @company
* @create 2021-12-15 2:59
*/
public class Test {

public static void main(String[] args) throws Exception {
String host = "192.168.0.116";
int port = 9300;
TransportClient client = new PreBuiltTransportClient(Settings.EMPTY).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), port));
System.out.println(client);
client.close();
}
}

运行结果

出现划红线的信息说明与Elasticsearch连接成功