开发者学堂课程【ElasticSearch 入门精讲:连接 ES_介绍】学习笔记,与课程紧密连接,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/631/detail/9981
连接 ES_介绍
ES JavaAPI 之连接 ES
通过 TransportClient 接口,我们可以不启动节点就可以和 ES 集群进行通信,它需要指定 ES 集群中其中-台或者多合机器 IP 地址和端口(默认9300)
public class ElasticSearchTest {
private static final int PORT = 9300;
private TransportClient client;
@Before
public void setUp() {
client = TransportClient.builder).build0);
InetSocketTransportAddress ista =
newInetSocketTransportAddress(new
InetSocketAddress("master", PORT);
client.addTransportAddresses(ista);
System.out.printn("cluster.name="+ client.settings)-.get("cluster.name");
@After
public void cleanUp() {client.close(;}
前提: Maven pom 依赖:
<dependency>
<groupId>org.elasticsearch</ groupId>
<artifactId> elasticsearch</artifactId>
<version>6.5.3</version>↓
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<arti factId>jackson-databind </artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<vers ion>2.0.0</version>
</ dependency>
1)如果需要使用其他名称的集群默认是 elasticsearch),需要如下设置
Settingg settings = Settings .builder ()
.put("cluster .name", "nCName") .bu1ld() ;
TransportClient client = TranaportClient .builder ()
/**
*初始化
*/
@Before
public void setUp0 throws UnknownHostException {
Settings settings = Settings.builder0.put(" cluster.name", "bigdata").build0;
client = new PreBuiltTransportClient(settings);
//用来指定集群中的节点 TCP/IP 协议,es 服务器的端口号是: 9300; HTTP 协议 ,端口号是9200TransportAddressjanson01=new TransportAddress(InetAddress.getByName("JANSON01") 9300);
TransportAddressjanson02=new TransportAddress(InetAddress getByName("JANSON02"), 9300);
TransportAddressjanson03=new TransportAddress(InetAddress getByName("JANSON03"), 9300);
client.addTransportAddresses(ianson01, janson02, janson03);
/**
*测试TransportClient实例是否正常创建
*/
@Test
"ic void testEnv( {
lger.info("TransportClient类的实例信息是: " + client);
/**
试 JAVA 客户端能否正常查询指定索引库中的信息
public void testNomalGet0) {
//需求:
查询索引库 bigdata 中 type 之 product,id 为1的 document 信息。