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 ...
随机推荐
- Alpha冲刺——第二天
Alpha第二天 听说 031502543 周龙荣(队长) 031502615 李家鹏 031502632 伍晨薇 031502637 张柽 031502639 郑秦 1.前言 任务分配是VV.ZQ. ...
- UBUNTU如何安装tar.gz版的flash
adobe flash player的官方下载页面为:https://get.adobe.com/cn/flashplayer/ 不过近期通过APT方式以及ubuntu的软件中心都安装不了flashp ...
- LintCode-54.转换字符串到整数
转换字符串到整数 实现atoi这个函数,将一个字符串转换为整数.如果没有合法的整数,返回0.如果整数超出了32位整数的范围,返回INT_MAX(2147483647)如果是正整数,或者INT_MIN( ...
- JTS空间分析工具包(GIS开源)学习 JAVA
JST空间分析工具包是一套JAVA API,提供一系列的空间数据分析操作.最近开发项目刚好需要用到,上网搜资料也少,就自己写下来记录一下.C++版本的拓扑分析开源工具叫:geos:.NET版本的拓扑分 ...
- YaoLingJump开发者日志(六)
作为一只天才魔法少女狐,不会魔法怎么行?于是我给瑶玲增加了一个技能:魔法弹. 当然,能使用魔法的前提是得有个魔杖,像这样: 魔杖不仅能让瑶玲使用魔法,当瑶玲被攻击时还能提供2s的无敌状态: ...
- C# 饼形图
原文链接:https://www.cnblogs.com/icyJ/archive/2012/10/08/Chart_Pie.html 需要实现的目标是: 1.将数据绑定到pie的后台数据中,自动生成 ...
- 几个关于控件的优先级: UseSystemPasswordChar > PasswordChar > 控件属性设置
using System; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms ...
- SpringBoot事件监听
代码演示: package com.boot.event.eventdemo; import org.springframework.boot.SpringApplication; import or ...
- Codeforces Round #521 Div. 3 玩耍记
A:签到. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> ...
- Andorid API Package ---> android.accessibilityservice
包名: android.accessibilityservice Added in API level 4 URL:http://developer.andro ...