ElasticSearch(四):使用Java连接ElasticSearch集群

时间:2022-05-31 01:19:29
 public class ESIndexMapping {

     private static String host="192.168.56.3"; // 服务器地址
private static int port=9300; // 端口 public static final String CLUSTER_NAME = "my-application"; //集群名称 private TransportClient client=null; private static Settings settings= Settings.builder()
.put("cluster.name",CLUSTER_NAME)
.put("client.transport.sniff", true)
.build(); //获取客户端
@SuppressWarnings({ "resource", "unchecked" })
@Before
public void getClient() throws Exception {
try {
client = new PreBuiltTransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host),port));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} //关闭客户端
@After
public void close() {
if(client!=null) {
client.close();
}
}
}