本地main方法连接elasticsearch方法
package com.lcb.soa.search.v2.test;
import org.junit.Before;
import org.junit.Test;
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;
import java.net.UnknownHostException;
/**
* *
* * @author liuhuapeng
* * @date 2021-10-27 16:39
* * @version 1.0
*
*/
public class JavaEsTest {
private String IP;
private int PORT;
@Before
public void init(){
//这里可以填写自己的Es的IP
this.IP = "192.168.6.206";
//端口可以不用改
this.PORT = 9300;
}
@Test
public void esClient(){
try {
Settings settings = Settings.builder().put("cluster.name", "my-application").build();
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddresses(new TransportAddress(InetAddress.getByName(IP), PORT));
System.out.println(client.toString());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}