Java操作ElasticSearch之创建客户端连接

Java操作ElasticSearch之创建客户端连接

ElasticSearch提供了主流开发语言的连接开发包 

新建的maven项目 添加如下依赖即可:


    org.elasticsearch.client
    transport
    5.5.2

连接代码:

package com.xhz.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();
    }
}

 

 

你可能感兴趣的:(Elasticsearch)