elasticsearch client
你可以用client做很多事情:
- 在集群中执行index, get, delete, search,update 操作
- 在集群中执行administrative tasks
- 如果你想再程序中嵌入elasticsearch或者执行单元测试,你可开启一个Node
获取elasticsearch的client是很简单的事情,有两种方式可以实现:
1.第一种是通过在程序中创建一个嵌入elasticsearch节点(Node),使之成为elasticsearch集群的一部分,然后通过这个节点来与elasticsearch集群通信。Node方式创建嵌入节点的方式如下:
// 启动节点
Node node = new NodeBuilder().node();
Client client = node.client();
// 关闭节点
node.close();
当启动一个节点,它会自动加入同网段的elasticsearch集群,一个前提就是elasticsearch的集群名(cluster.name)这个参数要设置一致。默认的话启动一个节点,elasticsearch集群会自动给它分配一些索引的分片,如果想让这个节点仅仅作为一个客户端而不去保存数据,可以把node.data设置成false或 node.client设置成true:
Node node = new NodeBuilder().client(true).node();
还有一种情况是你并不想把节点加入集群,只想用它进行单元测试时,就要启动一个“本地”的elasticsearch,这里“本地”指的是在JVM的级别下运行,即两个不同的elasticsearch节点运行在同一个JVM中时会组成一个集群。它需要把节点的local参数设置成true,下面是例子:
Node node = new NodeBuilder().local(true).node();
2.是用TransportClient这个接口和elasticsearch集群通信。TransportClient方式:通过TransportClient这个接口,可以不启动节点就可以和elasticsearch集群进行通信,它需要指定elasticsearch集群中其中一台或多台机的ip地址和端口:
Client client = new TransportClient()
.addTransportAddress(new InetSocketTransportAddress("host1", 9300))
.addTransportAddress(new InetSocketTransportAddress("host2", 9300));
client.close();
如果需要更改集群名(默认是elasticsearch),需要如下设置:
Settings settings = ImmutableSettings.settingsBuilder()
.put("cluster.name", "myClusterName").build();
Client client = new TransportClient(settings);
可以设置client.transport.sniff为true来使客户端去嗅探整个集群的状态,把集群中其它机器的ip地址加到客户端中,这样做的好处是一般不用手动设置集群里所有集群的ip到连接客户端,它会自动帮你添加,并且自动发现新加入集群的机器。代码实例如下:
Settings settings = ImmutableSettings.settingsBuilder()
.put("client.transport.sniff", true).build();
Client client = new TransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress("host1", 9300));
注意:自动嗅探至少需要提供集群中一个节点的地址。
| Parameter | Description |
|---|---|
| client.transport.ignore_cluster_name | Set to true to ignore cluster name validation of connected nodes. (since 0.19.4) |
| client.transport.ping_timeout | The time to wait for a ping response from a node. Defaults to 5s. |
| client.transport.nodes_sampler_interval | How often to sample / ping the nodes listed and connected. Defaults to 5s. |
elasticsearch client的更多相关文章
- org.elasticsearch.client.transport.NoNodeAvailableException
SpringBoot连接elasticsearch异常 2018-09-11 16:03:43.692 ERROR 8684 --- [ main] o.s.boot.SpringApplicatio ...
- ElasticSearch client API
从运行结果看并没有打印节点信息出来 从结果看出来,集群节点信道打印出来了,不过这种方法有个问题,就是当我们连接的节点挂掉了,就没法连接整个集群了,这个时候我们就利用他的一个嗅探的功能. 从这里我们可以 ...
- Elasticsearch教程(六) elasticsearch Client创建
Elasticsearch 创建Client有几种方式. 首先在 Elasticsearch 的配置文件 elasticsearch.yml中.定义cluster.name.如下: cluster ...
- Failed to instantiate [org.elasticsearch.client.transport.TransportClient]
Springboot 集成 ElasticSearch,springboot报错如下: Error starting ApplicationContext. To display the auto-c ...
- maven包引入问题ClassNotFoundException: org.elasticsearch.client.Cancellable
业务需要,做搜索功能,在springboot聚合项目下,新建了es模块module 但是在引入elasticsearch依赖的时候,出现了问题 引入相应依赖后 <dependency> & ...
- Elasticsearch client node 启动时出现警告:exception caught on transport layer 及java.net.NoRouteToHostException: No route to host
发现该问题源自发现kibana不能打开sense,并且看见elasticsearch插件处于服务不可用状态,但是在client node上curl localhost:9200发现能够返回ES基本信息 ...
- 一个C++的ElasticSearch Client
ElasticSearch官方是没有提供C++的client的:因此决定自己写一个,命名为ESClient https://github.com/ATinyAnt/ESClient(手下留星 star ...
- ES使用org.elasticsearch.client.transport.NoNodeAvailableException: No node available 错误解决方法
1) 端口错 client = new TransportClient().addTransportAddress(new InetSocketTransportAddress(ipAddress, ...
- How to write date range query in Nest ElasticSearch client?
Looking at the source code, there are two overloads of the OnField method. When I use the the that t ...
随机推荐
- Python中的eval
Python中的eval方法接受一个字符串参数,并且把字符串里面的内容当成Python代码来执行: eval的缺点是执行速度慢,并且会有安全风险
- Rescue(BFS时间最短 另开数组或优先队列)
Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M ...
- BluetoothServerSocket详解
一. BluetoorhServerSocket简介 1. 继承关系 public final class BluetoothServerSocket extends Object implement ...
- LintCode-88.最近公共祖先
最近公共祖先 给定一棵二叉树,找到两个节点的最近公共父节点(LCA). 最近公共祖先是两个节点的公共的祖先节点且具有最大深度. 注意事项 假设给出的两个节点都在树中存在 样例 对于下面这棵二叉树 LC ...
- Qt语言家(Qt Linguist)更新翻译报错-Qt5.9-MinGW
版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:Qt语言家(Qt Linguist)更新翻译报错-Qt5.9-MinGW 本文地址:h ...
- 解决连接mysql报错1130
最近在服务器上部署好的应用突然间连接不上mysql数据库,报错“ERROR 1130: Host xxx.xxx.xxx.xxx is not allowed to connect to this M ...
- perf打印调用栈的过程
perf_prepare_sample-->perf_callchain-->get_perf_callchain 上面的调用栈会使用 perf_event_output--> 0x ...
- 【Python】Python SQLAlchemy基本操作和常用技巧
首先说下,由于最新的 0.8 版还是开发版本,因此我使用的是 0.79 版,API 也许会有些不同.因为我是搭配 MySQL InnoDB 使用,所以使用其他数据库的也不能完全照搬本文. 接着就从安装 ...
- hdu 2686 Matrix && hdu 3367 Matrix Again (最大费用最大流)
Matrix Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- java动态代理(JDK和CGLIB)笔记
动态代理:为一堆interface或类的实现提供统一的执行通道,从含义上就像局域网电脑通过代理上网一样,走统一的通道,代理控制通道,自然可以在通道里加上自定义实现,例如像AOP切面,日志等. JDK的 ...