Elasticsearch教程(六) elasticsearch Client创建

Elasticsearch  创建Client有几种方式。

首先在 Elasticsearch  的配置文件 elasticsearch.yml中。定义cluster.name。如下:

 
  1. cluster.name: sojson-application

创建方式一:

 
  1. import static org.elasticsearch.node.NodeBuilder.*;
  2. //节点方式创建。
  3. Node node = nodeBuilder().clusterName("yourclustername").node();
  4. Client client = node.client();
  5. /*
  6. 还有很多节点方式的创建方式,查看下面的官网地址。
  7. https://www.elastic.co/guide/en/elasticsearch/client/java-api/2.0/node-client.html
  8. */

创建方式二:

 
  1. /**
  2. * 指定 ip地址创建
  3. */
  4. // on startup
  5. Client client = TransportClient.builder().build()
  6. .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host1"), 9300))
  7. .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host2"), 9300));
  8.  
  9. // on shutdown
  10. client.close();

创建方式三:

 
  1. //按集群名称创建
  2. Settings settings = Settings.settingsBuilder()
  3. .put("cluster.name", "sojson-application").build();
  4. Client client = TransportClient.builder().settings(settings).build();
  5. //Add transport addresses and do something with the client...

创建方式四:

 
  1. //同一内网Ip段,嗅的方式自己查找,组成集群。
  2. Settings settings = Settings.settingsBuilder()
  3. .put("client.transport.sniff", true).build();
  4. TransportClient client = TransportClient.builder().settings(settings).build();
  5. /*
  6. 客户端允许嗅其余的集群,它将数据节点添加到列表的机器使用。在这种情况下要注意,将使用的IP地址的其他节点开始(“publish”地址)。启用它,设置client.transport.sniff为 true:
  7. */

其实还有很多方式。具体使用哪种,看自己需求。

我的工具类:

 
  1. package com.sojson.core.elasticsearch.utils;
  2.  
  3. import java.net.InetAddress;
  4.  
  5. import org.elasticsearch.client.Client;
  6. import org.elasticsearch.client.transport.TransportClient;
  7. import org.elasticsearch.common.settings.Settings;
  8. import org.elasticsearch.common.transport.InetSocketTransportAddress;
  9.  
  10. import com.sojson.common.utils.LoggerUtils;
  11. import com.sojson.core.config.IConfig;
  12.  
  13. public class ESTools {
  14.  
  15. public final static Client client = build();
  16.  
  17. public final static Class clazz = ESTools.class;
  18.  
  19.  
  20.  
  21. /**
  22. * 创建一次
  23. * @return
  24. */
  25. private static Client build(){
  26. if(null != client){
  27. return client;
  28. }
  29. Client client = null;
  30. String ip = IConfig.get("es_ip");
  31. LoggerUtils.fmtDebug(clazz, "获取ESIP地址:%s", ip);
  32. try {
  33. LoggerUtils.fmtDebug(clazz, "创建Elasticsearch Client 开始");
  34. Settings settings = Settings
  35. .settingsBuilder()
  36. .put("cluster.name","sojson-application")
  37. .put("client.transport.sniff", true)
  38. .build();
  39. client = TransportClient.builder().settings(settings).build()
  40. .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ip), 9300));
  41. LoggerUtils.fmtDebug(clazz, "创建Elasticsearch Client 结束");
  42. } catch (Exception e) {
  43. LoggerUtils.fmtError(clazz, e, "创建Client异常");
  44. }
  45. return client;
  46. }
  47.  
  48. /**
  49. * 关闭
  50. */
  51. public static void close(){
  52. if(null != client){
  53. try {
  54. client.close();
  55. } catch (Exception e) {
  56.  
  57. }
  58. }
  59. }
  60.  
  61. }

 

后面的讲解,我都是采用这个工具类。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值