Java操作ElasticSearch之创建客户端连接
ElasticSearch提供了主流开发语言的连接开发包
新建的maven项目 添加如下依赖即可:
<dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>transport</artifactId> <version>5.5.2</version> </dependency>
连接代码:
package com.java1234.es; import java.net.InetAddress; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.InetSocketTransportAddress; import org.elasticsearch.transport.client.PreBuiltTransportClient; public class Test { private static String host="192.168.1.108"; // 服务器地址 private static int port=9300; // 端口 public static void main(String[] args) throws Exception{ TransportClient client = new PreBuiltTransportClient(Settings.EMPTY) .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(Test.host), Test.port)); System.out.println(client); client.close(); } }
这里有个Setting 等后面讲到集群再详解;