首页 文章 精选 留言 我的

精选列表

搜索[webpack入门],共10000篇文章
优秀的个人博客,低调大师

openstack M 版 neutron网络组件基础入门

在我们openstack学习当中,网络组件neutron无疑是令很多人很难理解的,可以说要深入理解 了neutron组件,你基本完成了openstack 60%的学习,存储方面只要不涉及到分布式,剩下的基本都比较简单了 相信很多人第一次看到这种图的时候都会被吓一跳,没错,这就是openstack neutron组件里面涉及到的数据流程,里面涉及到的知识点很多很多 Openstack网络模型中的几个概念网络: ManagementNetwork:管理网络,连接所有节点。 ExternalNetwork:外部网络,虚拟机通过此接口连通外部网络 DataNetwork:虚拟机网络,提供之间内部数据线通讯的网络. 这里搭建过程略过,可以参考之前写的L版本的搭建过程,这次环境如下,一个控制节点和一个计算节点 上面是控制节点,三张网卡 eno1777736 10.10.80.133 作为外部网络 eno33554960 10.10.10.130 作为管理网络 eno50332184 作为虚拟机网络 支持的网络类型有 flat vlan vxlan gre 而具体实现的这些的则有Linux bridge 和openvswitch 这里我们以Linux bridge 来实现flat网络为例来说明,其他基本差不多,更多内容可以参阅官方文档,这里有趣的是在最新版本 N ,官方已经提供了 ansible来部署openstack的指导说明 Flat网络,顾名思义是平面网络,要求宿主机的物理网卡直接与Linux bridge连接,,每个flat network都会独占一个物理网卡,关键参数配置如下 [root@controller~]#vim/etc/neutron/plugins/ml2/ml2_conf.ini tenant_network_types=flat [ml2_type_flat] flat_networks=martin_flat#这个名字自定义 [root@controller~]#vim/etc/neutron/plugins/ml2/linuxbridge_agent.ini physical_interface_mappings=martin_flat:eno50332184#名字保持与上面一样,后面是flat网卡名称 我们打开dashboard来创建第一个flat网络 接下来观察这些操作在服务器上面引起了什么变化 接下来我们来创建第一台虚拟机并关联到flat网络 观察计算节点的变化情况 dhcp功能 Neutron提供dhcp服务的组件是dhcp agent ,在网络节点上面运行,默认是通过dnsmasq是实现dhcp功能 配置文件在 [root@controller ~]# vim /etc/neutron/dhcp_agent.ini interface_driver = neutron.agent.linux.interface.BridgeInterfaceDriver ns-26782a82-61和tap26782a82-61是一对veth pair,他们将9225f654-522b-478e-80bc-06bf2948cd08 连接到网桥 brq9225f654-52上面 以上这些都只是openstack neutron网络组件最基本的东西,里面的内容实在是太多了,涉及到的知识点也很多 如 liunx bridge tap设备 虚拟对 namespace openvswitch 虚拟交换机 虚拟路由器 iptables 高可用 dvr 等等,包括我自己还有很多的东西需要学习,但是大家无需害怕,就从最简单的学习起,学会了一个,其他你照着模仿应该也能学会 最后给大家看一下 通过openvswith构建的vxlan网络时,两个虚拟机通讯的流程,有兴趣的可以自己深入一下 openvswitch流表结构

优秀的个人博客,低调大师

LOGSTASH再入门第一发

慢慢弄起来。。。 前年搞过,现在生疏了,再慢慢拾起来吧。 一些URL: https://www.elastic.co/downloads/logstash https://www.elastic.co/products/kibana http://kafka.apache.org/ https://www.elastic.co/products/elasticsearch https://www.gitbook.com/book/chenryn/kibana-guide-cn/details http://pan.baidu.com/share/link?uk=2267823173&shareid=2710953066&third=0 http://www.logstashbook.com/ 参考PDF: The LogStash Book - Turnbull, James.pdf

优秀的个人博客,低调大师

阿里云ECS Docker Machine Driver入门指南

+++title = "Aliyun Elastic Compute Service"description = "Aliyun driver for machine"keywords = ["machine, aliyun, driver, ecs"][menu.main]parent="smn_machine_drivers"+++ Docker Machine Driver of Aliyun ECS Docker技术目前在无论在社区还是在企业应用中,都有很强烈的反响。为了使得用户可以在阿里云ECS上体验和使用Docker技术,我们开发了相应的Docker Machine驱动。在创建machine之前,首先需要有一对阿里云的安全秘钥以及对应的Region信息。如果想创建VPC网络,还需要有VPC ID和VSwitch ID。相关

优秀的个人博客,低调大师

web开发快餐式入门指南 1.1 http

由于web应用大多数都在浏览器中进行操作,所以我们有必要先了解一下浏览器里面到底发生了什么。简而言之,当你在浏览器的地址栏中输入网址并按下回车,或者点击了网页上的某个链接时,浏览器就会按照网址给目标服务器发送请求。浏览器和服务器之间的请求遵循http协议,协议规定了所使用的格式,只有按照这种格式组织的数据才能相互识别。 具体来讲,浏览器先根据url中的主机部分通过dns查询出ip地址,然后组装http报文,发送给这个ip。服务器接到请求后便会处理,然后也会组装一个报文,把结果放入其中发给浏览器。浏览器根据html或js中写好的逻辑,以刷新页面或者弹出消息框等方式和用户交互。这一来一回才算结束。 格式 我们可以用浏览器的开发者工具,或者其它抓包软件进行抓包,从而看到背后的http请求是什么。比如访问www.baidu.com,则有可能会看到这样的报文: GET / HTTP/1.1 Host: www.baidu.com Connection: keep-alive Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36 Accept-Encoding: gzip, deflate, sdch Accept-Language: zh-CN,zh;q=0.8 Cookie: ... HTTP/1.1 200 OK Server: bfe/1.0.8.14 Date: Tue, 23 Feb 2016 12:43:08 GMT Content-Type: text/html;charset=utf-8 Transfer-Encoding: chunked Connection: keep-alive Cache-Control: private Expires: Tue, 23 Feb 2016 12:43:08 GMT Content-Encoding: gzip X-UA-Compatible: IE=Edge,chrome=1 BDPAGETYPE: 2 BDQID: 0xd678b56500003dd4 BDUSERID: 18401 Set-Cookie: ... ... 可以看到,上半部分是请求(request)的报文,下半部分是响应(response)的报文。请求和响应的报文都由三部分组成:状态行、消息头(header)和消息体(body)。其中状态行在请求或者响应中,分别叫做请求行和响应行,消息头在请求或者响应中也分别叫做请求头和响应头,消息体也叫正文,是可选的。 请求报文 我们先看请求报文,第一行GET / HTTP/1.1就是请求行,其中GET是方法,/是路径,HTTP/1.1是所使用的HTTP版本。 之后几行以:分割的文本是请求头,请求头的种类很多,其中还有不少自定义的请求头,但是上面列出的都是常见的请求头,我们只需要了解它们。 Host是所请求的域名,由于现在大量虚拟主机和云主机的存在,一个ip的主机上往往有很多个网站,所以要加上Host请求头,不能简单以ip来定位。 Accept是浏览器接受的格式,我们可以看到有html、xhtml和xml。 User-Agent是浏览器的类型,也包含浏览器内核类型和操作系统类型。由于用户可以任意改动请求头,它不一定是可信的。 Accept-Encoding是浏览器接受的编码,我们可以看出浏览器启用了gzip压缩。 Accept-Language是浏览器接受的语言。 Cookie是用户标识用户的数据,由服务端程序生成,储存在浏览器中,每次浏览器访问一个网站,都要带上已有的Cookie。 上面的报文中,由于是GET方法,没有消息体。 消息体如果存在,和消息头以两个换行\n\n分隔。如果有消息体的话,请求头中还要加上两种。一个是Content-Length,是消息体的长度。还有一个是Content-Type,是消息体的格式,这个参数很重要,因为大多数框架都自动解析消息体,如果错了的话就会解析失败。 响应报文 同理,响应报文的第一行HTTP/1.1 200 OK是响应行,HTTP/1.1是版本,200是状态码,OK是状态文本。响应头和请求头的格式一样,但内容有所不同。而且,服务器设置Cookie所用的响应头是Set-Cookie而不是Cookie。 上面的响应报文是带消息体的,同样和响应头以\n\n分隔。 方法 接下来要讲一下HTTP的方法。 HTTP的方法有很多种,但是如果你打开抓包软件记录你一天的浏览历史的话,你会发现99%都是GET或者POST,其中GET占大多数。一般情况下,我们只会用到这两种方法,GET常用于资源的获取,比如页面显示,POST则用于动作的提交,比如删除、新增、修改某种资源。 GET和POST都可以带参数,只不过参数在报文中的位置不同。GET方法中的参数出现在路径中,以?分割,比如/path/to/page?k1=v1&k2=v2。而且你也发现了,这种位置上只能写成键值对形式。HTTP协议中并没有对参数的长度进行限制,但由于GET参数是写在url中的,在浏览器以及服务器中会有限制,比如Chome中限制url长度不超过8182字节。 POST的参数在消息体中,长度任意。而且格式任意,比如可以使用json和xml,只要把Content-Type配置对了,让服务器能够正常识别就好。服务器可以限制消息体的长度,但也可以通过配置来取消。

优秀的个人博客,低调大师

Hexo 入门指南(一) - 简介 & 准备

Hexo是一个开源的静态博客生成器,用node.js开发,作者是台湾大学生tommy351。 为什么是博客 对于个人网站来说,没有比博客更合适的形式了。在博客中,文章才是最主要的,一切都显得主次分明,干净利落。相比之下,论坛中主题和回复鱼龙混杂,阅读体验非常差。同时,博客比论坛的数据库小很多,便于维护。 为什么是静态博客 很多人选择在虚拟主机或vps上面搭建动态博客。但是这些主机商通常“免费的不稳定,稳定的不免费”。前一段时间,我观察了我的个人博客友链上面的几个站点,一部分在十几天之后就销声匿迹了。独立博客如此麻烦的维护工作,能不能减轻一些呢?正如阮一峰前辈所说,blogger分为三个阶段。最开始,是门户博客。之后,是独立博客。最后,觉得独立博客自己管理起来费劲,便找个别人来管的空间,自己负责写就好。如果我们能够找到这样的空间,在自己保留最大控制权前提下,由别人托管,会省去不少事情。 静态博客编译之后是纯html页面,优点就是支持它的环境十分好找,例如github、gitcafe、七牛云存储等站点都支持静态页面托管,自然是我们的首选了。由于github page在国内访问较慢,这篇文章用gitcafe做示范。gitcafe是天朝本地化的github,同样提供展示页和域名绑定功能,不需要备案,就是爽。 但是静态博客并非没有缺点。动态博客更新文章时,脚本是不变的,只需要更新数据库。静态博客要频繁改动文件,不支持增量式上传的东西,比如ftp,就难于管理。此外,还要十分熟悉git各种命令,才能部署页面。 准备工作 git node.js markdown编辑器 gitcafe 域名 markdown编辑器是非必须的,只要你熟悉语法,随便一个编辑器来写都不是问题。 域名也是非必须的,gitcafe pages服务提供免费的二级域名。注册域名的教程这里就不写了。 安装 git git的客户端,本人推荐git-scm。 linux下面,在bash中键入: (Ubuntu, Debian) $ sudo apt-get install git (Fedora, Red Hat, CentOS) $ sudo yum install git windows或mac下,直接到git-scm官网下载安装。 安装 node.js linux下: $ sudo apt-get install nodejs $ sudo apt-get install npm yum同理。 windows或者mac下,直接到node.js官网下载安装。 windows还要设置环境变量,把node.js安装路径写进path里面,用半角分号分隔。 markdown 编辑器 windows下推荐markdown pad。 mac下推荐mou。 gitcafe 首先注册一个账号,之后点击查看如何使用pages服务。 相关网页 Hexo主页 Hexo github 地址 git book

优秀的个人博客,低调大师

用Python爬取了拉勾网的招聘信息+详细教程+趣味学习+快速爬虫入门+学习交流+大神+爬虫入门

关于 一直埋头学习,不知当前趋势,这是学习一门技术过程中最大的忌讳。刚好利用python爬虫,抓取一下拉勾网关于python职位的一些基本要求,不仅能知道岗位的基本技能要求,还能锻炼一下代码能力,学以致用,一举两得。 准备 工具 :python 2.7,PyCharm 类库:urllib2、BeautifulSoup、time、re、sys、json、collections、xlsxwriter 分析及代码实现 进入拉勾网进行分析,要想获取每个岗位的关键词,首先要知道每个岗位详情页面的url,通过对比我们发现,https://www.lagou.com/jobs/4289433.html中,只有4289433这一串数字是不同的,那么就可以知道我们只要获取到每个岗位的这一串数字,我们就可以爬取每个岗位详情页面。 通过F12查看,我们可以看到xhr请求中https://www.lagou.com/jobs/positionAjax.json?px=default&city=%E5%8C%97%E4%BA%AC&needAddtionalResult=false的响应消息里的参数positionId的值为详情页面url的那串数字,如下图 ,那么接下来我们就爬取这个请求来获取所有的positionId。 首先我们通过分析可以看到这是个post请求且form的参数为first、pn、kd,通过不同岗位列表页面的请求,我们可以看到first的取值逻辑是pn为1的时候,first为true,当pn不为1的时候,first的取值为false(其中pn为岗位列表的页数),还有kd为一个固定值(这里是python) def get_positionId(pn): positionId_list = [] url = 'https://www.lagou.com/jobs/positionAjax.json?px=default&city=%E5%8C%97%E4%BA%AC&needAddtionalResult=false' headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36', 'Referer': 'https://www.lagou.com/jobs/list_Python?px=default&city=%E5%8C%97%E4%BA%AC' } if pn == 1: first = 'true' else: first = 'false' data = {'first': first, 'pn': pn, 'kd':kd #这里取变量值,可以获取不同岗位的关键词 } page = get_page(url, headers, data) if page == None: return None max_pageNum = get_pageSize(page) result = page['content']['positionResult']['result'] for num in range(0, max_pageNum): positionId = result[num]['positionId'] positionId_list.append(positionId) return positionId_list #该函数返回一个列表页的所有岗位的positionId 在获取到每个岗位的positionId后,我们就可以根据获取到的positionId进行拼接得到每个岗位详情页面的url,然后爬取这些url,来获取每个岗位的关键词(这里还有一个比较坑人的地方就是通过爬取来的网页内容和通过定位得到的内容竟然是不一样的,害的我纠结了好久),分析该网页如下图: 具体的实现如下: #获取每个岗位的职位要求def get_content(positionId): url = 'https://www.lagou.com/jobs/%s.html' %(positionId) headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36', 'Referer': 'https://www.lagou.com/jobs/list_Python?px=default&city=%E5%8C%97%E4%BA%AC' } page = get_page(url,headers,data=0) soup = Bs(page, 'html.parser') content = soup.find('dd', class_='job_bt').get_text() return content 接下来就是对获取到的岗位描述进行过滤处理,来获取英文关键词,实现如下: #对获取的关键词列表进行过滤去重,获取top50的关键词#处理岗位描述,获取英文关键词def get_keyword(content): pattern = re.compile('[a-zA-Z]+') keyword = pattern.findall(content) return keyword 然后,在通过collections中的Counter模块获取到这些英文关键词中的top50,实现如下: #对获取的关键词列表进行过滤去重,获取top50的关键词def parser_keyword(keyword_list): for i in range(len(keyword_list)): keyword_list[i] = keyword_list[i].lower() keyword_top = Counter(keyword_list).most_common(50) return keyword_top 最后把top50的关键词保存到Excel中,并且生成分析图,实现如下: #数据保存到Excel中,并且生成报表。def save_excel(keyword_top): row = 1 col = 0 workbook = xlsxwriter.Workbook('lagou.xlsx') worksheet = workbook.add_worksheet('lagou') worksheet.write(0, col, u'关键词') worksheet.write(0, col+1, u'频次') for name, num in keyword_top: worksheet.write(row, col, name) worksheet.write(row, col+1, num) row += 1 chart = workbook.add_chart({'type': 'area'}) chart.add_series({ 'categories': 'lagou!$A$2:$A$51', 'values': 'lagou!$B$2:$B$51' }) chart.set_title({'name': u'关键词排名'}) chart.set_x_axis({'name': u'关键词'}) chart.set_y_axis({'name': u'频次(/次)'}) worksheet.insert_chart('C2', chart, {'x_offset':15, 'y_offset':10}) workbook.close() 结果 具体生成的分析图如下: image.png 如果对您有点帮助的话,麻烦您给点个赞,谢谢。 最后附上全部的代码: 大家如果有问题都可以评论区留言,另外如果需要一个学习交流的平台可以加小编的群:719+139+688,群里面有很多学习资料还有大神的直播分享,希望对大家有所帮助,另外在这个上面我回复可能稍微慢一点,但是你们有问题在评论区留言我都会帮你们解决的。

优秀的个人博客,低调大师

Elasticsearch 编程API入门系列---入门1(Java Client类型、连接es集群、添加json格式的数据、添加map格式...

https://www.elastic.co/guide/en/elasticsearch/client/java-api/index.html 我这里,elasticsearch用的是2.4.X版本。 https://www.elastic.co/guide/en/elasticsearch/client/java-api/2.4/java-docs.html 第一步:删除默认的App.java 第二步:选中后,再ctrl + n ,记住,要求自己能用快捷键,就用快捷键! 第三步:输入class,直接回车,再回车 第四步:我这里,以TestEs.java为例 第五步: 第六步:输入代码,这里不多说 通过TransportClient这个类,指定es集群中其中一台或多台机的ip地址和端口 TransportClient client = TransportClient.builder().build().addTransportAddress(new InetSocketTransportAddress (InetAddress.getByName("host1"), 9300)).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host2"), 9300)); 如果需要使用其他名称的集群(默认是elasticsearch),需要如下设置 Settings settings = Settings.settingsBuilder().put("cluster.name", "myClusterName").build(); TransportClientclient = TransportClient.builder().settings(settings).build().addTransportAddress(new InetSocketTransportAddress (InetAddress.getByName("host1"), 9300)); 通过TransportClient这个接口,自动嗅探整个集群的状态,es会自动把集群中其它机器的ip地址加到客户端中 Settings settings = Settings.settingsBuilder().put("client.transport.sniff", true).build(); TransportClient.builder().settings(settings).build().addTransportAddress(new InetSocketTransportAddress (InetAddress.getByName("host1"), 9300)); 更详细,请见 Elasticsearch的Java Client类型 索引index(四种json,map,bean,es helper) IndexResponse response = client.prepareIndex("zhouls", "emp", "1").setSource().get() 查询get GetResponse response = client.prepareGet("zhouls", "emp", "1").get(); 更新update 删除delete DeleteResponse response = client.prepareDelete("zhouls", "emp", "1").execute().actionGet(); 总数count long count = client.prepareCount("zhouls").get().getCount(); 附上代码 前提 准备,开启3台机器组建的es集群进程 test1测试(连接192.168.80.10和192.168.80.11) 1 package zhouls.bigdata.myElasticsearch; 2 3 import static org.junit.Assert.*; 4 5 import java.net.InetAddress; 6 import java.util.HashMap; 7 import java.util.List; 8 9 import org.elasticsearch.action.bulk.BulkItemResponse; 10 import org.elasticsearch.action.bulk.BulkRequestBuilder; 11 import org.elasticsearch.action.bulk.BulkResponse; 12 import org.elasticsearch.action.delete.DeleteRequest; 13 import org.elasticsearch.action.get.GetResponse; 14 import org.elasticsearch.action.index.IndexRequest; 15 import org.elasticsearch.action.index.IndexResponse; 16 import org.elasticsearch.action.search.SearchResponse; 17 import org.elasticsearch.action.search.SearchType; 18 import org.elasticsearch.action.update.UpdateResponse; 19 import org.elasticsearch.client.transport.TransportClient; 20 import org.elasticsearch.cluster.node.DiscoveryNode; 21 import org.elasticsearch.common.settings.Settings; 22 import org.elasticsearch.common.transport.InetSocketTransportAddress; 23 import org.elasticsearch.common.transport.TransportAddress; 24 import org.elasticsearch.common.xcontent.XContentBuilder; 25 import org.elasticsearch.common.xcontent.XContentFactory; 26 import org.elasticsearch.index.query.MatchQueryBuilder.Operator; 27 import org.elasticsearch.index.query.QueryBuilders; 28 import org.elasticsearch.search.SearchHit; 29 import org.elasticsearch.search.SearchHits; 30 import org.elasticsearch.search.aggregations.Aggregation; 31 import org.elasticsearch.search.aggregations.AggregationBuilders; 32 import org.elasticsearch.search.aggregations.bucket.terms.Terms; 33 import org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket; 34 import org.elasticsearch.search.aggregations.metrics.sum.Sum; 35 import org.elasticsearch.search.sort.SortOrder; 36 import org.junit.Before; 37 import org.junit.Test; 38 39 import com.fasterxml.jackson.databind.ObjectMapper; 40 import com.google.common.collect.ImmutableList; 41 42 public class TestEs { 43 44 //es和hadoop没关系啊,获取一个transportclient就可以操作es了 45 46 47 private TransportClient transportClient; 48 @Before//@Before和@Test的区别:每次执行都要先经过@Before,好比是,它是一个模板。 49 //before表示在执行每个test方法之前运行,常与@Test搭配使用 50 public void test0() throws Exception { 51 //获取TransportClient,来操作es 52 transportClient = TransportClient.builder().build(); 53 //需要使用9300端口 54 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.10"), 9300); 55 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 56 transportClient.addTransportAddress(transportAddress); 57 } 58 59 /** 60 * 用java代码测试的时候这样写是没有问题的,比较简单 61 * @throws Exception 62 */ 63 @Test 64 public void test1() throws Exception { 65 //获取TransportClient,来操作es 66 TransportClient transportClient = TransportClient.builder().build(); 67 //需要使用9300端口,指定es集群中的节点信息, 这个地方指定的端口是节点和节点之间的通信端口是9300,不是Http请求的端口9200. 68 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.11"), 9300); 69 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 70 transportClient.addTransportAddress(transportAddress); 71 System.out.println(transportClient.toString()); 72 } test2测试(连接192.168.80.10、192.168.80.11和192.168.80.12)(即es集群) 1 package zhouls.bigdata.myElasticsearch; 2 3 import static org.junit.Assert.*; 4 5 import java.net.InetAddress; 6 import java.util.HashMap; 7 import java.util.List; 8 9 import org.elasticsearch.action.bulk.BulkItemResponse; 10 import org.elasticsearch.action.bulk.BulkRequestBuilder; 11 import org.elasticsearch.action.bulk.BulkResponse; 12 import org.elasticsearch.action.delete.DeleteRequest; 13 import org.elasticsearch.action.get.GetResponse; 14 import org.elasticsearch.action.index.IndexRequest; 15 import org.elasticsearch.action.index.IndexResponse; 16 import org.elasticsearch.action.search.SearchResponse; 17 import org.elasticsearch.action.search.SearchType; 18 import org.elasticsearch.action.update.UpdateResponse; 19 import org.elasticsearch.client.transport.TransportClient; 20 import org.elasticsearch.cluster.node.DiscoveryNode; 21 import org.elasticsearch.common.settings.Settings; 22 import org.elasticsearch.common.transport.InetSocketTransportAddress; 23 import org.elasticsearch.common.transport.TransportAddress; 24 import org.elasticsearch.common.xcontent.XContentBuilder; 25 import org.elasticsearch.common.xcontent.XContentFactory; 26 import org.elasticsearch.index.query.MatchQueryBuilder.Operator; 27 import org.elasticsearch.index.query.QueryBuilders; 28 import org.elasticsearch.search.SearchHit; 29 import org.elasticsearch.search.SearchHits; 30 import org.elasticsearch.search.aggregations.Aggregation; 31 import org.elasticsearch.search.aggregations.AggregationBuilders; 32 import org.elasticsearch.search.aggregations.bucket.terms.Terms; 33 import org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket; 34 import org.elasticsearch.search.aggregations.metrics.sum.Sum; 35 import org.elasticsearch.search.sort.SortOrder; 36 import org.junit.Before; 37 import org.junit.Test; 38 39 import com.fasterxml.jackson.databind.ObjectMapper; 40 import com.google.common.collect.ImmutableList; 41 42 public class TestEs { 43 44 //es和hadoop没关系啊,获取一个transportclient就可以操作es了 45 46 47 private TransportClient transportClient; 48 @Before//@Before和@Test的区别:每次执行都要先经过@Before,好比是,它是一个模板。 49 //before表示在执行每个test方法之前运行,常与@Test搭配使用 50 public void test0() throws Exception { 51 //获取TransportClient,来操作es 52 transportClient = TransportClient.builder().build(); 53 //需要使用9300端口 54 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.10"), 9300); 55 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 56 transportClient.addTransportAddress(transportAddress); 57 } 58 59 /** 60 * 用java代码测试的时候这样写是没有问题的,比较简单 61 * @throws Exception 62 */ 63 @Test 64 public void test1() throws Exception { 65 //获取TransportClient,来操作es 66 TransportClient transportClient = TransportClient.builder().build(); 67 //需要使用9300端口,指定es集群中的节点信息, 这个地方指定的端口是节点和节点之间的通信端口是9300,不是Http请求的端口9200. 68 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.11"), 9300); 69 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 70 transportClient.addTransportAddress(transportAddress); 71 System.out.println(transportClient.toString()); 72 } 73 74 /** 75 * 可以这样写,防止代码中指定的链接失效 76 * 但是写起来比较麻烦 77 * 在实际工作中这样写不是很靠谱,需要完善,做测试可以 78 * @throws Exception 79 */ 80 @Test 81 public void test2() throws Exception { 82 //获取TransportClient,来操作es,通过TransportClient可以和es集群交互 83 TransportClient transportClient = TransportClient.builder().build(); 84 //需要使用9300端口,指定es集群中的节点信息, 这个地方指定的端口是节点和节点之间的通信端口是9300,不是Http请求的端口9200. 85 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.10"), 9300); 86 TransportAddress transportAddress1 = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.11"), 9300); 87 TransportAddress transportAddress2 = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.12"), 9300); 88 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 89 transportClient.addTransportAddresses(transportAddress,transportAddress1,transportAddress2);//加入多个地址 90 System.out.println(transportClient.toString()); 91 } test3测试(连接192.168.80.10、192.168.80.11和192.168.80.12)(生产环境下建议) 1 package zhouls.bigdata.myElasticsearch; 2 3 import static org.junit.Assert.*; 4 5 import java.net.InetAddress; 6 import java.util.HashMap; 7 import java.util.List; 8 9 import org.elasticsearch.action.bulk.BulkItemResponse; 10 import org.elasticsearch.action.bulk.BulkRequestBuilder; 11 import org.elasticsearch.action.bulk.BulkResponse; 12 import org.elasticsearch.action.delete.DeleteRequest; 13 import org.elasticsearch.action.get.GetResponse; 14 import org.elasticsearch.action.index.IndexRequest; 15 import org.elasticsearch.action.index.IndexResponse; 16 import org.elasticsearch.action.search.SearchResponse; 17 import org.elasticsearch.action.search.SearchType; 18 import org.elasticsearch.action.update.UpdateResponse; 19 import org.elasticsearch.client.transport.TransportClient; 20 import org.elasticsearch.cluster.node.DiscoveryNode; 21 import org.elasticsearch.common.settings.Settings; 22 import org.elasticsearch.common.transport.InetSocketTransportAddress; 23 import org.elasticsearch.common.transport.TransportAddress; 24 import org.elasticsearch.common.xcontent.XContentBuilder; 25 import org.elasticsearch.common.xcontent.XContentFactory; 26 import org.elasticsearch.index.query.MatchQueryBuilder.Operator; 27 import org.elasticsearch.index.query.QueryBuilders; 28 import org.elasticsearch.search.SearchHit; 29 import org.elasticsearch.search.SearchHits; 30 import org.elasticsearch.search.aggregations.Aggregation; 31 import org.elasticsearch.search.aggregations.AggregationBuilders; 32 import org.elasticsearch.search.aggregations.bucket.terms.Terms; 33 import org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket; 34 import org.elasticsearch.search.aggregations.metrics.sum.Sum; 35 import org.elasticsearch.search.sort.SortOrder; 36 import org.junit.Before; 37 import org.junit.Test; 38 39 import com.fasterxml.jackson.databind.ObjectMapper; 40 import com.google.common.collect.ImmutableList; 41 42 public class TestEs { 43 44 //es和hadoop没关系啊,获取一个transportclient就可以操作es了 45 46 47 private TransportClient transportClient; 48 @Before//@Before和@Test的区别:每次执行都要先经过@Before,好比是,它是一个模板。 49 //before表示在执行每个test方法之前运行,常与@Test搭配使用 50 public void test0() throws Exception { 51 //获取TransportClient,来操作es 52 transportClient = TransportClient.builder().build(); 53 //需要使用9300端口 54 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.10"), 9300); 55 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 56 transportClient.addTransportAddress(transportAddress); 57 } 58 59 /** 60 * 用java代码测试的时候这样写是没有问题的,比较简单 61 * @throws Exception 62 */ 63 @Test 64 public void test1() throws Exception { 65 //获取TransportClient,来操作es 66 TransportClient transportClient = TransportClient.builder().build(); 67 //需要使用9300端口,指定es集群中的节点信息, 这个地方指定的端口是节点和节点之间的通信端口是9300,不是Http请求的端口9200. 68 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.11"), 9300); 69 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 70 transportClient.addTransportAddress(transportAddress); 71 System.out.println(transportClient.toString()); 72 } 73 74 /** 75 * 可以这样写,防止代码中指定的链接失效 76 * 但是写起来比较麻烦 77 * 在实际工作中这样写不是很靠谱,需要完善,做测试可以 78 * @throws Exception 79 */ 80 @Test 81 public void test2() throws Exception { 82 //获取TransportClient,来操作es,通过TransportClient可以和es集群交互 83 TransportClient transportClient = TransportClient.builder().build(); 84 //需要使用9300端口,指定es集群中的节点信息, 这个地方指定的端口是节点和节点之间的通信端口是9300,不是Http请求的端口9200. 85 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.10"), 9300); 86 TransportAddress transportAddress1 = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.11"), 9300); 87 TransportAddress transportAddress2 = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.12"), 9300); 88 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 89 transportClient.addTransportAddresses(transportAddress,transportAddress1,transportAddress2);//加入多个地址 90 System.out.println(transportClient.toString()); 91 } 92 93 /** 94 * 实际生产环境下面,建议这样用,加上下面这些配置信息 95 * @throws Exception 96 */ 97 @Test 98 public void test3() throws Exception { 99 //指定es的配置信息 100 Settings settings = Settings.settingsBuilder() 101 .put("cluster.name", "elasticsearch")//集群名称 102 //如果集群名称在配置文件中被修改了,那么在这需要显式定义一下 103 //es集群名称默认是 elasticsearch sniff嗅; 发现; 104 .put("client.transport.sniff", true)//开启集群的嗅探功能,只需要指定集群中一个节点信息即可获取到集群中的所有节点信息 105 //开启集群的嗅探功能,这样可以保证es会自动把集群中的其他节点信息添加到transportClient里面 106 //开启嗅探功能后 只要指定集群中的任意一个可用节点就可以了.当把代码运行之后TransportClient里面会把集群中所有节点的信息都拿到,能识别集群中的所有节点. 107 .build(); 108 109 //获取TransportClient,来操作es,//通过TransportClient可以和es集群交互 110 TransportClient transportClient = TransportClient.builder().settings(settings).build(); 111 //需要使用9300端口,指定es集群中的节点信息, 这个地方指定的端口是节点和节点之间的通信端口是9300,不是Http请求的端口9200. 112 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.10"), 9300); 113 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 114 transportClient.addTransportAddress(transportAddress); 115 116 //获取client链接到的节点信息, //获取当前transportClient连接到了集群多少个节点 117 List<DiscoveryNode> connectedNodes = transportClient.connectedNodes(); 118 for (DiscoveryNode discoveryNode : connectedNodes) {//for星型循环,将connectedNodes的值,一一传给DiscoveryNode discoveryNode 119 System.out.println(discoveryNode.getHostName());//打印192.168.80.10;192.168.80.11;192.168.80.12 120 //如果加入transportClient.addTransportAddresses(transportAddress) 只有一个ip,打印的就只有一个. 121 } 122 } test4测试(添加json格式的数据) 1 package zhouls.bigdata.myElasticsearch; 2 3 import static org.junit.Assert.*; 4 5 import java.net.InetAddress; 6 import java.util.HashMap; 7 import java.util.List; 8 9 import org.elasticsearch.action.bulk.BulkItemResponse; 10 import org.elasticsearch.action.bulk.BulkRequestBuilder; 11 import org.elasticsearch.action.bulk.BulkResponse; 12 import org.elasticsearch.action.delete.DeleteRequest; 13 import org.elasticsearch.action.get.GetResponse; 14 import org.elasticsearch.action.index.IndexRequest; 15 import org.elasticsearch.action.index.IndexResponse; 16 import org.elasticsearch.action.search.SearchResponse; 17 import org.elasticsearch.action.search.SearchType; 18 import org.elasticsearch.action.update.UpdateResponse; 19 import org.elasticsearch.client.transport.TransportClient; 20 import org.elasticsearch.cluster.node.DiscoveryNode; 21 import org.elasticsearch.common.settings.Settings; 22 import org.elasticsearch.common.transport.InetSocketTransportAddress; 23 import org.elasticsearch.common.transport.TransportAddress; 24 import org.elasticsearch.common.xcontent.XContentBuilder; 25 import org.elasticsearch.common.xcontent.XContentFactory; 26 import org.elasticsearch.index.query.MatchQueryBuilder.Operator; 27 import org.elasticsearch.index.query.QueryBuilders; 28 import org.elasticsearch.search.SearchHit; 29 import org.elasticsearch.search.SearchHits; 30 import org.elasticsearch.search.aggregations.Aggregation; 31 import org.elasticsearch.search.aggregations.AggregationBuilders; 32 import org.elasticsearch.search.aggregations.bucket.terms.Terms; 33 import org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket; 34 import org.elasticsearch.search.aggregations.metrics.sum.Sum; 35 import org.elasticsearch.search.sort.SortOrder; 36 import org.junit.Before; 37 import org.junit.Test; 38 39 import com.fasterxml.jackson.databind.ObjectMapper; 40 import com.google.common.collect.ImmutableList; 41 42 public class TestEs { 43 44 //es和hadoop没关系啊,获取一个transportclient就可以操作es了 45 46 47 private TransportClient transportClient; 48 @Before//@Before和@Test的区别:每次执行都要先经过@Before,好比是,它是一个模板。 49 //before表示在执行每个test方法之前运行,常与@Test搭配使用 50 public void test0() throws Exception { 51 //获取TransportClient,来操作es 52 transportClient = TransportClient.builder().build(); 53 //需要使用9300端口 54 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.10"), 9300); 55 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 56 transportClient.addTransportAddress(transportAddress); 57 } 58 59 /** 60 * 用java代码测试的时候这样写是没有问题的,比较简单 61 * @throws Exception 62 */ 63 @Test 64 public void test1() throws Exception { 65 //获取TransportClient,来操作es 66 TransportClient transportClient = TransportClient.builder().build(); 67 //需要使用9300端口,指定es集群中的节点信息, 这个地方指定的端口是节点和节点之间的通信端口是9300,不是Http请求的端口9200. 68 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.11"), 9300); 69 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 70 transportClient.addTransportAddress(transportAddress); 71 System.out.println(transportClient.toString()); 72 } 73 74 /** 75 * 可以这样写,防止代码中指定的链接失效 76 * 但是写起来比较麻烦 77 * 在实际工作中这样写不是很靠谱,需要完善,做测试可以 78 * @throws Exception 79 */ 80 @Test 81 public void test2() throws Exception { 82 //获取TransportClient,来操作es,通过TransportClient可以和es集群交互 83 TransportClient transportClient = TransportClient.builder().build(); 84 //需要使用9300端口,指定es集群中的节点信息, 这个地方指定的端口是节点和节点之间的通信端口是9300,不是Http请求的端口9200. 85 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.10"), 9300); 86 TransportAddress transportAddress1 = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.11"), 9300); 87 TransportAddress transportAddress2 = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.12"), 9300); 88 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 89 transportClient.addTransportAddresses(transportAddress,transportAddress1,transportAddress2);//加入多个地址 90 System.out.println(transportClient.toString()); 91 } 92 93 /** 94 * 实际生产环境下面,建议这样用,加上下面这些配置信息 95 * @throws Exception 96 */ 97 @Test 98 public void test3() throws Exception { 99 //指定es的配置信息 100 Settings settings = Settings.settingsBuilder() 101 .put("cluster.name", "elasticsearch")//集群名称 102 //如果集群名称在配置文件中被修改了,那么在这需要显式定义一下 103 //es集群名称默认是 elasticsearch sniff嗅; 发现; 104 .put("client.transport.sniff", true)//开启集群的嗅探功能,只需要指定集群中一个节点信息即可获取到集群中的所有节点信息 105 //开启集群的嗅探功能,这样可以保证es会自动把集群中的其他节点信息添加到transportClient里面 106 //开启嗅探功能后 只要指定集群中的任意一个可用节点就可以了.当把代码运行之后TransportClient里面会把集群中所有节点的信息都拿到,能识别集群中的所有节点. 107 .build(); 108 109 //获取TransportClient,来操作es,//通过TransportClient可以和es集群交互 110 TransportClient transportClient = TransportClient.builder().settings(settings).build(); 111 //需要使用9300端口,指定es集群中的节点信息, 这个地方指定的端口是节点和节点之间的通信端口是9300,不是Http请求的端口9200. 112 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.10"), 9300); 113 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 114 transportClient.addTransportAddress(transportAddress); 115 116 //获取client链接到的节点信息, //获取当前transportClient连接到了集群多少个节点 117 List<DiscoveryNode> connectedNodes = transportClient.connectedNodes(); 118 for (DiscoveryNode discoveryNode : connectedNodes) {//for星型循环,将connectedNodes的值,一一传给DiscoveryNode discoveryNode 119 System.out.println(discoveryNode.getHostName());//打印192.168.80.10;192.168.80.11;192.168.80.12 120 //如果加入transportClient.addTransportAddresses(transportAddress) 只有一个ip,打印的就只有一个. 121 } 122 } 123 124 125 String index = "zhouls";//设置索引库 126 String type = "emp";//设置类型 127 128 //索引index(四种格式:json,map,bean,es helper) 129 130 /** 131 * index-1 json 132 * 实际工作中使用 133 * @throws Exception 134 */ 135 @Test 136 public void test4() throws Exception { 137 String jsonStr = "{\"name\":\"tom zhang\",\"age\":19}";//需要转义下 //向索引库中传入一个String字符串,还可以接受其他类型 138 IndexResponse indexResponse = transportClient.prepareIndex(index, type, "1")//添加一个id=1的数据 139 .setSource(jsonStr)//设值,这是json格式的 140 .get(); 141 //.execute().actionGet(); 这个和上面的get()方法是一样的,get()就是对.execute().actionGet() 进行了封装 142 System.out.println(indexResponse.getVersion()); 143 //得到这个数据的version,如果version=1代表是新添加的数据 144 } 然后,在浏览器里,输入http://192.168.80.10:9200/_plugin/head/ test5测试(添加map格式的数据) 1 package zhouls.bigdata.myElasticsearch; 2 3 import static org.junit.Assert.*; 4 5 import java.net.InetAddress; 6 import java.util.HashMap; 7 import java.util.List; 8 9 import org.elasticsearch.action.bulk.BulkItemResponse; 10 import org.elasticsearch.action.bulk.BulkRequestBuilder; 11 import org.elasticsearch.action.bulk.BulkResponse; 12 import org.elasticsearch.action.delete.DeleteRequest; 13 import org.elasticsearch.action.get.GetResponse; 14 import org.elasticsearch.action.index.IndexRequest; 15 import org.elasticsearch.action.index.IndexResponse; 16 import org.elasticsearch.action.search.SearchResponse; 17 import org.elasticsearch.action.search.SearchType; 18 import org.elasticsearch.action.update.UpdateResponse; 19 import org.elasticsearch.client.transport.TransportClient; 20 import org.elasticsearch.cluster.node.DiscoveryNode; 21 import org.elasticsearch.common.settings.Settings; 22 import org.elasticsearch.common.transport.InetSocketTransportAddress; 23 import org.elasticsearch.common.transport.TransportAddress; 24 import org.elasticsearch.common.xcontent.XContentBuilder; 25 import org.elasticsearch.common.xcontent.XContentFactory; 26 import org.elasticsearch.index.query.MatchQueryBuilder.Operator; 27 import org.elasticsearch.index.query.QueryBuilders; 28 import org.elasticsearch.search.SearchHit; 29 import org.elasticsearch.search.SearchHits; 30 import org.elasticsearch.search.aggregations.Aggregation; 31 import org.elasticsearch.search.aggregations.AggregationBuilders; 32 import org.elasticsearch.search.aggregations.bucket.terms.Terms; 33 import org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket; 34 import org.elasticsearch.search.aggregations.metrics.sum.Sum; 35 import org.elasticsearch.search.sort.SortOrder; 36 import org.junit.Before; 37 import org.junit.Test; 38 39 import com.fasterxml.jackson.databind.ObjectMapper; 40 import com.google.common.collect.ImmutableList; 41 42 public class TestEs { 43 44 //es和hadoop没关系啊,获取一个transportclient就可以操作es了 45 46 47 private TransportClient transportClient; 48 @Before//@Before和@Test的区别:每次执行都要先经过@Before,好比是,它是一个模板。 49 //before表示在执行每个test方法之前运行,常与@Test搭配使用 50 public void test0() throws Exception { 51 //获取TransportClient,来操作es 52 transportClient = TransportClient.builder().build(); 53 //需要使用9300端口 54 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.10"), 9300); 55 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 56 transportClient.addTransportAddress(transportAddress); 57 } 58 59 /** 60 * 用java代码测试的时候这样写是没有问题的,比较简单 61 * @throws Exception 62 */ 63 @Test 64 public void test1() throws Exception { 65 //获取TransportClient,来操作es 66 TransportClient transportClient = TransportClient.builder().build(); 67 //需要使用9300端口,指定es集群中的节点信息, 这个地方指定的端口是节点和节点之间的通信端口是9300,不是Http请求的端口9200. 68 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.11"), 9300); 69 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 70 transportClient.addTransportAddress(transportAddress); 71 System.out.println(transportClient.toString()); 72 } 73 74 /** 75 * 可以这样写,防止代码中指定的链接失效 76 * 但是写起来比较麻烦 77 * 在实际工作中这样写不是很靠谱,需要完善,做测试可以 78 * @throws Exception 79 */ 80 @Test 81 public void test2() throws Exception { 82 //获取TransportClient,来操作es,通过TransportClient可以和es集群交互 83 TransportClient transportClient = TransportClient.builder().build(); 84 //需要使用9300端口,指定es集群中的节点信息, 这个地方指定的端口是节点和节点之间的通信端口是9300,不是Http请求的端口9200. 85 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.10"), 9300); 86 TransportAddress transportAddress1 = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.11"), 9300); 87 TransportAddress transportAddress2 = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.12"), 9300); 88 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 89 transportClient.addTransportAddresses(transportAddress,transportAddress1,transportAddress2);//加入多个地址 90 System.out.println(transportClient.toString()); 91 } 92 93 /** 94 * 实际生产环境下面,建议这样用,加上下面这些配置信息 95 * @throws Exception 96 */ 97 @Test 98 public void test3() throws Exception { 99 //指定es的配置信息 100 Settings settings = Settings.settingsBuilder() 101 .put("cluster.name", "elasticsearch")//集群名称 102 //如果集群名称在配置文件中被修改了,那么在这需要显式定义一下 103 //es集群名称默认是 elasticsearch sniff嗅; 发现; 104 .put("client.transport.sniff", true)//开启集群的嗅探功能,只需要指定集群中一个节点信息即可获取到集群中的所有节点信息 105 //开启集群的嗅探功能,这样可以保证es会自动把集群中的其他节点信息添加到transportClient里面 106 //开启嗅探功能后 只要指定集群中的任意一个可用节点就可以了.当把代码运行之后TransportClient里面会把集群中所有节点的信息都拿到,能识别集群中的所有节点. 107 .build(); 108 109 //获取TransportClient,来操作es,//通过TransportClient可以和es集群交互 110 TransportClient transportClient = TransportClient.builder().settings(settings).build(); 111 //需要使用9300端口,指定es集群中的节点信息, 这个地方指定的端口是节点和节点之间的通信端口是9300,不是Http请求的端口9200. 112 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.10"), 9300); 113 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 114 transportClient.addTransportAddress(transportAddress); 115 116 //获取client链接到的节点信息, //获取当前transportClient连接到了集群多少个节点 117 List<DiscoveryNode> connectedNodes = transportClient.connectedNodes(); 118 for (DiscoveryNode discoveryNode : connectedNodes) {//for星型循环,将connectedNodes的值,一一传给DiscoveryNode discoveryNode 119 System.out.println(discoveryNode.getHostName());//打印192.168.80.10;192.168.80.11;192.168.80.12 120 //如果加入transportClient.addTransportAddresses(transportAddress) 只有一个ip,打印的就只有一个. 121 } 122 } 123 124 125 String index = "zhouls";//设置索引库 126 String type = "emp";//设置类型 127 128 //索引index(四种格式:json,map,bean,es helper) 129 130 /** 131 * index-1 json 132 * 实际工作中使用 133 * @throws Exception 134 */ 135 @Test 136 public void test4() throws Exception { 137 String jsonStr = "{\"name\":\"tom zhang\",\"age\":19}";//需要转义下 //向索引库中传入一个String字符串,还可以接受其他类型 138 IndexResponse indexResponse = transportClient.prepareIndex(index, type, "1")//添加一个id=1的数据 139 .setSource(jsonStr)//设值,这是json格式的 140 .get(); 141 //.execute().actionGet(); 这个和上面的get()方法是一样的,get()就是对.execute().actionGet() 进行了封装 142 System.out.println(indexResponse.getVersion()); 143 //得到这个数据的version,如果version=1代表是新添加的数据 144 } 145 146 147 148 /** 149 * index-2 hashmap 150 * 实际工作中使用 151 * @throws Exception 152 */ 153 @Test 154 public void test5() throws Exception {//把hashmap类型的数据放入index库 155 HashMap<String, Object> hashMap = new HashMap<String, Object>(); 156 //HashMap<String, Object> hashMap是迭代器变量 157 hashMap.put("name", "tom"); 158 hashMap.put("age", 15); 159 IndexResponse indexResponse = transportClient.prepareIndex(index, type, "2")//添加一个id=2的数据 160 .setSource(hashMap)//设值 161 .get(); 162 //.execute().actionGet(); 这个和上面的get()方法是一样的,get()就是对.execute().actionGet() 进行了封装 163 System.out.println(indexResponse.getVersion()); 164 } 165 test6测试(添加bean格式的数据) 需要引入对象,这里新建Person.java实体类 1 package zhouls.bigdata.myElasticsearch; 2 3 4 5 public class Person {//实体类 6 7 private String name; 8 private int age; 9 public String getName() { 10 return name; 11 } 12 public void setName(String name) { 13 this.name = name; 14 } 15 public int getAge() { 16 return age; 17 } 18 public void setAge(int age) { 19 this.age = age; 20 } 21 22 23 24 } 1 package zhouls.bigdata.myElasticsearch; 2 3 import static org.junit.Assert.*; 4 5 import java.net.InetAddress; 6 import java.util.HashMap; 7 import java.util.List; 8 9 import org.elasticsearch.action.bulk.BulkItemResponse; 10 import org.elasticsearch.action.bulk.BulkRequestBuilder; 11 import org.elasticsearch.action.bulk.BulkResponse; 12 import org.elasticsearch.action.delete.DeleteRequest; 13 import org.elasticsearch.action.get.GetResponse; 14 import org.elasticsearch.action.index.IndexRequest; 15 import org.elasticsearch.action.index.IndexResponse; 16 import org.elasticsearch.action.search.SearchResponse; 17 import org.elasticsearch.action.search.SearchType; 18 import org.elasticsearch.action.update.UpdateResponse; 19 import org.elasticsearch.client.transport.TransportClient; 20 import org.elasticsearch.cluster.node.DiscoveryNode; 21 import org.elasticsearch.common.settings.Settings; 22 import org.elasticsearch.common.transport.InetSocketTransportAddress; 23 import org.elasticsearch.common.transport.TransportAddress; 24 import org.elasticsearch.common.xcontent.XContentBuilder; 25 import org.elasticsearch.common.xcontent.XContentFactory; 26 import org.elasticsearch.index.query.MatchQueryBuilder.Operator; 27 import org.elasticsearch.index.query.QueryBuilders; 28 import org.elasticsearch.search.SearchHit; 29 import org.elasticsearch.search.SearchHits; 30 import org.elasticsearch.search.aggregations.Aggregation; 31 import org.elasticsearch.search.aggregations.AggregationBuilders; 32 import org.elasticsearch.search.aggregations.bucket.terms.Terms; 33 import org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket; 34 import org.elasticsearch.search.aggregations.metrics.sum.Sum; 35 import org.elasticsearch.search.sort.SortOrder; 36 import org.junit.Before; 37 import org.junit.Test; 38 39 import com.fasterxml.jackson.databind.ObjectMapper; 40 import com.google.common.collect.ImmutableList; 41 42 public class TestEs { 43 44 //es和hadoop没关系啊,获取一个transportclient就可以操作es了 45 46 47 private TransportClient transportClient; 48 @Before//@Before和@Test的区别:每次执行都要先经过@Before,好比是,它是一个模板。 49 //before表示在执行每个test方法之前运行,常与@Test搭配使用 50 public void test0() throws Exception { 51 //获取TransportClient,来操作es 52 transportClient = TransportClient.builder().build(); 53 //需要使用9300端口 54 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.10"), 9300); 55 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 56 transportClient.addTransportAddress(transportAddress); 57 } 58 59 /** 60 * 用java代码测试的时候这样写是没有问题的,比较简单 61 * @throws Exception 62 */ 63 @Test 64 public void test1() throws Exception { 65 //获取TransportClient,来操作es 66 TransportClient transportClient = TransportClient.builder().build(); 67 //需要使用9300端口,指定es集群中的节点信息, 这个地方指定的端口是节点和节点之间的通信端口是9300,不是Http请求的端口9200. 68 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.11"), 9300); 69 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 70 transportClient.addTransportAddress(transportAddress); 71 System.out.println(transportClient.toString()); 72 } 73 74 /** 75 * 可以这样写,防止代码中指定的链接失效 76 * 但是写起来比较麻烦 77 * 在实际工作中这样写不是很靠谱,需要完善,做测试可以 78 * @throws Exception 79 */ 80 @Test 81 public void test2() throws Exception { 82 //获取TransportClient,来操作es,通过TransportClient可以和es集群交互 83 TransportClient transportClient = TransportClient.builder().build(); 84 //需要使用9300端口,指定es集群中的节点信息, 这个地方指定的端口是节点和节点之间的通信端口是9300,不是Http请求的端口9200. 85 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.10"), 9300); 86 TransportAddress transportAddress1 = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.11"), 9300); 87 TransportAddress transportAddress2 = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.12"), 9300); 88 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 89 transportClient.addTransportAddresses(transportAddress,transportAddress1,transportAddress2);//加入多个地址 90 System.out.println(transportClient.toString()); 91 } 92 93 /** 94 * 实际生产环境下面,建议这样用,加上下面这些配置信息 95 * @throws Exception 96 */ 97 @Test 98 public void test3() throws Exception { 99 //指定es的配置信息 100 Settings settings = Settings.settingsBuilder() 101 .put("cluster.name", "elasticsearch")//集群名称 102 //如果集群名称在配置文件中被修改了,那么在这需要显式定义一下 103 //es集群名称默认是 elasticsearch sniff嗅; 发现; 104 .put("client.transport.sniff", true)//开启集群的嗅探功能,只需要指定集群中一个节点信息即可获取到集群中的所有节点信息 105 //开启集群的嗅探功能,这样可以保证es会自动把集群中的其他节点信息添加到transportClient里面 106 //开启嗅探功能后 只要指定集群中的任意一个可用节点就可以了.当把代码运行之后TransportClient里面会把集群中所有节点的信息都拿到,能识别集群中的所有节点. 107 .build(); 108 109 //获取TransportClient,来操作es,//通过TransportClient可以和es集群交互 110 TransportClient transportClient = TransportClient.builder().settings(settings).build(); 111 //需要使用9300端口,指定es集群中的节点信息, 这个地方指定的端口是节点和节点之间的通信端口是9300,不是Http请求的端口9200. 112 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.10"), 9300); 113 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 114 transportClient.addTransportAddress(transportAddress); 115 116 //获取client链接到的节点信息, //获取当前transportClient连接到了集群多少个节点 117 List<DiscoveryNode> connectedNodes = transportClient.connectedNodes(); 118 for (DiscoveryNode discoveryNode : connectedNodes) {//for星型循环,将connectedNodes的值,一一传给DiscoveryNode discoveryNode 119 System.out.println(discoveryNode.getHostName());//打印192.168.80.10;192.168.80.11;192.168.80.12 120 //如果加入transportClient.addTransportAddresses(transportAddress) 只有一个ip,打印的就只有一个. 121 } 122 } 123 124 125 String index = "zhouls";//设置索引库 126 String type = "emp";//设置类型 127 128 //索引index(四种格式:json,map,bean,es helper) 129 130 /** 131 * index-1 json 132 * 实际工作中使用 133 * @throws Exception 134 */ 135 @Test 136 public void test4() throws Exception { 137 String jsonStr = "{\"name\":\"tom zhang\",\"age\":19}";//需要转义下 //向索引库中传入一个String字符串,还可以接受其他类型 138 IndexResponse indexResponse = transportClient.prepareIndex(index, type, "1")//添加一个id=1的数据 139 .setSource(jsonStr)//设值,这是json格式的 140 .get(); 141 //.execute().actionGet(); 这个和上面的get()方法是一样的,get()就是对.execute().actionGet() 进行了封装 142 System.out.println(indexResponse.getVersion()); 143 //得到这个数据的version,如果version=1代表是新添加的数据 144 } 145 146 147 148 /** 149 * index-2 hashmap 150 * 实际工作中使用 151 * @throws Exception 152 */ 153 @Test 154 public void test5() throws Exception {//把hashmap类型的数据放入index库 155 HashMap<String, Object> hashMap = new HashMap<String, Object>(); 156 //HashMap<String, Object> hashMap是迭代器变量 157 hashMap.put("name", "tom"); 158 hashMap.put("age", 15); 159 IndexResponse indexResponse = transportClient.prepareIndex(index, type, "2")//添加一个id=2的数据 160 .setSource(hashMap)//设值 161 .get(); 162 //.execute().actionGet(); 这个和上面的get()方法是一样的,get()就是对.execute().actionGet() 进行了封装 163 System.out.println(indexResponse.getVersion()); 164 } 165 166 167 168 169 /** 170 * index-3 bean 171 * 实际工作中使用 172 * 使用对象的时候需要把对象中的属性转化成json字符串 173 * @throws Exception 174 */ 175 176 // <dependency> 177 // <groupId>com.fasterxml.jackson.core</groupId> 178 // <artifactId>jackson-databind</artifactId> 179 // <version>2.1.3</version> 180 // </dependency> 181 182 183 @Test 184 public void test6() throws Exception {//传入一个对象到index索引库,这里是Person对象 185 Person person = new Person(); 186 person.setName("mack"); 187 person.setAge(20); 188 189 //如果直接传入一个person对象会报错,java.lang.IllegalArgumentException,必须把对象转换成一个Json字符串,使用jackson依赖 190 //IndexResponse indexResponse = transportClient.prepareIndex(index, type, "9").setSource(person).get(); 191 192 193 ObjectMapper objectMapper = new ObjectMapper(); 194 String writeValueAsString = objectMapper.writeValueAsString(person); 195 IndexResponse indexResponse = transportClient.prepareIndex(index, type, "3") 196 .setSource(writeValueAsString) 197 .get(); 198 // IndexResponse indexResponse = transportClient.prepareIndex(index, type, "3").setSource(objectMapper.writeValueAsString(person)).get(); 199 200 System.out.println(indexResponse.getVersion()); 201 } test7测试(添加helper格式的数据) 1 package zhouls.bigdata.myElasticsearch; 2 3 import static org.junit.Assert.*; 4 5 import java.net.InetAddress; 6 import java.util.HashMap; 7 import java.util.List; 8 9 import org.elasticsearch.action.bulk.BulkItemResponse; 10 import org.elasticsearch.action.bulk.BulkRequestBuilder; 11 import org.elasticsearch.action.bulk.BulkResponse; 12 import org.elasticsearch.action.delete.DeleteRequest; 13 import org.elasticsearch.action.get.GetResponse; 14 import org.elasticsearch.action.index.IndexRequest; 15 import org.elasticsearch.action.index.IndexResponse; 16 import org.elasticsearch.action.search.SearchResponse; 17 import org.elasticsearch.action.search.SearchType; 18 import org.elasticsearch.action.update.UpdateResponse; 19 import org.elasticsearch.client.transport.TransportClient; 20 import org.elasticsearch.cluster.node.DiscoveryNode; 21 import org.elasticsearch.common.settings.Settings; 22 import org.elasticsearch.common.transport.InetSocketTransportAddress; 23 import org.elasticsearch.common.transport.TransportAddress; 24 import org.elasticsearch.common.xcontent.XContentBuilder; 25 import org.elasticsearch.common.xcontent.XContentFactory; 26 import org.elasticsearch.index.query.MatchQueryBuilder.Operator; 27 import org.elasticsearch.index.query.QueryBuilders; 28 import org.elasticsearch.search.SearchHit; 29 import org.elasticsearch.search.SearchHits; 30 import org.elasticsearch.search.aggregations.Aggregation; 31 import org.elasticsearch.search.aggregations.AggregationBuilders; 32 import org.elasticsearch.search.aggregations.bucket.terms.Terms; 33 import org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket; 34 import org.elasticsearch.search.aggregations.metrics.sum.Sum; 35 import org.elasticsearch.search.sort.SortOrder; 36 import org.junit.Before; 37 import org.junit.Test; 38 39 import com.fasterxml.jackson.databind.ObjectMapper; 40 import com.google.common.collect.ImmutableList; 41 42 public class TestEs { 43 44 //es和hadoop没关系啊,获取一个transportclient就可以操作es了 45 46 47 private TransportClient transportClient; 48 @Before//@Before和@Test的区别:每次执行都要先经过@Before,好比是,它是一个模板。 49 //before表示在执行每个test方法之前运行,常与@Test搭配使用 50 public void test0() throws Exception { 51 //获取TransportClient,来操作es 52 transportClient = TransportClient.builder().build(); 53 //需要使用9300端口 54 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.10"), 9300); 55 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 56 transportClient.addTransportAddress(transportAddress); 57 } 58 59 /** 60 * 用java代码测试的时候这样写是没有问题的,比较简单 61 * @throws Exception 62 */ 63 @Test 64 public void test1() throws Exception { 65 //获取TransportClient,来操作es 66 TransportClient transportClient = TransportClient.builder().build(); 67 //需要使用9300端口,指定es集群中的节点信息, 这个地方指定的端口是节点和节点之间的通信端口是9300,不是Http请求的端口9200. 68 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.11"), 9300); 69 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 70 transportClient.addTransportAddress(transportAddress); 71 System.out.println(transportClient.toString()); 72 } 73 74 /** 75 * 可以这样写,防止代码中指定的链接失效 76 * 但是写起来比较麻烦 77 * 在实际工作中这样写不是很靠谱,需要完善,做测试可以 78 * @throws Exception 79 */ 80 @Test 81 public void test2() throws Exception { 82 //获取TransportClient,来操作es,通过TransportClient可以和es集群交互 83 TransportClient transportClient = TransportClient.builder().build(); 84 //需要使用9300端口,指定es集群中的节点信息, 这个地方指定的端口是节点和节点之间的通信端口是9300,不是Http请求的端口9200. 85 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.10"), 9300); 86 TransportAddress transportAddress1 = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.11"), 9300); 87 TransportAddress transportAddress2 = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.12"), 9300); 88 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 89 transportClient.addTransportAddresses(transportAddress,transportAddress1,transportAddress2);//加入多个地址 90 System.out.println(transportClient.toString()); 91 } 92 93 /** 94 * 实际生产环境下面,建议这样用,加上下面这些配置信息 95 * @throws Exception 96 */ 97 @Test 98 public void test3() throws Exception { 99 //指定es的配置信息 100 Settings settings = Settings.settingsBuilder() 101 .put("cluster.name", "elasticsearch")//集群名称 102 //如果集群名称在配置文件中被修改了,那么在这需要显式定义一下 103 //es集群名称默认是 elasticsearch sniff嗅; 发现; 104 .put("client.transport.sniff", true)//开启集群的嗅探功能,只需要指定集群中一个节点信息即可获取到集群中的所有节点信息 105 //开启集群的嗅探功能,这样可以保证es会自动把集群中的其他节点信息添加到transportClient里面 106 //开启嗅探功能后 只要指定集群中的任意一个可用节点就可以了.当把代码运行之后TransportClient里面会把集群中所有节点的信息都拿到,能识别集群中的所有节点. 107 .build(); 108 109 //获取TransportClient,来操作es,//通过TransportClient可以和es集群交互 110 TransportClient transportClient = TransportClient.builder().settings(settings).build(); 111 //需要使用9300端口,指定es集群中的节点信息, 这个地方指定的端口是节点和节点之间的通信端口是9300,不是Http请求的端口9200. 112 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.10"), 9300); 113 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 114 transportClient.addTransportAddress(transportAddress); 115 116 //获取client链接到的节点信息, //获取当前transportClient连接到了集群多少个节点 117 List<DiscoveryNode> connectedNodes = transportClient.connectedNodes(); 118 for (DiscoveryNode discoveryNode : connectedNodes) {//for星型循环,将connectedNodes的值,一一传给DiscoveryNode discoveryNode 119 System.out.println(discoveryNode.getHostName());//打印192.168.80.10;192.168.80.11;192.168.80.12 120 //如果加入transportClient.addTransportAddresses(transportAddress) 只有一个ip,打印的就只有一个. 121 } 122 } 123 124 125 String index = "zhouls";//设置索引库 126 String type = "emp";//设置类型 127 128 //索引index(四种格式:json,map,bean,es helper) 129 130 /** 131 * index-1 json 132 * 实际工作中使用 133 * @throws Exception 134 */ 135 @Test 136 public void test4() throws Exception { 137 String jsonStr = "{\"name\":\"tom zhang\",\"age\":19}";//需要转义下 //向索引库中传入一个String字符串,还可以接受其他类型 138 IndexResponse indexResponse = transportClient.prepareIndex(index, type, "1")//添加一个id=1的数据 139 .setSource(jsonStr)//设值,这是json格式的 140 .get(); 141 //.execute().actionGet(); 这个和上面的get()方法是一样的,get()就是对.execute().actionGet() 进行了封装 142 System.out.println(indexResponse.getVersion()); 143 //得到这个数据的version,如果version=1代表是新添加的数据 144 } 145 146 147 148 /** 149 * index-2 hashmap 150 * 实际工作中使用 151 * @throws Exception 152 */ 153 @Test 154 public void test5() throws Exception {//把hashmap类型的数据放入index库 155 HashMap<String, Object> hashMap = new HashMap<String, Object>(); 156 //HashMap<String, Object> hashMap是迭代器变量 157 hashMap.put("name", "tom"); 158 hashMap.put("age", 15); 159 IndexResponse indexResponse = transportClient.prepareIndex(index, type, "2")//添加一个id=2的数据 160 .setSource(hashMap)//设值 161 .get(); 162 //.execute().actionGet(); 这个和上面的get()方法是一样的,get()就是对.execute().actionGet() 进行了封装 163 System.out.println(indexResponse.getVersion()); 164 } 165 166 167 168 169 /** 170 * index-3 bean 171 * 实际工作中使用 172 * 使用对象的时候需要把对象中的属性转化成json字符串 173 * @throws Exception 174 */ 175 176 // <dependency> 177 // <groupId>com.fasterxml.jackson.core</groupId> 178 // <artifactId>jackson-databind</artifactId> 179 // <version>2.1.3</version> 180 // </dependency> 181 182 183 @Test 184 public void test6() throws Exception {//传入一个对象到index索引库,这里是Person对象 185 Person person = new Person(); 186 person.setName("mack"); 187 person.setAge(20); 188 189 //如果直接传入一个person对象会报错,java.lang.IllegalArgumentException,必须把对象转换成一个Json字符串,使用jackson依赖 190 //IndexResponse indexResponse = transportClient.prepareIndex(index, type, "9").setSource(person).get(); 191 192 193 ObjectMapper objectMapper = new ObjectMapper(); 194 String writeValueAsString = objectMapper.writeValueAsString(person); 195 IndexResponse indexResponse = transportClient.prepareIndex(index, type, "3") 196 .setSource(writeValueAsString) 197 .get(); 198 // IndexResponse indexResponse = transportClient.prepareIndex(index, type, "3").setSource(objectMapper.writeValueAsString(person)).get(); 199 200 System.out.println(indexResponse.getVersion()); 201 } 202 203 /** 204 * index -4 es helper 205 * 测试数据这样使用 206 * @throws Exception 207 */ 208 @Test 209 public void test7() throws Exception { 210 XContentBuilder builder = XContentFactory.jsonBuilder()//XContentFactory 这个是ES官方提供的可以构建Json字符串的工具类. 211 .startObject() 212 .field("name", "jessic") 213 .field("age", 28) 214 .endObject(); 215 216 IndexResponse indexResponse = transportClient.prepareIndex(index, type, "4") 217 .setSource(builder) 218 .get(); 219 System.out.println(indexResponse.getVersion()); 220 } test8测试(通过id get查询数据) 1 package zhouls.bigdata.myElasticsearch; 2 3 import static org.junit.Assert.*; 4 5 import java.net.InetAddress; 6 import java.util.HashMap; 7 import java.util.List; 8 9 import org.elasticsearch.action.bulk.BulkItemResponse; 10 import org.elasticsearch.action.bulk.BulkRequestBuilder; 11 import org.elasticsearch.action.bulk.BulkResponse; 12 import org.elasticsearch.action.delete.DeleteRequest; 13 import org.elasticsearch.action.get.GetResponse; 14 import org.elasticsearch.action.index.IndexRequest; 15 import org.elasticsearch.action.index.IndexResponse; 16 import org.elasticsearch.action.search.SearchResponse; 17 import org.elasticsearch.action.search.SearchType; 18 import org.elasticsearch.action.update.UpdateResponse; 19 import org.elasticsearch.client.transport.TransportClient; 20 import org.elasticsearch.cluster.node.DiscoveryNode; 21 import org.elasticsearch.common.settings.Settings; 22 import org.elasticsearch.common.transport.InetSocketTransportAddress; 23 import org.elasticsearch.common.transport.TransportAddress; 24 import org.elasticsearch.common.xcontent.XContentBuilder; 25 import org.elasticsearch.common.xcontent.XContentFactory; 26 import org.elasticsearch.index.query.MatchQueryBuilder.Operator; 27 import org.elasticsearch.index.query.QueryBuilders; 28 import org.elasticsearch.search.SearchHit; 29 import org.elasticsearch.search.SearchHits; 30 import org.elasticsearch.search.aggregations.Aggregation; 31 import org.elasticsearch.search.aggregations.AggregationBuilders; 32 import org.elasticsearch.search.aggregations.bucket.terms.Terms; 33 import org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket; 34 import org.elasticsearch.search.aggregations.metrics.sum.Sum; 35 import org.elasticsearch.search.sort.SortOrder; 36 import org.junit.Before; 37 import org.junit.Test; 38 39 import com.fasterxml.jackson.databind.ObjectMapper; 40 import com.google.common.collect.ImmutableList; 41 42 public class TestEs { 43 44 //es和hadoop没关系啊,获取一个transportclient就可以操作es了 45 46 47 private TransportClient transportClient; 48 @Before//@Before和@Test的区别:每次执行都要先经过@Before,好比是,它是一个模板。 49 //before表示在执行每个test方法之前运行,常与@Test搭配使用 50 public void test0() throws Exception { 51 //获取TransportClient,来操作es 52 transportClient = TransportClient.builder().build(); 53 //需要使用9300端口 54 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.10"), 9300); 55 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 56 transportClient.addTransportAddress(transportAddress); 57 } 58 59 /** 60 * 用java代码测试的时候这样写是没有问题的,比较简单 61 * @throws Exception 62 */ 63 @Test 64 public void test1() throws Exception { 65 //获取TransportClient,来操作es 66 TransportClient transportClient = TransportClient.builder().build(); 67 //需要使用9300端口,指定es集群中的节点信息, 这个地方指定的端口是节点和节点之间的通信端口是9300,不是Http请求的端口9200. 68 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.11"), 9300); 69 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 70 transportClient.addTransportAddress(transportAddress); 71 System.out.println(transportClient.toString()); 72 } 73 74 /** 75 * 可以这样写,防止代码中指定的链接失效 76 * 但是写起来比较麻烦 77 * 在实际工作中这样写不是很靠谱,需要完善,做测试可以 78 * @throws Exception 79 */ 80 @Test 81 public void test2() throws Exception { 82 //获取TransportClient,来操作es,通过TransportClient可以和es集群交互 83 TransportClient transportClient = TransportClient.builder().build(); 84 //需要使用9300端口,指定es集群中的节点信息, 这个地方指定的端口是节点和节点之间的通信端口是9300,不是Http请求的端口9200. 85 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.10"), 9300); 86 TransportAddress transportAddress1 = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.11"), 9300); 87 TransportAddress transportAddress2 = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.12"), 9300); 88 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 89 transportClient.addTransportAddresses(transportAddress,transportAddress1,transportAddress2);//加入多个地址 90 System.out.println(transportClient.toString()); 91 } 92 93 /** 94 * 实际生产环境下面,建议这样用,加上下面这些配置信息 95 * @throws Exception 96 */ 97 @Test 98 public void test3() throws Exception { 99 //指定es的配置信息 100 Settings settings = Settings.settingsBuilder() 101 .put("cluster.name", "elasticsearch")//集群名称 102 //如果集群名称在配置文件中被修改了,那么在这需要显式定义一下 103 //es集群名称默认是 elasticsearch sniff嗅; 发现; 104 .put("client.transport.sniff", true)//开启集群的嗅探功能,只需要指定集群中一个节点信息即可获取到集群中的所有节点信息 105 //开启集群的嗅探功能,这样可以保证es会自动把集群中的其他节点信息添加到transportClient里面 106 //开启嗅探功能后 只要指定集群中的任意一个可用节点就可以了.当把代码运行之后TransportClient里面会把集群中所有节点的信息都拿到,能识别集群中的所有节点. 107 .build(); 108 109 //获取TransportClient,来操作es,//通过TransportClient可以和es集群交互 110 TransportClient transportClient = TransportClient.builder().settings(settings).build(); 111 //需要使用9300端口,指定es集群中的节点信息, 这个地方指定的端口是节点和节点之间的通信端口是9300,不是Http请求的端口9200. 112 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.10"), 9300); 113 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 114 transportClient.addTransportAddress(transportAddress); 115 116 //获取client链接到的节点信息, //获取当前transportClient连接到了集群多少个节点 117 List<DiscoveryNode> connectedNodes = transportClient.connectedNodes(); 118 for (DiscoveryNode discoveryNode : connectedNodes) {//for星型循环,将connectedNodes的值,一一传给DiscoveryNode discoveryNode 119 System.out.println(discoveryNode.getHostName());//打印192.168.80.10;192.168.80.11;192.168.80.12 120 //如果加入transportClient.addTransportAddresses(transportAddress) 只有一个ip,打印的就只有一个. 121 } 122 } 123 124 125 String index = "zhouls";//设置索引库 126 String type = "emp";//设置类型 127 128 //索引index(四种格式:json,map,bean,es helper) 129 130 /** 131 * index-1 json 132 * 实际工作中使用 133 * @throws Exception 134 */ 135 @Test 136 public void test4() throws Exception { 137 String jsonStr = "{\"name\":\"tom zhang\",\"age\":19}";//需要转义下 //向索引库中传入一个String字符串,还可以接受其他类型 138 IndexResponse indexResponse = transportClient.prepareIndex(index, type, "1")//添加一个id=1的数据 139 .setSource(jsonStr)//设值,这是json格式的 140 .get(); 141 //.execute().actionGet(); 这个和上面的get()方法是一样的,get()就是对.execute().actionGet() 进行了封装 142 System.out.println(indexResponse.getVersion()); 143 //得到这个数据的version,如果version=1代表是新添加的数据 144 } 145 146 147 148 /** 149 * index-2 hashmap 150 * 实际工作中使用 151 * @throws Exception 152 */ 153 @Test 154 public void test5() throws Exception {//把hashmap类型的数据放入index库 155 HashMap<String, Object> hashMap = new HashMap<String, Object>(); 156 //HashMap<String, Object> hashMap是迭代器变量 157 hashMap.put("name", "tom"); 158 hashMap.put("age", 15); 159 IndexResponse indexResponse = transportClient.prepareIndex(index, type, "2")//添加一个id=2的数据 160 .setSource(hashMap)//设值 161 .get(); 162 //.execute().actionGet(); 这个和上面的get()方法是一样的,get()就是对.execute().actionGet() 进行了封装 163 System.out.println(indexResponse.getVersion()); 164 } 165 166 167 168 169 /** 170 * index-3 bean 171 * 实际工作中使用 172 * 使用对象的时候需要把对象中的属性转化成json字符串 173 * @throws Exception 174 */ 175 176 // <dependency> 177 // <groupId>com.fasterxml.jackson.core</groupId> 178 // <artifactId>jackson-databind</artifactId> 179 // <version>2.1.3</version> 180 // </dependency> 181 182 183 @Test 184 public void test6() throws Exception {//传入一个对象到index索引库,这里是Person对象 185 Person person = new Person(); 186 person.setName("mack"); 187 person.setAge(20); 188 189 //如果直接传入一个person对象会报错,java.lang.IllegalArgumentException,必须把对象转换成一个Json字符串,使用jackson依赖 190 //IndexResponse indexResponse = transportClient.prepareIndex(index, type, "9").setSource(person).get(); 191 192 193 ObjectMapper objectMapper = new ObjectMapper(); 194 String writeValueAsString = objectMapper.writeValueAsString(person); 195 IndexResponse indexResponse = transportClient.prepareIndex(index, type, "3") 196 .setSource(writeValueAsString) 197 .get(); 198 // IndexResponse indexResponse = transportClient.prepareIndex(index, type, "3").setSource(objectMapper.writeValueAsString(person)).get(); 199 200 System.out.println(indexResponse.getVersion()); 201 } 202 203 /** 204 * index -4 es helper 205 * 测试数据这样使用 206 * @throws Exception 207 */ 208 @Test 209 public void test7() throws Exception { 210 XContentBuilder builder = XContentFactory.jsonBuilder()//XContentFactory 这个是ES官方提供的可以构建Json字符串的工具类. 211 .startObject() 212 .field("name", "jessic") 213 .field("age", 28) 214 .endObject(); 215 216 IndexResponse indexResponse = transportClient.prepareIndex(index, type, "4") 217 .setSource(builder) 218 .get(); 219 System.out.println(indexResponse.getVersion()); 220 } 221 222 223 /** 224 * get 查询 225 * 通过id查询 226 * @throws Exception 227 */ 228 @Test 229 public void test8() throws Exception { 230 GetResponse getResponse = transportClient.prepareGet(index, type, "4")//查询id为4的数据 231 .get(); 232 System.out.println(getResponse.getSourceAsString()); 233 } test9测试(局部更新数据) 1 package zhouls.bigdata.myElasticsearch; 2 3 import static org.junit.Assert.*; 4 5 import java.net.InetAddress; 6 import java.util.HashMap; 7 import java.util.List; 8 9 import org.elasticsearch.action.bulk.BulkItemResponse; 10 import org.elasticsearch.action.bulk.BulkRequestBuilder; 11 import org.elasticsearch.action.bulk.BulkResponse; 12 import org.elasticsearch.action.delete.DeleteRequest; 13 import org.elasticsearch.action.get.GetResponse; 14 import org.elasticsearch.action.index.IndexRequest; 15 import org.elasticsearch.action.index.IndexResponse; 16 import org.elasticsearch.action.search.SearchResponse; 17 import org.elasticsearch.action.search.SearchType; 18 import org.elasticsearch.action.update.UpdateResponse; 19 import org.elasticsearch.client.transport.TransportClient; 20 import org.elasticsearch.cluster.node.DiscoveryNode; 21 import org.elasticsearch.common.settings.Settings; 22 import org.elasticsearch.common.transport.InetSocketTransportAddress; 23 import org.elasticsearch.common.transport.TransportAddress; 24 import org.elasticsearch.common.xcontent.XContentBuilder; 25 import org.elasticsearch.common.xcontent.XContentFactory; 26 import org.elasticsearch.index.query.MatchQueryBuilder.Operator; 27 import org.elasticsearch.index.query.QueryBuilders; 28 import org.elasticsearch.search.SearchHit; 29 import org.elasticsearch.search.SearchHits; 30 import org.elasticsearch.search.aggregations.Aggregation; 31 import org.elasticsearch.search.aggregations.AggregationBuilders; 32 import org.elasticsearch.search.aggregations.bucket.terms.Terms; 33 import org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket; 34 import org.elasticsearch.search.aggregations.metrics.sum.Sum; 35 import org.elasticsearch.search.sort.SortOrder; 36 import org.junit.Before; 37 import org.junit.Test; 38 39 import com.fasterxml.jackson.databind.ObjectMapper; 40 import com.google.common.collect.ImmutableList; 41 42 public class TestEs { 43 44 //es和hadoop没关系啊,获取一个transportclient就可以操作es了 45 46 47 private TransportClient transportClient; 48 @Before//@Before和@Test的区别:每次执行都要先经过@Before,好比是,它是一个模板。 49 //before表示在执行每个test方法之前运行,常与@Test搭配使用 50 public void test0() throws Exception { 51 //获取TransportClient,来操作es 52 transportClient = TransportClient.builder().build(); 53 //需要使用9300端口 54 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.10"), 9300); 55 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 56 transportClient.addTransportAddress(transportAddress); 57 } 58 59 /** 60 * 用java代码测试的时候这样写是没有问题的,比较简单 61 * @throws Exception 62 */ 63 @Test 64 public void test1() throws Exception { 65 //获取TransportClient,来操作es 66 TransportClient transportClient = TransportClient.builder().build(); 67 //需要使用9300端口,指定es集群中的节点信息, 这个地方指定的端口是节点和节点之间的通信端口是9300,不是Http请求的端口9200. 68 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.11"), 9300); 69 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 70 transportClient.addTransportAddress(transportAddress); 71 System.out.println(transportClient.toString()); 72 } 73 74 /** 75 * 可以这样写,防止代码中指定的链接失效 76 * 但是写起来比较麻烦 77 * 在实际工作中这样写不是很靠谱,需要完善,做测试可以 78 * @throws Exception 79 */ 80 @Test 81 public void test2() throws Exception { 82 //获取TransportClient,来操作es,通过TransportClient可以和es集群交互 83 TransportClient transportClient = TransportClient.builder().build(); 84 //需要使用9300端口,指定es集群中的节点信息, 这个地方指定的端口是节点和节点之间的通信端口是9300,不是Http请求的端口9200. 85 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.10"), 9300); 86 TransportAddress transportAddress1 = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.11"), 9300); 87 TransportAddress transportAddress2 = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.12"), 9300); 88 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 89 transportClient.addTransportAddresses(transportAddress,transportAddress1,transportAddress2);//加入多个地址 90 System.out.println(transportClient.toString()); 91 } 92 93 /** 94 * 实际生产环境下面,建议这样用,加上下面这些配置信息 95 * @throws Exception 96 */ 97 @Test 98 public void test3() throws Exception { 99 //指定es的配置信息 100 Settings settings = Settings.settingsBuilder() 101 .put("cluster.name", "elasticsearch")//集群名称 102 //如果集群名称在配置文件中被修改了,那么在这需要显式定义一下 103 //es集群名称默认是 elasticsearch sniff嗅; 发现; 104 .put("client.transport.sniff", true)//开启集群的嗅探功能,只需要指定集群中一个节点信息即可获取到集群中的所有节点信息 105 //开启集群的嗅探功能,这样可以保证es会自动把集群中的其他节点信息添加到transportClient里面 106 //开启嗅探功能后 只要指定集群中的任意一个可用节点就可以了.当把代码运行之后TransportClient里面会把集群中所有节点的信息都拿到,能识别集群中的所有节点. 107 .build(); 108 109 //获取TransportClient,来操作es,//通过TransportClient可以和es集群交互 110 TransportClient transportClient = TransportClient.builder().settings(settings).build(); 111 //需要使用9300端口,指定es集群中的节点信息, 这个地方指定的端口是节点和节点之间的通信端口是9300,不是Http请求的端口9200. 112 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.10"), 9300); 113 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 114 transportClient.addTransportAddress(transportAddress); 115 116 //获取client链接到的节点信息, //获取当前transportClient连接到了集群多少个节点 117 List<DiscoveryNode> connectedNodes = transportClient.connectedNodes(); 118 for (DiscoveryNode discoveryNode : connectedNodes) {//for星型循环,将connectedNodes的值,一一传给DiscoveryNode discoveryNode 119 System.out.println(discoveryNode.getHostName());//打印192.168.80.10;192.168.80.11;192.168.80.12 120 //如果加入transportClient.addTransportAddresses(transportAddress) 只有一个ip,打印的就只有一个. 121 } 122 } 123 124 125 String index = "zhouls";//设置索引库 126 String type = "emp";//设置类型 127 128 //索引index(四种格式:json,map,bean,es helper) 129 130 /** 131 * index-1 json 132 * 实际工作中使用 133 * @throws Exception 134 */ 135 @Test 136 public void test4() throws Exception { 137 String jsonStr = "{\"name\":\"tom zhang\",\"age\":19}";//需要转义下 //向索引库中传入一个String字符串,还可以接受其他类型 138 IndexResponse indexResponse = transportClient.prepareIndex(index, type, "1")//添加一个id=1的数据 139 .setSource(jsonStr)//设值,这是json格式的 140 .get(); 141 //.execute().actionGet(); 这个和上面的get()方法是一样的,get()就是对.execute().actionGet() 进行了封装 142 System.out.println(indexResponse.getVersion()); 143 //得到这个数据的version,如果version=1代表是新添加的数据 144 } 145 146 147 148 /** 149 * index-2 hashmap 150 * 实际工作中使用 151 * @throws Exception 152 */ 153 @Test 154 public void test5() throws Exception {//把hashmap类型的数据放入index库 155 HashMap<String, Object> hashMap = new HashMap<String, Object>(); 156 //HashMap<String, Object> hashMap是迭代器变量 157 hashMap.put("name", "tom"); 158 hashMap.put("age", 15); 159 IndexResponse indexResponse = transportClient.prepareIndex(index, type, "2")//添加一个id=2的数据 160 .setSource(hashMap)//设值 161 .get(); 162 //.execute().actionGet(); 这个和上面的get()方法是一样的,get()就是对.execute().actionGet() 进行了封装 163 System.out.println(indexResponse.getVersion()); 164 } 165 166 167 168 169 /** 170 * index-3 bean 171 * 实际工作中使用 172 * 使用对象的时候需要把对象中的属性转化成json字符串 173 * @throws Exception 174 */ 175 176 // <dependency> 177 // <groupId>com.fasterxml.jackson.core</groupId> 178 // <artifactId>jackson-databind</artifactId> 179 // <version>2.1.3</version> 180 // </dependency> 181 182 183 @Test 184 public void test6() throws Exception {//传入一个对象到index索引库,这里是Person对象 185 Person person = new Person(); 186 person.setName("mack"); 187 person.setAge(20); 188 189 //如果直接传入一个person对象会报错,java.lang.IllegalArgumentException,必须把对象转换成一个Json字符串,使用jackson依赖 190 //IndexResponse indexResponse = transportClient.prepareIndex(index, type, "9").setSource(person).get(); 191 192 193 ObjectMapper objectMapper = new ObjectMapper(); 194 String writeValueAsString = objectMapper.writeValueAsString(person); 195 IndexResponse indexResponse = transportClient.prepareIndex(index, type, "3") 196 .setSource(writeValueAsString) 197 .get(); 198 // IndexResponse indexResponse = transportClient.prepareIndex(index, type, "3").setSource(objectMapper.writeValueAsString(person)).get(); 199 200 System.out.println(indexResponse.getVersion()); 201 } 202 203 /** 204 * index -4 es helper 205 * 测试数据这样使用 206 * @throws Exception 207 */ 208 @Test 209 public void test7() throws Exception { 210 XContentBuilder builder = XContentFactory.jsonBuilder()//XContentFactory 这个是ES官方提供的可以构建Json字符串的工具类. 211 .startObject() 212 .field("name", "jessic") 213 .field("age", 28) 214 .endObject(); 215 216 IndexResponse indexResponse = transportClient.prepareIndex(index, type, "4") 217 .setSource(builder) 218 .get(); 219 System.out.println(indexResponse.getVersion()); 220 } 221 222 223 /** 224 * get 查询 225 * 通过id查询 226 * @throws Exception 227 */ 228 @Test 229 public void test8() throws Exception { 230 GetResponse getResponse = transportClient.prepareGet(index, type, "4")//查询id为4的数据 231 .get(); 232 System.out.println(getResponse.getSourceAsString()); 233 } 234 235 /** 236 * 局部更新 237 * @throws Exception 238 */ 239 @Test 240 public void test9() throws Exception { 241 XContentBuilder builder = XContentFactory.jsonBuilder()//XContentFactory 这个是ES官方提供的可以构建Json字符串的工具类. 242 .startObject() 243 .field("age", 29) 244 .endObject(); 245 246 UpdateResponse updateResponse = transportClient.prepareUpdate(index, type, "4")//更新id为4的数据 247 .setDoc(builder) 248 .get(); 249 System.out.println(updateResponse.getVersion());//version打印2 数据更新 250 } test10测试(通过id来删除数据) 1 package zhouls.bigdata.myElasticsearch; 2 3 import static org.junit.Assert.*; 4 5 import java.net.InetAddress; 6 import java.util.HashMap; 7 import java.util.List; 8 9 import org.elasticsearch.action.bulk.BulkItemResponse; 10 import org.elasticsearch.action.bulk.BulkRequestBuilder; 11 import org.elasticsearch.action.bulk.BulkResponse; 12 import org.elasticsearch.action.delete.DeleteRequest; 13 import org.elasticsearch.action.get.GetResponse; 14 import org.elasticsearch.action.index.IndexRequest; 15 import org.elasticsearch.action.index.IndexResponse; 16 import org.elasticsearch.action.search.SearchResponse; 17 import org.elasticsearch.action.search.SearchType; 18 import org.elasticsearch.action.update.UpdateResponse; 19 import org.elasticsearch.client.transport.TransportClient; 20 import org.elasticsearch.cluster.node.DiscoveryNode; 21 import org.elasticsearch.common.settings.Settings; 22 import org.elasticsearch.common.transport.InetSocketTransportAddress; 23 import org.elasticsearch.common.transport.TransportAddress; 24 import org.elasticsearch.common.xcontent.XContentBuilder; 25 import org.elasticsearch.common.xcontent.XContentFactory; 26 import org.elasticsearch.index.query.MatchQueryBuilder.Operator; 27 import org.elasticsearch.index.query.QueryBuilders; 28 import org.elasticsearch.search.SearchHit; 29 import org.elasticsearch.search.SearchHits; 30 import org.elasticsearch.search.aggregations.Aggregation; 31 import org.elasticsearch.search.aggregations.AggregationBuilders; 32 import org.elasticsearch.search.aggregations.bucket.terms.Terms; 33 import org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket; 34 import org.elasticsearch.search.aggregations.metrics.sum.Sum; 35 import org.elasticsearch.search.sort.SortOrder; 36 import org.junit.Before; 37 import org.junit.Test; 38 39 import com.fasterxml.jackson.databind.ObjectMapper; 40 import com.google.common.collect.ImmutableList; 41 42 public class TestEs { 43 44 //es和hadoop没关系啊,获取一个transportclient就可以操作es了 45 46 47 private TransportClient transportClient; 48 @Before//@Before和@Test的区别:每次执行都要先经过@Before,好比是,它是一个模板。 49 //before表示在执行每个test方法之前运行,常与@Test搭配使用 50 public void test0() throws Exception { 51 //获取TransportClient,来操作es 52 transportClient = TransportClient.builder().build(); 53 //需要使用9300端口 54 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.10"), 9300); 55 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 56 transportClient.addTransportAddress(transportAddress); 57 } 58 59 /** 60 * 用java代码测试的时候这样写是没有问题的,比较简单 61 * @throws Exception 62 */ 63 @Test 64 public void test1() throws Exception { 65 //获取TransportClient,来操作es 66 TransportClient transportClient = TransportClient.builder().build(); 67 //需要使用9300端口,指定es集群中的节点信息, 这个地方指定的端口是节点和节点之间的通信端口是9300,不是Http请求的端口9200. 68 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.11"), 9300); 69 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 70 transportClient.addTransportAddress(transportAddress); 71 System.out.println(transportClient.toString()); 72 } 73 74 /** 75 * 可以这样写,防止代码中指定的链接失效 76 * 但是写起来比较麻烦 77 * 在实际工作中这样写不是很靠谱,需要完善,做测试可以 78 * @throws Exception 79 */ 80 @Test 81 public void test2() throws Exception { 82 //获取TransportClient,来操作es,通过TransportClient可以和es集群交互 83 TransportClient transportClient = TransportClient.builder().build(); 84 //需要使用9300端口,指定es集群中的节点信息, 这个地方指定的端口是节点和节点之间的通信端口是9300,不是Http请求的端口9200. 85 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.10"), 9300); 86 TransportAddress transportAddress1 = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.11"), 9300); 87 TransportAddress transportAddress2 = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.12"), 9300); 88 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 89 transportClient.addTransportAddresses(transportAddress,transportAddress1,transportAddress2);//加入多个地址 90 System.out.println(transportClient.toString()); 91 } 92 93 /** 94 * 实际生产环境下面,建议这样用,加上下面这些配置信息 95 * @throws Exception 96 */ 97 @Test 98 public void test3() throws Exception { 99 //指定es的配置信息 100 Settings settings = Settings.settingsBuilder() 101 .put("cluster.name", "elasticsearch")//集群名称 102 //如果集群名称在配置文件中被修改了,那么在这需要显式定义一下 103 //es集群名称默认是 elasticsearch sniff嗅; 发现; 104 .put("client.transport.sniff", true)//开启集群的嗅探功能,只需要指定集群中一个节点信息即可获取到集群中的所有节点信息 105 //开启集群的嗅探功能,这样可以保证es会自动把集群中的其他节点信息添加到transportClient里面 106 //开启嗅探功能后 只要指定集群中的任意一个可用节点就可以了.当把代码运行之后TransportClient里面会把集群中所有节点的信息都拿到,能识别集群中的所有节点. 107 .build(); 108 109 //获取TransportClient,来操作es,//通过TransportClient可以和es集群交互 110 TransportClient transportClient = TransportClient.builder().settings(settings).build(); 111 //需要使用9300端口,指定es集群中的节点信息, 这个地方指定的端口是节点和节点之间的通信端口是9300,不是Http请求的端口9200. 112 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.10"), 9300); 113 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 114 transportClient.addTransportAddress(transportAddress); 115 116 //获取client链接到的节点信息, //获取当前transportClient连接到了集群多少个节点 117 List<DiscoveryNode> connectedNodes = transportClient.connectedNodes(); 118 for (DiscoveryNode discoveryNode : connectedNodes) {//for星型循环,将connectedNodes的值,一一传给DiscoveryNode discoveryNode 119 System.out.println(discoveryNode.getHostName());//打印192.168.80.10;192.168.80.11;192.168.80.12 120 //如果加入transportClient.addTransportAddresses(transportAddress) 只有一个ip,打印的就只有一个. 121 } 122 } 123 124 125 String index = "zhouls";//设置索引库 126 String type = "emp";//设置类型 127 128 //索引index(四种格式:json,map,bean,es helper) 129 130 /** 131 * index-1 json 132 * 实际工作中使用 133 * @throws Exception 134 */ 135 @Test 136 public void test4() throws Exception { 137 String jsonStr = "{\"name\":\"tom zhang\",\"age\":19}";//需要转义下 //向索引库中传入一个String字符串,还可以接受其他类型 138 IndexResponse indexResponse = transportClient.prepareIndex(index, type, "1")//添加一个id=1的数据 139 .setSource(jsonStr)//设值,这是json格式的 140 .get(); 141 //.execute().actionGet(); 这个和上面的get()方法是一样的,get()就是对.execute().actionGet() 进行了封装 142 System.out.println(indexResponse.getVersion()); 143 //得到这个数据的version,如果version=1代表是新添加的数据 144 } 145 146 147 148 /** 149 * index-2 hashmap 150 * 实际工作中使用 151 * @throws Exception 152 */ 153 @Test 154 public void test5() throws Exception {//把hashmap类型的数据放入index库 155 HashMap<String, Object> hashMap = new HashMap<String, Object>(); 156 //HashMap<String, Object> hashMap是迭代器变量 157 hashMap.put("name", "tom"); 158 hashMap.put("age", 15); 159 IndexResponse indexResponse = transportClient.prepareIndex(index, type, "2")//添加一个id=2的数据 160 .setSource(hashMap)//设值 161 .get(); 162 //.execute().actionGet(); 这个和上面的get()方法是一样的,get()就是对.execute().actionGet() 进行了封装 163 System.out.println(indexResponse.getVersion()); 164 } 165 166 167 168 169 /** 170 * index-3 bean 171 * 实际工作中使用 172 * 使用对象的时候需要把对象中的属性转化成json字符串 173 * @throws Exception 174 */ 175 176 // <dependency> 177 // <groupId>com.fasterxml.jackson.core</groupId> 178 // <artifactId>jackson-databind</artifactId> 179 // <version>2.1.3</version> 180 // </dependency> 181 182 183 @Test 184 public void test6() throws Exception {//传入一个对象到index索引库,这里是Person对象 185 Person person = new Person(); 186 person.setName("mack"); 187 person.setAge(20); 188 189 //如果直接传入一个person对象会报错,java.lang.IllegalArgumentException,必须把对象转换成一个Json字符串,使用jackson依赖 190 //IndexResponse indexResponse = transportClient.prepareIndex(index, type, "9").setSource(person).get(); 191 192 193 ObjectMapper objectMapper = new ObjectMapper(); 194 String writeValueAsString = objectMapper.writeValueAsString(person); 195 IndexResponse indexResponse = transportClient.prepareIndex(index, type, "3") 196 .setSource(writeValueAsString) 197 .get(); 198 // IndexResponse indexResponse = transportClient.prepareIndex(index, type, "3").setSource(objectMapper.writeValueAsString(person)).get(); 199 200 System.out.println(indexResponse.getVersion()); 201 } 202 203 /** 204 * index -4 es helper 205 * 测试数据这样使用 206 * @throws Exception 207 */ 208 @Test 209 public void test7() throws Exception { 210 XContentBuilder builder = XContentFactory.jsonBuilder()//XContentFactory 这个是ES官方提供的可以构建Json字符串的工具类. 211 .startObject() 212 .field("name", "jessic") 213 .field("age", 28) 214 .endObject(); 215 216 IndexResponse indexResponse = transportClient.prepareIndex(index, type, "4") 217 .setSource(builder) 218 .get(); 219 System.out.println(indexResponse.getVersion()); 220 } 221 222 223 /** 224 * get 查询 225 * 通过id查询 226 * @throws Exception 227 */ 228 @Test 229 public void test8() throws Exception { 230 GetResponse getResponse = transportClient.prepareGet(index, type, "4")//查询id为4的数据 231 .get(); 232 System.out.println(getResponse.getSourceAsString()); 233 } 234 235 /** 236 * 局部更新 237 * @throws Exception 238 */ 239 @Test 240 public void test9() throws Exception { 241 XContentBuilder builder = XContentFactory.jsonBuilder()//XContentFactory 这个是ES官方提供的可以构建Json字符串的工具类. 242 .startObject() 243 .field("age", 29) 244 .endObject(); 245 246 UpdateResponse updateResponse = transportClient.prepareUpdate(index, type, "4")//更新id为4的数据 247 .setDoc(builder) 248 .get(); 249 System.out.println(updateResponse.getVersion());//version打印2 数据更新 250 } 251 252 /** 253 * 删除 254 * 通过id删除 255 * @throws Exception 256 */ 257 @Test 258 public void test10() throws Exception { 259 transportClient.prepareDelete(index, type, "4")//删除id为4的数据 260 .get(); 261 } test11测试(求数据行总数) 1 package zhouls.bigdata.myElasticsearch; 2 3 import static org.junit.Assert.*; 4 5 import java.net.InetAddress; 6 import java.util.HashMap; 7 import java.util.List; 8 9 import org.elasticsearch.action.bulk.BulkItemResponse; 10 import org.elasticsearch.action.bulk.BulkRequestBuilder; 11 import org.elasticsearch.action.bulk.BulkResponse; 12 import org.elasticsearch.action.delete.DeleteRequest; 13 import org.elasticsearch.action.get.GetResponse; 14 import org.elasticsearch.action.index.IndexRequest; 15 import org.elasticsearch.action.index.IndexResponse; 16 import org.elasticsearch.action.search.SearchResponse; 17 import org.elasticsearch.action.search.SearchType; 18 import org.elasticsearch.action.update.UpdateResponse; 19 import org.elasticsearch.client.transport.TransportClient; 20 import org.elasticsearch.cluster.node.DiscoveryNode; 21 import org.elasticsearch.common.settings.Settings; 22 import org.elasticsearch.common.transport.InetSocketTransportAddress; 23 import org.elasticsearch.common.transport.TransportAddress; 24 import org.elasticsearch.common.xcontent.XContentBuilder; 25 import org.elasticsearch.common.xcontent.XContentFactory; 26 import org.elasticsearch.index.query.MatchQueryBuilder.Operator; 27 import org.elasticsearch.index.query.QueryBuilders; 28 import org.elasticsearch.search.SearchHit; 29 import org.elasticsearch.search.SearchHits; 30 import org.elasticsearch.search.aggregations.Aggregation; 31 import org.elasticsearch.search.aggregations.AggregationBuilders; 32 import org.elasticsearch.search.aggregations.bucket.terms.Terms; 33 import org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket; 34 import org.elasticsearch.search.aggregations.metrics.sum.Sum; 35 import org.elasticsearch.search.sort.SortOrder; 36 import org.junit.Before; 37 import org.junit.Test; 38 39 import com.fasterxml.jackson.databind.ObjectMapper; 40 import com.google.common.collect.ImmutableList; 41 42 public class TestEs { 43 44 //es和hadoop没关系啊,获取一个transportclient就可以操作es了 45 46 47 private TransportClient transportClient; 48 @Before//@Before和@Test的区别:每次执行都要先经过@Before,好比是,它是一个模板。 49 //before表示在执行每个test方法之前运行,常与@Test搭配使用 50 public void test0() throws Exception { 51 //获取TransportClient,来操作es 52 transportClient = TransportClient.builder().build(); 53 //需要使用9300端口 54 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.10"), 9300); 55 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 56 transportClient.addTransportAddress(transportAddress); 57 } 58 59 /** 60 * 用java代码测试的时候这样写是没有问题的,比较简单 61 * @throws Exception 62 */ 63 @Test 64 public void test1() throws Exception { 65 //获取TransportClient,来操作es 66 TransportClient transportClient = TransportClient.builder().build(); 67 //需要使用9300端口,指定es集群中的节点信息, 这个地方指定的端口是节点和节点之间的通信端口是9300,不是Http请求的端口9200. 68 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.11"), 9300); 69 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 70 transportClient.addTransportAddress(transportAddress); 71 System.out.println(transportClient.toString()); 72 } 73 74 /** 75 * 可以这样写,防止代码中指定的链接失效 76 * 但是写起来比较麻烦 77 * 在实际工作中这样写不是很靠谱,需要完善,做测试可以 78 * @throws Exception 79 */ 80 @Test 81 public void test2() throws Exception { 82 //获取TransportClient,来操作es,通过TransportClient可以和es集群交互 83 TransportClient transportClient = TransportClient.builder().build(); 84 //需要使用9300端口,指定es集群中的节点信息, 这个地方指定的端口是节点和节点之间的通信端口是9300,不是Http请求的端口9200. 85 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.10"), 9300); 86 TransportAddress transportAddress1 = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.11"), 9300); 87 TransportAddress transportAddress2 = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.12"), 9300); 88 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 89 transportClient.addTransportAddresses(transportAddress,transportAddress1,transportAddress2);//加入多个地址 90 System.out.println(transportClient.toString()); 91 } 92 93 /** 94 * 实际生产环境下面,建议这样用,加上下面这些配置信息 95 * @throws Exception 96 */ 97 @Test 98 public void test3() throws Exception { 99 //指定es的配置信息 100 Settings settings = Settings.settingsBuilder() 101 .put("cluster.name", "elasticsearch")//集群名称 102 //如果集群名称在配置文件中被修改了,那么在这需要显式定义一下 103 //es集群名称默认是 elasticsearch sniff嗅; 发现; 104 .put("client.transport.sniff", true)//开启集群的嗅探功能,只需要指定集群中一个节点信息即可获取到集群中的所有节点信息 105 //开启集群的嗅探功能,这样可以保证es会自动把集群中的其他节点信息添加到transportClient里面 106 //开启嗅探功能后 只要指定集群中的任意一个可用节点就可以了.当把代码运行之后TransportClient里面会把集群中所有节点的信息都拿到,能识别集群中的所有节点. 107 .build(); 108 109 //获取TransportClient,来操作es,//通过TransportClient可以和es集群交互 110 TransportClient transportClient = TransportClient.builder().settings(settings).build(); 111 //需要使用9300端口,指定es集群中的节点信息, 这个地方指定的端口是节点和节点之间的通信端口是9300,不是Http请求的端口9200. 112 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.10"), 9300); 113 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 114 transportClient.addTransportAddress(transportAddress); 115 116 //获取client链接到的节点信息, //获取当前transportClient连接到了集群多少个节点 117 List<DiscoveryNode> connectedNodes = transportClient.connectedNodes(); 118 for (DiscoveryNode discoveryNode : connectedNodes) {//for星型循环,将connectedNodes的值,一一传给DiscoveryNode discoveryNode 119 System.out.println(discoveryNode.getHostName());//打印192.168.80.10;192.168.80.11;192.168.80.12 120 //如果加入transportClient.addTransportAddresses(transportAddress) 只有一个ip,打印的就只有一个. 121 } 122 } 123 124 125 String index = "zhouls";//设置索引库 126 String type = "emp";//设置类型 127 128 //索引index(四种格式:json,map,bean,es helper) 129 130 /** 131 * index-1 json 132 * 实际工作中使用 133 * @throws Exception 134 */ 135 @Test 136 public void test4() throws Exception { 137 String jsonStr = "{\"name\":\"tom zhang\",\"age\":19}";//需要转义下 //向索引库中传入一个String字符串,还可以接受其他类型 138 IndexResponse indexResponse = transportClient.prepareIndex(index, type, "1")//添加一个id=1的数据 139 .setSource(jsonStr)//设值,这是json格式的 140 .get(); 141 //.execute().actionGet(); 这个和上面的get()方法是一样的,get()就是对.execute().actionGet() 进行了封装 142 System.out.println(indexResponse.getVersion()); 143 //得到这个数据的version,如果version=1代表是新添加的数据 144 } 145 146 147 148 /** 149 * index-2 hashmap 150 * 实际工作中使用 151 * @throws Exception 152 */ 153 @Test 154 public void test5() throws Exception {//把hashmap类型的数据放入index库 155 HashMap<String, Object> hashMap = new HashMap<String, Object>(); 156 //HashMap<String, Object> hashMap是迭代器变量 157 hashMap.put("name", "tom"); 158 hashMap.put("age", 15); 159 IndexResponse indexResponse = transportClient.prepareIndex(index, type, "2")//添加一个id=2的数据 160 .setSource(hashMap)//设值 161 .get(); 162 //.execute().actionGet(); 这个和上面的get()方法是一样的,get()就是对.execute().actionGet() 进行了封装 163 System.out.println(indexResponse.getVersion()); 164 } 165 166 167 168 169 /** 170 * index-3 bean 171 * 实际工作中使用 172 * 使用对象的时候需要把对象中的属性转化成json字符串 173 * @throws Exception 174 */ 175 176 // <dependency> 177 // <groupId>com.fasterxml.jackson.core</groupId> 178 // <artifactId>jackson-databind</artifactId> 179 // <version>2.1.3</version> 180 // </dependency> 181 182 183 @Test 184 public void test6() throws Exception {//传入一个对象到index索引库,这里是Person对象 185 Person person = new Person(); 186 person.setName("mack"); 187 person.setAge(20); 188 189 //如果直接传入一个person对象会报错,java.lang.IllegalArgumentException,必须把对象转换成一个Json字符串,使用jackson依赖 190 //IndexResponse indexResponse = transportClient.prepareIndex(index, type, "9").setSource(person).get(); 191 192 193 ObjectMapper objectMapper = new ObjectMapper(); 194 String writeValueAsString = objectMapper.writeValueAsString(person); 195 IndexResponse indexResponse = transportClient.prepareIndex(index, type, "3") 196 .setSource(writeValueAsString) 197 .get(); 198 // IndexResponse indexResponse = transportClient.prepareIndex(index, type, "3").setSource(objectMapper.writeValueAsString(person)).get(); 199 200 System.out.println(indexResponse.getVersion()); 201 } 202 203 /** 204 * index -4 es helper 205 * 测试数据这样使用 206 * @throws Exception 207 */ 208 @Test 209 public void test7() throws Exception { 210 XContentBuilder builder = XContentFactory.jsonBuilder()//XContentFactory 这个是ES官方提供的可以构建Json字符串的工具类. 211 .startObject() 212 .field("name", "jessic") 213 .field("age", 28) 214 .endObject(); 215 216 IndexResponse indexResponse = transportClient.prepareIndex(index, type, "4") 217 .setSource(builder) 218 .get(); 219 System.out.println(indexResponse.getVersion()); 220 } 221 222 223 /** 224 * get 查询 225 * 通过id查询 226 * @throws Exception 227 */ 228 @Test 229 public void test8() throws Exception { 230 GetResponse getResponse = transportClient.prepareGet(index, type, "4")//查询id为4的数据 231 .get(); 232 System.out.println(getResponse.getSourceAsString()); 233 } 234 235 /** 236 * 局部更新 237 * @throws Exception 238 */ 239 @Test 240 public void test9() throws Exception { 241 XContentBuilder builder = XContentFactory.jsonBuilder()//XContentFactory 这个是ES官方提供的可以构建Json字符串的工具类. 242 .startObject() 243 .field("age", 29) 244 .endObject(); 245 246 UpdateResponse updateResponse = transportClient.prepareUpdate(index, type, "4")//更新id为4的数据 247 .setDoc(builder) 248 .get(); 249 System.out.println(updateResponse.getVersion());//version打印2 数据更新 250 } 251 252 /** 253 * 删除 254 * 通过id删除 255 * @throws Exception 256 */ 257 @Test 258 public void test10() throws Exception { 259 transportClient.prepareDelete(index, type, "4")//删除id为4的数据 260 .get(); 261 } 262 263 /** 264 * count 取总数 类似于sql中的 select count(1) from table; 265 * 求总数 266 * 类似于mysql中的select count(*) 267 */ 268 @Test 269 public void test11() throws Exception { 270 long count = transportClient.prepareCount(index)//查找索引库中的数据个数 271 .setTypes(type) 272 .get() 273 .getCount(); 274 System.out.println(count); 275 } test12测试(批量 bulk操作数据) 1 package zhouls.bigdata.myElasticsearch; 2 3 import static org.junit.Assert.*; 4 5 import java.net.InetAddress; 6 import java.util.HashMap; 7 import java.util.List; 8 9 import org.elasticsearch.action.bulk.BulkItemResponse; 10 import org.elasticsearch.action.bulk.BulkRequestBuilder; 11 import org.elasticsearch.action.bulk.BulkResponse; 12 import org.elasticsearch.action.delete.DeleteRequest; 13 import org.elasticsearch.action.get.GetResponse; 14 import org.elasticsearch.action.index.IndexRequest; 15 import org.elasticsearch.action.index.IndexResponse; 16 import org.elasticsearch.action.search.SearchResponse; 17 import org.elasticsearch.action.search.SearchType; 18 import org.elasticsearch.action.update.UpdateResponse; 19 import org.elasticsearch.client.transport.TransportClient; 20 import org.elasticsearch.cluster.node.DiscoveryNode; 21 import org.elasticsearch.common.settings.Settings; 22 import org.elasticsearch.common.transport.InetSocketTransportAddress; 23 import org.elasticsearch.common.transport.TransportAddress; 24 import org.elasticsearch.common.xcontent.XContentBuilder; 25 import org.elasticsearch.common.xcontent.XContentFactory; 26 import org.elasticsearch.index.query.MatchQueryBuilder.Operator; 27 import org.elasticsearch.index.query.QueryBuilders; 28 import org.elasticsearch.search.SearchHit; 29 import org.elasticsearch.search.SearchHits; 30 import org.elasticsearch.search.aggregations.Aggregation; 31 import org.elasticsearch.search.aggregations.AggregationBuilders; 32 import org.elasticsearch.search.aggregations.bucket.terms.Terms; 33 import org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket; 34 import org.elasticsearch.search.aggregations.metrics.sum.Sum; 35 import org.elasticsearch.search.sort.SortOrder; 36 import org.junit.Before; 37 import org.junit.Test; 38 39 import com.fasterxml.jackson.databind.ObjectMapper; 40 import com.google.common.collect.ImmutableList; 41 42 public class TestEs { 43 44 //es和hadoop没关系啊,获取一个transportclient就可以操作es了 45 46 47 private TransportClient transportClient; 48 @Before//@Before和@Test的区别:每次执行都要先经过@Before,好比是,它是一个模板。 49 //before表示在执行每个test方法之前运行,常与@Test搭配使用 50 public void test0() throws Exception { 51 //获取TransportClient,来操作es 52 transportClient = TransportClient.builder().build(); 53 //需要使用9300端口 54 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.10"), 9300); 55 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 56 transportClient.addTransportAddress(transportAddress); 57 } 58 59 /** 60 * 用java代码测试的时候这样写是没有问题的,比较简单 61 * @throws Exception 62 */ 63 @Test 64 public void test1() throws Exception { 65 //获取TransportClient,来操作es 66 TransportClient transportClient = TransportClient.builder().build(); 67 //需要使用9300端口,指定es集群中的节点信息, 这个地方指定的端口是节点和节点之间的通信端口是9300,不是Http请求的端口9200. 68 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.11"), 9300); 69 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 70 transportClient.addTransportAddress(transportAddress); 71 System.out.println(transportClient.toString()); 72 } 73 74 /** 75 * 可以这样写,防止代码中指定的链接失效 76 * 但是写起来比较麻烦 77 * 在实际工作中这样写不是很靠谱,需要完善,做测试可以 78 * @throws Exception 79 */ 80 @Test 81 public void test2() throws Exception { 82 //获取TransportClient,来操作es,通过TransportClient可以和es集群交互 83 TransportClient transportClient = TransportClient.builder().build(); 84 //需要使用9300端口,指定es集群中的节点信息, 这个地方指定的端口是节点和节点之间的通信端口是9300,不是Http请求的端口9200. 85 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.10"), 9300); 86 TransportAddress transportAddress1 = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.11"), 9300); 87 TransportAddress transportAddress2 = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.12"), 9300); 88 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 89 transportClient.addTransportAddresses(transportAddress,transportAddress1,transportAddress2);//加入多个地址 90 System.out.println(transportClient.toString()); 91 } 92 93 /** 94 * 实际生产环境下面,建议这样用,加上下面这些配置信息 95 * @throws Exception 96 */ 97 @Test 98 public void test3() throws Exception { 99 //指定es的配置信息 100 Settings settings = Settings.settingsBuilder() 101 .put("cluster.name", "elasticsearch")//集群名称 102 //如果集群名称在配置文件中被修改了,那么在这需要显式定义一下 103 //es集群名称默认是 elasticsearch sniff嗅; 发现; 104 .put("client.transport.sniff", true)//开启集群的嗅探功能,只需要指定集群中一个节点信息即可获取到集群中的所有节点信息 105 //开启集群的嗅探功能,这样可以保证es会自动把集群中的其他节点信息添加到transportClient里面 106 //开启嗅探功能后 只要指定集群中的任意一个可用节点就可以了.当把代码运行之后TransportClient里面会把集群中所有节点的信息都拿到,能识别集群中的所有节点. 107 .build(); 108 109 //获取TransportClient,来操作es,//通过TransportClient可以和es集群交互 110 TransportClient transportClient = TransportClient.builder().settings(settings).build(); 111 //需要使用9300端口,指定es集群中的节点信息, 这个地方指定的端口是节点和节点之间的通信端口是9300,不是Http请求的端口9200. 112 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.10"), 9300); 113 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 114 transportClient.addTransportAddress(transportAddress); 115 116 //获取client链接到的节点信息, //获取当前transportClient连接到了集群多少个节点 117 List<DiscoveryNode> connectedNodes = transportClient.connectedNodes(); 118 for (DiscoveryNode discoveryNode : connectedNodes) {//for星型循环,将connectedNodes的值,一一传给DiscoveryNode discoveryNode 119 System.out.println(discoveryNode.getHostName());//打印192.168.80.10;192.168.80.11;192.168.80.12 120 //如果加入transportClient.addTransportAddresses(transportAddress) 只有一个ip,打印的就只有一个. 121 } 122 } 123 124 125 String index = "zhouls";//设置索引库 126 String type = "emp";//设置类型 127 128 //索引index(四种格式:json,map,bean,es helper) 129 130 /** 131 * index-1 json 132 * 实际工作中使用 133 * @throws Exception 134 */ 135 @Test 136 public void test4() throws Exception { 137 String jsonStr = "{\"name\":\"tom zhang\",\"age\":19}";//需要转义下 //向索引库中传入一个String字符串,还可以接受其他类型 138 IndexResponse indexResponse = transportClient.prepareIndex(index, type, "1")//添加一个id=1的数据 139 .setSource(jsonStr)//设值,这是json格式的 140 .get(); 141 //.execute().actionGet(); 这个和上面的get()方法是一样的,get()就是对.execute().actionGet() 进行了封装 142 System.out.println(indexResponse.getVersion()); 143 //得到这个数据的version,如果version=1代表是新添加的数据 144 } 145 146 147 148 /** 149 * index-2 hashmap 150 * 实际工作中使用 151 * @throws Exception 152 */ 153 @Test 154 public void test5() throws Exception {//把hashmap类型的数据放入index库 155 HashMap<String, Object> hashMap = new HashMap<String, Object>(); 156 //HashMap<String, Object> hashMap是迭代器变量 157 hashMap.put("name", "tom"); 158 hashMap.put("age", 15); 159 IndexResponse indexResponse = transportClient.prepareIndex(index, type, "2")//添加一个id=2的数据 160 .setSource(hashMap)//设值 161 .get(); 162 //.execute().actionGet(); 这个和上面的get()方法是一样的,get()就是对.execute().actionGet() 进行了封装 163 System.out.println(indexResponse.getVersion()); 164 } 165 166 167 168 169 /** 170 * index-3 bean 171 * 实际工作中使用 172 * 使用对象的时候需要把对象中的属性转化成json字符串 173 * @throws Exception 174 */ 175 176 // <dependency> 177 // <groupId>com.fasterxml.jackson.core</groupId> 178 // <artifactId>jackson-databind</artifactId> 179 // <version>2.1.3</version> 180 // </dependency> 181 182 183 @Test 184 public void test6() throws Exception {//传入一个对象到index索引库,这里是Person对象 185 Person person = new Person(); 186 person.setName("mack"); 187 person.setAge(20); 188 189 //如果直接传入一个person对象会报错,java.lang.IllegalArgumentException,必须把对象转换成一个Json字符串,使用jackson依赖 190 //IndexResponse indexResponse = transportClient.prepareIndex(index, type, "9").setSource(person).get(); 191 192 193 ObjectMapper objectMapper = new ObjectMapper(); 194 String writeValueAsString = objectMapper.writeValueAsString(person); 195 IndexResponse indexResponse = transportClient.prepareIndex(index, type, "3") 196 .setSource(writeValueAsString) 197 .get(); 198 // IndexResponse indexResponse = transportClient.prepareIndex(index, type, "3").setSource(objectMapper.writeValueAsString(person)).get(); 199 200 System.out.println(indexResponse.getVersion()); 201 } 202 203 /** 204 * index -4 es helper 205 * 测试数据这样使用 206 * @throws Exception 207 */ 208 @Test 209 public void test7() throws Exception { 210 XContentBuilder builder = XContentFactory.jsonBuilder()//XContentFactory 这个是ES官方提供的可以构建Json字符串的工具类. 211 .startObject() 212 .field("name", "jessic") 213 .field("age", 28) 214 .endObject(); 215 216 IndexResponse indexResponse = transportClient.prepareIndex(index, type, "4") 217 .setSource(builder) 218 .get(); 219 System.out.println(indexResponse.getVersion()); 220 } 221 222 223 /** 224 * get 查询 225 * 通过id查询 226 * @throws Exception 227 */ 228 @Test 229 public void test8() throws Exception { 230 GetResponse getResponse = transportClient.prepareGet(index, type, "4")//查询id为4的数据 231 .get(); 232 System.out.println(getResponse.getSourceAsString()); 233 } 234 235 /** 236 * 局部更新 237 * @throws Exception 238 */ 239 @Test 240 public void test9() throws Exception { 241 XContentBuilder builder = XContentFactory.jsonBuilder()//XContentFactory 这个是ES官方提供的可以构建Json字符串的工具类. 242 .startObject() 243 .field("age", 29) 244 .endObject(); 245 246 UpdateResponse updateResponse = transportClient.prepareUpdate(index, type, "4")//更新id为4的数据 247 .setDoc(builder) 248 .get(); 249 System.out.println(updateResponse.getVersion());//version打印2 数据更新 250 } 251 252 /** 253 * 删除 254 * 通过id删除 255 * @throws Exception 256 */ 257 @Test 258 public void test10() throws Exception { 259 transportClient.prepareDelete(index, type, "4")//删除id为4的数据 260 .get(); 261 } 262 263 /** 264 * count 取总数 类似于sql中的 select count(1) from table; 265 * 求总数 266 * 类似于mysql中的select count(*) 267 */ 268 @Test 269 public void test11() throws Exception { 270 long count = transportClient.prepareCount(index)//查找索引库中的数据个数 271 .setTypes(type) 272 .get() 273 .getCount(); 274 System.out.println(count); 275 } 276 277 278 /** 279 * bulk 批量操作 适合初始化数据的时候使用,提高效率 280 * 批量操作 bulk 281 * @throws Exception 282 */ 283 @Test 284 public void test12() throws Exception { 285 BulkRequestBuilder prepareBulk = transportClient.prepareBulk(); 286 287 //for循环执行---- 288 //index请求 289 IndexRequest indexRequest = new IndexRequest(index, type, "10"); 290 indexRequest.source("{\"name\":\"zhangsan\",\"age\":17}"); 291 //delete请求 292 DeleteRequest deleteRequest = new DeleteRequest(index, type, "1"); 293 294 295 prepareBulk.add(indexRequest );//bulkBuilder中可以添加多个操作,这里一个是建立索引的操作. 296 prepareBulk.add(deleteRequest);//一个是删除的操作 297 298 //执行 bulk 299 BulkResponse bulkResponse = prepareBulk.get(); 300 if(bulkResponse.hasFailures()){//批量操作中可能有的操作会出现问题,这个地方对操作失败的处理 301 //有执行失败的 302 BulkItemResponse[] items = bulkResponse.getItems(); 303 for (BulkItemResponse bulkItemResponse : items) { 304 //获取失败信息,并打印 305 System.out.println(bulkItemResponse.getFailureMessage()); 306 } 307 }else{ 308 System.out.println("全部执行成功!"); 309 } 310 } https://www.elastic.co/guide/en/elasticsearch/client/java-api/index.html 我这里,elasticsearch用的是2.4.X版本。 https://www.elastic.co/guide/en/elasticsearch/client/java-api/2.4/java-docs.html 第一步:删除默认的App.java 第二步:选中后,再ctrl + n ,记住,要求自己能用快捷键,就用快捷键! 第三步:输入class,直接回车,再回车 第四步:我这里,以TestEs.java为例 第五步: 第六步:输入代码,这里不多说 通过TransportClient这个类,指定es集群中其中一台或多台机的ip地址和端口 TransportClient client = TransportClient.builder().build().addTransportAddress(new InetSocketTransportAddress (InetAddress.getByName("host1"), 9300)).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host2"), 9300)); 如果需要使用其他名称的集群(默认是elasticsearch),需要如下设置 Settings settings = Settings.settingsBuilder().put("cluster.name", "myClusterName").build(); TransportClientclient = TransportClient.builder().settings(settings).build().addTransportAddress(new InetSocketTransportAddress (InetAddress.getByName("host1"), 9300)); 通过TransportClient这个接口,自动嗅探整个集群的状态,es会自动把集群中其它机器的ip地址加到客户端中 Settings settings = Settings.settingsBuilder().put("client.transport.sniff", true).build(); TransportClient.builder().settings(settings).build().addTransportAddress(new InetSocketTransportAddress (InetAddress.getByName("host1"), 9300)); 更详细,请见 Elasticsearch的Java Client类型 索引index(四种json,map,bean,es helper) IndexResponse response = client.prepareIndex("zhouls", "emp", "1").setSource().get() 查询get GetResponse response = client.prepareGet("zhouls", "emp", "1").get(); 更新update 删除delete DeleteResponse response = client.prepareDelete("zhouls", "emp", "1").execute().actionGet(); 总数count long count = client.prepareCount("zhouls").get().getCount(); 附上代码 前提 准备,开启3台机器组建的es集群进程 test1测试(连接192.168.80.10和192.168.80.11) 1 package zhouls.bigdata.myElasticsearch; 2 3 import static org.junit.Assert.*; 4 5 import java.net.InetAddress; 6 import java.util.HashMap; 7 import java.util.List; 8 9 import org.elasticsearch.action.bulk.BulkItemResponse; 10 import org.elasticsearch.action.bulk.BulkRequestBuilder; 11 import org.elasticsearch.action.bulk.BulkResponse; 12 import org.elasticsearch.action.delete.DeleteRequest; 13 import org.elasticsearch.action.get.GetResponse; 14 import org.elasticsearch.action.index.IndexRequest; 15 import org.elasticsearch.action.index.IndexResponse; 16 import org.elasticsearch.action.search.SearchResponse; 17 import org.elasticsearch.action.search.SearchType; 18 import org.elasticsearch.action.update.UpdateResponse; 19 import org.elasticsearch.client.transport.TransportClient; 20 import org.elasticsearch.cluster.node.DiscoveryNode; 21 import org.elasticsearch.common.settings.Settings; 22 import org.elasticsearch.common.transport.InetSocketTransportAddress; 23 import org.elasticsearch.common.transport.TransportAddress; 24 import org.elasticsearch.common.xcontent.XContentBuilder; 25 import org.elasticsearch.common.xcontent.XContentFactory; 26 import org.elasticsearch.index.query.MatchQueryBuilder.Operator; 27 import org.elasticsearch.index.query.QueryBuilders; 28 import org.elasticsearch.search.SearchHit; 29 import org.elasticsearch.search.SearchHits; 30 import org.elasticsearch.search.aggregations.Aggregation; 31 import org.elasticsearch.search.aggregations.AggregationBuilders; 32 import org.elasticsearch.search.aggregations.bucket.terms.Terms; 33 import org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket; 34 import org.elasticsearch.search.aggregations.metrics.sum.Sum; 35 import org.elasticsearch.search.sort.SortOrder; 36 import org.junit.Before; 37 import org.junit.Test; 38 39 import com.fasterxml.jackson.databind.ObjectMapper; 40 import com.google.common.collect.ImmutableList; 41 42 public class TestEs { 43 44 //es和hadoop没关系啊,获取一个transportclient就可以操作es了 45 46 47 private TransportClient transportClient; 48 @Before//@Before和@Test的区别:每次执行都要先经过@Before,好比是,它是一个模板。 49 //before表示在执行每个test方法之前运行,常与@Test搭配使用 50 public void test0() throws Exception { 51 //获取TransportClient,来操作es 52 transportClient = TransportClient.builder().build(); 53 //需要使用9300端口 54 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.10"), 9300); 55 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 56 transportClient.addTransportAddress(transportAddress); 57 } 58 59 /** 60 * 用java代码测试的时候这样写是没有问题的,比较简单 61 * @throws Exception 62 */ 63 @Test 64 public void test1() throws Exception { 65 //获取TransportClient,来操作es 66 TransportClient transportClient = TransportClient.builder().build(); 67 //需要使用9300端口,指定es集群中的节点信息, 这个地方指定的端口是节点和节点之间的通信端口是9300,不是Http请求的端口9200. 68 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.11"), 9300); 69 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 70 transportClient.addTransportAddress(transportAddress); 71 System.out.println(transportClient.toString()); 72 } test2测试(连接192.168.80.10、192.168.80.11和192.168.80.12)(即es集群) 1 package zhouls.bigdata.myElasticsearch; 2 3 import static org.junit.Assert.*; 4 5 import java.net.InetAddress; 6 import java.util.HashMap; 7 import java.util.List; 8 9 import org.elasticsearch.action.bulk.BulkItemResponse; 10 import org.elasticsearch.action.bulk.BulkRequestBuilder; 11 import org.elasticsearch.action.bulk.BulkResponse; 12 import org.elasticsearch.action.delete.DeleteRequest; 13 import org.elasticsearch.action.get.GetResponse; 14 import org.elasticsearch.action.index.IndexRequest; 15 import org.elasticsearch.action.index.IndexResponse; 16 import org.elasticsearch.action.search.SearchResponse; 17 import org.elasticsearch.action.search.SearchType; 18 import org.elasticsearch.action.update.UpdateResponse; 19 import org.elasticsearch.client.transport.TransportClient; 20 import org.elasticsearch.cluster.node.DiscoveryNode; 21 import org.elasticsearch.common.settings.Settings; 22 import org.elasticsearch.common.transport.InetSocketTransportAddress; 23 import org.elasticsearch.common.transport.TransportAddress; 24 import org.elasticsearch.common.xcontent.XContentBuilder; 25 import org.elasticsearch.common.xcontent.XContentFactory; 26 import org.elasticsearch.index.query.MatchQueryBuilder.Operator; 27 import org.elasticsearch.index.query.QueryBuilders; 28 import org.elasticsearch.search.SearchHit; 29 import org.elasticsearch.search.SearchHits; 30 import org.elasticsearch.search.aggregations.Aggregation; 31 import org.elasticsearch.search.aggregations.AggregationBuilders; 32 import org.elasticsearch.search.aggregations.bucket.terms.Terms; 33 import org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket; 34 import org.elasticsearch.search.aggregations.metrics.sum.Sum; 35 import org.elasticsearch.search.sort.SortOrder; 36 import org.junit.Before; 37 import org.junit.Test; 38 39 import com.fasterxml.jackson.databind.ObjectMapper; 40 import com.google.common.collect.ImmutableList; 41 42 public class TestEs { 43 44 //es和hadoop没关系啊,获取一个transportclient就可以操作es了 45 46 47 private TransportClient transportClient; 48 @Before//@Before和@Test的区别:每次执行都要先经过@Before,好比是,它是一个模板。 49 //before表示在执行每个test方法之前运行,常与@Test搭配使用 50 public void test0() throws Exception { 51 //获取TransportClient,来操作es 52 transportClient = TransportClient.builder().build(); 53 //需要使用9300端口 54 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.10"), 9300); 55 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 56 transportClient.addTransportAddress(transportAddress); 57 } 58 59 /** 60 * 用java代码测试的时候这样写是没有问题的,比较简单 61 * @throws Exception 62 */ 63 @Test 64 public void test1() throws Exception { 65 //获取TransportClient,来操作es 66 TransportClient transportClient = TransportClient.builder().build(); 67 //需要使用9300端口,指定es集群中的节点信息, 这个地方指定的端口是节点和节点之间的通信端口是9300,不是Http请求的端口9200. 68 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.11"), 9300); 69 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 70 transportClient.addTransportAddress(transportAddress); 71 System.out.println(transportClient.toString()); 72 } 73 74 /** 75 * 可以这样写,防止代码中指定的链接失效 76 * 但是写起来比较麻烦 77 * 在实际工作中这样写不是很靠谱,需要完善,做测试可以 78 * @throws Exception 79 */ 80 @Test 81 public void test2() throws Exception { 82 //获取TransportClient,来操作es,通过TransportClient可以和es集群交互 83 TransportClient transportClient = TransportClient.builder().build(); 84 //需要使用9300端口,指定es集群中的节点信息, 这个地方指定的端口是节点和节点之间的通信端口是9300,不是Http请求的端口9200. 85 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.10"), 9300); 86 TransportAddress transportAddress1 = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.11"), 9300); 87 TransportAddress transportAddress2 = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.12"), 9300); 88 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 89 transportClient.addTransportAddresses(transportAddress,transportAddress1,transportAddress2);//加入多个地址 90 System.out.println(transportClient.toString()); 91 } test3测试(连接192.168.80.10、192.168.80.11和192.168.80.12)(生产环境下建议) 1 package zhouls.bigdata.myElasticsearch; 2 3 import static org.junit.Assert.*; 4 5 import java.net.InetAddress; 6 import java.util.HashMap; 7 import java.util.List; 8 9 import org.elasticsearch.action.bulk.BulkItemResponse; 10 import org.elasticsearch.action.bulk.BulkRequestBuilder; 11 import org.elasticsearch.action.bulk.BulkResponse; 12 import org.elasticsearch.action.delete.DeleteRequest; 13 import org.elasticsearch.action.get.GetResponse; 14 import org.elasticsearch.action.index.IndexRequest; 15 import org.elasticsearch.action.index.IndexResponse; 16 import org.elasticsearch.action.search.SearchResponse; 17 import org.elasticsearch.action.search.SearchType; 18 import org.elasticsearch.action.update.UpdateResponse; 19 import org.elasticsearch.client.transport.TransportClient; 20 import org.elasticsearch.cluster.node.DiscoveryNode; 21 import org.elasticsearch.common.settings.Settings; 22 import org.elasticsearch.common.transport.InetSocketTransportAddress; 23 import org.elasticsearch.common.transport.TransportAddress; 24 import org.elasticsearch.common.xcontent.XContentBuilder; 25 import org.elasticsearch.common.xcontent.XContentFactory; 26 import org.elasticsearch.index.query.MatchQueryBuilder.Operator; 27 import org.elasticsearch.index.query.QueryBuilders; 28 import org.elasticsearch.search.SearchHit; 29 import org.elasticsearch.search.SearchHits; 30 import org.elasticsearch.search.aggregations.Aggregation; 31 import org.elasticsearch.search.aggregations.AggregationBuilders; 32 import org.elasticsearch.search.aggregations.bucket.terms.Terms; 33 import org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket; 34 import org.elasticsearch.search.aggregations.metrics.sum.Sum; 35 import org.elasticsearch.search.sort.SortOrder; 36 import org.junit.Before; 37 import org.junit.Test; 38 39 import com.fasterxml.jackson.databind.ObjectMapper; 40 import com.google.common.collect.ImmutableList; 41 42 public class TestEs { 43 44 //es和hadoop没关系啊,获取一个transportclient就可以操作es了 45 46 47 private TransportClient transportClient; 48 @Before//@Before和@Test的区别:每次执行都要先经过@Before,好比是,它是一个模板。 49 //before表示在执行每个test方法之前运行,常与@Test搭配使用 50 public void test0() throws Exception { 51 //获取TransportClient,来操作es 52 transportClient = TransportClient.builder().build(); 53 //需要使用9300端口 54 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.10"), 9300); 55 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 56 transportClient.addTransportAddress(transportAddress); 57 } 58 59 /** 60 * 用java代码测试的时候这样写是没有问题的,比较简单 61 * @throws Exception 62 */ 63 @Test 64 public void test1() throws Exception { 65 //获取TransportClient,来操作es 66 TransportClient transportClient = TransportClient.builder().build(); 67 //需要使用9300端口,指定es集群中的节点信息, 这个地方指定的端口是节点和节点之间的通信端口是9300,不是Http请求的端口9200. 68 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.11"), 9300); 69 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 70 transportClient.addTransportAddress(transportAddress); 71 System.out.println(transportClient.toString()); 72 } 73 74 /** 75 * 可以这样写,防止代码中指定的链接失效 76 * 但是写起来比较麻烦 77 * 在实际工作中这样写不是很靠谱,需要完善,做测试可以 78 * @throws Exception 79 */ 80 @Test 81 public void test2() throws Exception { 82 //获取TransportClient,来操作es,通过TransportClient可以和es集群交互 83 TransportClient transportClient = TransportClient.builder().build(); 84 //需要使用9300端口,指定es集群中的节点信息, 这个地方指定的端口是节点和节点之间的通信端口是9300,不是Http请求的端口9200. 85 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.10"), 9300); 86 TransportAddress transportAddress1 = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.11"), 9300); 87 TransportAddress transportAddress2 = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.12"), 9300); 88 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 89 transportClient.addTransportAddresses(transportAddress,transportAddress1,transportAddress2);//加入多个地址 90 System.out.println(transportClient.toString()); 91 } 92 93 /** 94 * 实际生产环境下面,建议这样用,加上下面这些配置信息 95 * @throws Exception 96 */ 97 @Test 98 public void test3() throws Exception { 99 //指定es的配置信息 100 Settings settings = Settings.settingsBuilder() 101 .put("cluster.name", "elasticsearch")//集群名称 102 //如果集群名称在配置文件中被修改了,那么在这需要显式定义一下 103 //es集群名称默认是 elasticsearch sniff嗅; 发现; 104 .put("client.transport.sniff", true)//开启集群的嗅探功能,只需要指定集群中一个节点信息即可获取到集群中的所有节点信息 105 //开启集群的嗅探功能,这样可以保证es会自动把集群中的其他节点信息添加到transportClient里面 106 //开启嗅探功能后 只要指定集群中的任意一个可用节点就可以了.当把代码运行之后TransportClient里面会把集群中所有节点的信息都拿到,能识别集群中的所有节点. 107 .build(); 108 109 //获取TransportClient,来操作es,//通过TransportClient可以和es集群交互 110 TransportClient transportClient = TransportClient.builder().settings(settings).build(); 111 //需要使用9300端口,指定es集群中的节点信息, 这个地方指定的端口是节点和节点之间的通信端口是9300,不是Http请求的端口9200. 112 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.10"), 9300); 113 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 114 transportClient.addTransportAddress(transportAddress); 115 116 //获取client链接到的节点信息, //获取当前transportClient连接到了集群多少个节点 117 List<DiscoveryNode> connectedNodes = transportClient.connectedNodes(); 118 for (DiscoveryNode discoveryNode : connectedNodes) {//for星型循环,将connectedNodes的值,一一传给DiscoveryNode discoveryNode 119 System.out.println(discoveryNode.getHostName());//打印192.168.80.10;192.168.80.11;192.168.80.12 120 //如果加入transportClient.addTransportAddresses(transportAddress) 只有一个ip,打印的就只有一个. 121 } 122 } test4测试(添加json格式的数据) 1 package zhouls.bigdata.myElasticsearch; 2 3 import static org.junit.Assert.*; 4 5 import java.net.InetAddress; 6 import java.util.HashMap; 7 import java.util.List; 8 9 import org.elasticsearch.action.bulk.BulkItemResponse; 10 import org.elasticsearch.action.bulk.BulkRequestBuilder; 11 import org.elasticsearch.action.bulk.BulkResponse; 12 import org.elasticsearch.action.delete.DeleteRequest; 13 import org.elasticsearch.action.get.GetResponse; 14 import org.elasticsearch.action.index.IndexRequest; 15 import org.elasticsearch.action.index.IndexResponse; 16 import org.elasticsearch.action.search.SearchResponse; 17 import org.elasticsearch.action.search.SearchType; 18 import org.elasticsearch.action.update.UpdateResponse; 19 import org.elasticsearch.client.transport.TransportClient; 20 import org.elasticsearch.cluster.node.DiscoveryNode; 21 import org.elasticsearch.common.settings.Settings; 22 import org.elasticsearch.common.transport.InetSocketTransportAddress; 23 import org.elasticsearch.common.transport.TransportAddress; 24 import org.elasticsearch.common.xcontent.XContentBuilder; 25 import org.elasticsearch.common.xcontent.XContentFactory; 26 import org.elasticsearch.index.query.MatchQueryBuilder.Operator; 27 import org.elasticsearch.index.query.QueryBuilders; 28 import org.elasticsearch.search.SearchHit; 29 import org.elasticsearch.search.SearchHits; 30 import org.elasticsearch.search.aggregations.Aggregation; 31 import org.elasticsearch.search.aggregations.AggregationBuilders; 32 import org.elasticsearch.search.aggregations.bucket.terms.Terms; 33 import org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket; 34 import org.elasticsearch.search.aggregations.metrics.sum.Sum; 35 import org.elasticsearch.search.sort.SortOrder; 36 import org.junit.Before; 37 import org.junit.Test; 38 39 import com.fasterxml.jackson.databind.ObjectMapper; 40 import com.google.common.collect.ImmutableList; 41 42 public class TestEs { 43 44 //es和hadoop没关系啊,获取一个transportclient就可以操作es了 45 46 47 private TransportClient transportClient; 48 @Before//@Before和@Test的区别:每次执行都要先经过@Before,好比是,它是一个模板。 49 //before表示在执行每个test方法之前运行,常与@Test搭配使用 50 public void test0() throws Exception { 51 //获取TransportClient,来操作es 52 transportClient = TransportClient.builder().build(); 53 //需要使用9300端口 54 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.10"), 9300); 55 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 56 transportClient.addTransportAddress(transportAddress); 57 } 58 59 /** 60 * 用java代码测试的时候这样写是没有问题的,比较简单 61 * @throws Exception 62 */ 63 @Test 64 public void test1() throws Exception { 65 //获取TransportClient,来操作es 66 TransportClient transportClient = TransportClient.builder().build(); 67 //需要使用9300端口,指定es集群中的节点信息, 这个地方指定的端口是节点和节点之间的通信端口是9300,不是Http请求的端口9200. 68 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.11"), 9300); 69 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 70 transportClient.addTransportAddress(transportAddress); 71 System.out.println(transportClient.toString()); 72 } 73 74 /** 75 * 可以这样写,防止代码中指定的链接失效 76 * 但是写起来比较麻烦 77 * 在实际工作中这样写不是很靠谱,需要完善,做测试可以 78 * @throws Exception 79 */ 80 @Test 81 public void test2() throws Exception { 82 //获取TransportClient,来操作es,通过TransportClient可以和es集群交互 83 TransportClient transportClient = TransportClient.builder().build(); 84 //需要使用9300端口,指定es集群中的节点信息, 这个地方指定的端口是节点和节点之间的通信端口是9300,不是Http请求的端口9200. 85 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.10"), 9300); 86 TransportAddress transportAddress1 = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.11"), 9300); 87 TransportAddress transportAddress2 = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.12"), 9300); 88 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 89 transportClient.addTransportAddresses(transportAddress,transportAddress1,transportAddress2);//加入多个地址 90 System.out.println(transportClient.toString()); 91 } 92 93 /** 94 * 实际生产环境下面,建议这样用,加上下面这些配置信息 95 * @throws Exception 96 */ 97 @Test 98 public void test3() throws Exception { 99 //指定es的配置信息 100 Settings settings = Settings.settingsBuilder() 101 .put("cluster.name", "elasticsearch")//集群名称 102 //如果集群名称在配置文件中被修改了,那么在这需要显式定义一下 103 //es集群名称默认是 elasticsearch sniff嗅; 发现; 104 .put("client.transport.sniff", true)//开启集群的嗅探功能,只需要指定集群中一个节点信息即可获取到集群中的所有节点信息 105 //开启集群的嗅探功能,这样可以保证es会自动把集群中的其他节点信息添加到transportClient里面 106 //开启嗅探功能后 只要指定集群中的任意一个可用节点就可以了.当把代码运行之后TransportClient里面会把集群中所有节点的信息都拿到,能识别集群中的所有节点. 107 .build(); 108 109 //获取TransportClient,来操作es,//通过TransportClient可以和es集群交互 110 TransportClient transportClient = TransportClient.builder().settings(settings).build(); 111 //需要使用9300端口,指定es集群中的节点信息, 这个地方指定的端口是节点和节点之间的通信端口是9300,不是Http请求的端口9200. 112 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.10"), 9300); 113 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 114 transportClient.addTransportAddress(transportAddress); 115 116 //获取client链接到的节点信息, //获取当前transportClient连接到了集群多少个节点 117 List<DiscoveryNode> connectedNodes = transportClient.connectedNodes(); 118 for (DiscoveryNode discoveryNode : connectedNodes) {//for星型循环,将connectedNodes的值,一一传给DiscoveryNode discoveryNode 119 System.out.println(discoveryNode.getHostName());//打印192.168.80.10;192.168.80.11;192.168.80.12 120 //如果加入transportClient.addTransportAddresses(transportAddress) 只有一个ip,打印的就只有一个. 121 } 122 } 123 124 125 String index = "zhouls";//设置索引库 126 String type = "emp";//设置类型 127 128 //索引index(四种格式:json,map,bean,es helper) 129 130 /** 131 * index-1 json 132 * 实际工作中使用 133 * @throws Exception 134 */ 135 @Test 136 public void test4() throws Exception { 137 String jsonStr = "{\"name\":\"tom zhang\",\"age\":19}";//需要转义下 //向索引库中传入一个String字符串,还可以接受其他类型 138 IndexResponse indexResponse = transportClient.prepareIndex(index, type, "1")//添加一个id=1的数据 139 .setSource(jsonStr)//设值,这是json格式的 140 .get(); 141 //.execute().actionGet(); 这个和上面的get()方法是一样的,get()就是对.execute().actionGet() 进行了封装 142 System.out.println(indexResponse.getVersion()); 143 //得到这个数据的version,如果version=1代表是新添加的数据 144 } 然后,在浏览器里,输入http://192.168.80.10:9200/_plugin/head/ test5测试(添加map格式的数据) 1 package zhouls.bigdata.myElasticsearch; 2 3 import static org.junit.Assert.*; 4 5 import java.net.InetAddress; 6 import java.util.HashMap; 7 import java.util.List; 8 9 import org.elasticsearch.action.bulk.BulkItemResponse; 10 import org.elasticsearch.action.bulk.BulkRequestBuilder; 11 import org.elasticsearch.action.bulk.BulkResponse; 12 import org.elasticsearch.action.delete.DeleteRequest; 13 import org.elasticsearch.action.get.GetResponse; 14 import org.elasticsearch.action.index.IndexRequest; 15 import org.elasticsearch.action.index.IndexResponse; 16 import org.elasticsearch.action.search.SearchResponse; 17 import org.elasticsearch.action.search.SearchType; 18 import org.elasticsearch.action.update.UpdateResponse; 19 import org.elasticsearch.client.transport.TransportClient; 20 import org.elasticsearch.cluster.node.DiscoveryNode; 21 import org.elasticsearch.common.settings.Settings; 22 import org.elasticsearch.common.transport.InetSocketTransportAddress; 23 import org.elasticsearch.common.transport.TransportAddress; 24 import org.elasticsearch.common.xcontent.XContentBuilder; 25 import org.elasticsearch.common.xcontent.XContentFactory; 26 import org.elasticsearch.index.query.MatchQueryBuilder.Operator; 27 import org.elasticsearch.index.query.QueryBuilders; 28 import org.elasticsearch.search.SearchHit; 29 import org.elasticsearch.search.SearchHits; 30 import org.elasticsearch.search.aggregations.Aggregation; 31 import org.elasticsearch.search.aggregations.AggregationBuilders; 32 import org.elasticsearch.search.aggregations.bucket.terms.Terms; 33 import org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket; 34 import org.elasticsearch.search.aggregations.metrics.sum.Sum; 35 import org.elasticsearch.search.sort.SortOrder; 36 import org.junit.Before; 37 import org.junit.Test; 38 39 import com.fasterxml.jackson.databind.ObjectMapper; 40 import com.google.common.collect.ImmutableList; 41 42 public class TestEs { 43 44 //es和hadoop没关系啊,获取一个transportclient就可以操作es了 45 46 47 private TransportClient transportClient; 48 @Before//@Before和@Test的区别:每次执行都要先经过@Before,好比是,它是一个模板。 49 //before表示在执行每个test方法之前运行,常与@Test搭配使用 50 public void test0() throws Exception { 51 //获取TransportClient,来操作es 52 transportClient = TransportClient.builder().build(); 53 //需要使用9300端口 54 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.10"), 9300); 55 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 56 transportClient.addTransportAddress(transportAddress); 57 } 58 59 /** 60 * 用java代码测试的时候这样写是没有问题的,比较简单 61 * @throws Exception 62 */ 63 @Test 64 public void test1() throws Exception { 65 //获取TransportClient,来操作es 66 TransportClient transportClient = TransportClient.builder().build(); 67 //需要使用9300端口,指定es集群中的节点信息, 这个地方指定的端口是节点和节点之间的通信端口是9300,不是Http请求的端口9200. 68 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.11"), 9300); 69 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 70 transportClient.addTransportAddress(transportAddress); 71 System.out.println(transportClient.toString()); 72 } 73 74 /** 75 * 可以这样写,防止代码中指定的链接失效 76 * 但是写起来比较麻烦 77 * 在实际工作中这样写不是很靠谱,需要完善,做测试可以 78 * @throws Exception 79 */ 80 @Test 81 public void test2() throws Exception { 82 //获取TransportClient,来操作es,通过TransportClient可以和es集群交互 83 TransportClient transportClient = TransportClient.builder().build(); 84 //需要使用9300端口,指定es集群中的节点信息, 这个地方指定的端口是节点和节点之间的通信端口是9300,不是Http请求的端口9200. 85 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.10"), 9300); 86 TransportAddress transportAddress1 = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.11"), 9300); 87 TransportAddress transportAddress2 = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.12"), 9300); 88 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 89 transportClient.addTransportAddresses(transportAddress,transportAddress1,transportAddress2);//加入多个地址 90 System.out.println(transportClient.toString()); 91 } 92 93 /** 94 * 实际生产环境下面,建议这样用,加上下面这些配置信息 95 * @throws Exception 96 */ 97 @Test 98 public void test3() throws Exception { 99 //指定es的配置信息 100 Settings settings = Settings.settingsBuilder() 101 .put("cluster.name", "elasticsearch")//集群名称 102 //如果集群名称在配置文件中被修改了,那么在这需要显式定义一下 103 //es集群名称默认是 elasticsearch sniff嗅; 发现; 104 .put("client.transport.sniff", true)//开启集群的嗅探功能,只需要指定集群中一个节点信息即可获取到集群中的所有节点信息 105 //开启集群的嗅探功能,这样可以保证es会自动把集群中的其他节点信息添加到transportClient里面 106 //开启嗅探功能后 只要指定集群中的任意一个可用节点就可以了.当把代码运行之后TransportClient里面会把集群中所有节点的信息都拿到,能识别集群中的所有节点. 107 .build(); 108 109 //获取TransportClient,来操作es,//通过TransportClient可以和es集群交互 110 TransportClient transportClient = TransportClient.builder().settings(settings).build(); 111 //需要使用9300端口,指定es集群中的节点信息, 这个地方指定的端口是节点和节点之间的通信端口是9300,不是Http请求的端口9200. 112 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.10"), 9300); 113 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 114 transportClient.addTransportAddress(transportAddress); 115 116 //获取client链接到的节点信息, //获取当前transportClient连接到了集群多少个节点 117 List<DiscoveryNode> connectedNodes = transportClient.connectedNodes(); 118 for (DiscoveryNode discoveryNode : connectedNodes) {//for星型循环,将connectedNodes的值,一一传给DiscoveryNode discoveryNode 119 System.out.println(discoveryNode.getHostName());//打印192.168.80.10;192.168.80.11;192.168.80.12 120 //如果加入transportClient.addTransportAddresses(transportAddress) 只有一个ip,打印的就只有一个. 121 } 122 } 123 124 125 String index = "zhouls";//设置索引库 126 String type = "emp";//设置类型 127 128 //索引index(四种格式:json,map,bean,es helper) 129 130 /** 131 * index-1 json 132 * 实际工作中使用 133 * @throws Exception 134 */ 135 @Test 136 public void test4() throws Exception { 137 String jsonStr = "{\"name\":\"tom zhang\",\"age\":19}";//需要转义下 //向索引库中传入一个String字符串,还可以接受其他类型 138 IndexResponse indexResponse = transportClient.prepareIndex(index, type, "1")//添加一个id=1的数据 139 .setSource(jsonStr)//设值,这是json格式的 140 .get(); 141 //.execute().actionGet(); 这个和上面的get()方法是一样的,get()就是对.execute().actionGet() 进行了封装 142 System.out.println(indexResponse.getVersion()); 143 //得到这个数据的version,如果version=1代表是新添加的数据 144 } 145 146 147 148 /** 149 * index-2 hashmap 150 * 实际工作中使用 151 * @throws Exception 152 */ 153 @Test 154 public void test5() throws Exception {//把hashmap类型的数据放入index库 155 HashMap<String, Object> hashMap = new HashMap<String, Object>(); 156 //HashMap<String, Object> hashMap是迭代器变量 157 hashMap.put("name", "tom"); 158 hashMap.put("age", 15); 159 IndexResponse indexResponse = transportClient.prepareIndex(index, type, "2")//添加一个id=2的数据 160 .setSource(hashMap)//设值 161 .get(); 162 //.execute().actionGet(); 这个和上面的get()方法是一样的,get()就是对.execute().actionGet() 进行了封装 163 System.out.println(indexResponse.getVersion()); 164 } 165 test6测试(添加bean格式的数据) 需要引入对象,这里新建Person.java实体类 1 package zhouls.bigdata.myElasticsearch; 2 3 4 5 public class Person {//实体类 6 7 private String name; 8 private int age; 9 public String getName() { 10 return name; 11 } 12 public void setName(String name) { 13 this.name = name; 14 } 15 public int getAge() { 16 return age; 17 } 18 public void setAge(int age) { 19 this.age = age; 20 } 21 22 23 24 } 1 package zhouls.bigdata.myElasticsearch; 2 3 import static org.junit.Assert.*; 4 5 import java.net.InetAddress; 6 import java.util.HashMap; 7 import java.util.List; 8 9 import org.elasticsearch.action.bulk.BulkItemResponse; 10 import org.elasticsearch.action.bulk.BulkRequestBuilder; 11 import org.elasticsearch.action.bulk.BulkResponse; 12 import org.elasticsearch.action.delete.DeleteRequest; 13 import org.elasticsearch.action.get.GetResponse; 14 import org.elasticsearch.action.index.IndexRequest; 15 import org.elasticsearch.action.index.IndexResponse; 16 import org.elasticsearch.action.search.SearchResponse; 17 import org.elasticsearch.action.search.SearchType; 18 import org.elasticsearch.action.update.UpdateResponse; 19 import org.elasticsearch.client.transport.TransportClient; 20 import org.elasticsearch.cluster.node.DiscoveryNode; 21 import org.elasticsearch.common.settings.Settings; 22 import org.elasticsearch.common.transport.InetSocketTransportAddress; 23 import org.elasticsearch.common.transport.TransportAddress; 24 import org.elasticsearch.common.xcontent.XContentBuilder; 25 import org.elasticsearch.common.xcontent.XContentFactory; 26 import org.elasticsearch.index.query.MatchQueryBuilder.Operator; 27 import org.elasticsearch.index.query.QueryBuilders; 28 import org.elasticsearch.search.SearchHit; 29 import org.elasticsearch.search.SearchHits; 30 import org.elasticsearch.search.aggregations.Aggregation; 31 import org.elasticsearch.search.aggregations.AggregationBuilders; 32 import org.elasticsearch.search.aggregations.bucket.terms.Terms; 33 import org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket; 34 import org.elasticsearch.search.aggregations.metrics.sum.Sum; 35 import org.elasticsearch.search.sort.SortOrder; 36 import org.junit.Before; 37 import org.junit.Test; 38 39 import com.fasterxml.jackson.databind.ObjectMapper; 40 import com.google.common.collect.ImmutableList; 41 42 public class TestEs { 43 44 //es和hadoop没关系啊,获取一个transportclient就可以操作es了 45 46 47 private TransportClient transportClient; 48 @Before//@Before和@Test的区别:每次执行都要先经过@Before,好比是,它是一个模板。 49 //before表示在执行每个test方法之前运行,常与@Test搭配使用 50 public void test0() throws Exception { 51 //获取TransportClient,来操作es 52 transportClient = TransportClient.builder().build(); 53 //需要使用9300端口 54 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.10"), 9300); 55 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 56 transportClient.addTransportAddress(transportAddress); 57 } 58 59 /** 60 * 用java代码测试的时候这样写是没有问题的,比较简单 61 * @throws Exception 62 */ 63 @Test 64 public void test1() throws Exception { 65 //获取TransportClient,来操作es 66 TransportClient transportClient = TransportClient.builder().build(); 67 //需要使用9300端口,指定es集群中的节点信息, 这个地方指定的端口是节点和节点之间的通信端口是9300,不是Http请求的端口9200. 68 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.11"), 9300); 69 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 70 transportClient.addTransportAddress(transportAddress); 71 System.out.println(transportClient.toString()); 72 } 73 74 /** 75 * 可以这样写,防止代码中指定的链接失效 76 * 但是写起来比较麻烦 77 * 在实际工作中这样写不是很靠谱,需要完善,做测试可以 78 * @throws Exception 79 */ 80 @Test 81 public void test2() throws Exception { 82 //获取TransportClient,来操作es,通过TransportClient可以和es集群交互 83 TransportClient transportClient = TransportClient.builder().build(); 84 //需要使用9300端口,指定es集群中的节点信息, 这个地方指定的端口是节点和节点之间的通信端口是9300,不是Http请求的端口9200. 85 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.10"), 9300); 86 TransportAddress transportAddress1 = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.11"), 9300); 87 TransportAddress transportAddress2 = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.12"), 9300); 88 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 89 transportClient.addTransportAddresses(transportAddress,transportAddress1,transportAddress2);//加入多个地址 90 System.out.println(transportClient.toString()); 91 } 92 93 /** 94 * 实际生产环境下面,建议这样用,加上下面这些配置信息 95 * @throws Exception 96 */ 97 @Test 98 public void test3() throws Exception { 99 //指定es的配置信息 100 Settings settings = Settings.settingsBuilder() 101 .put("cluster.name", "elasticsearch")//集群名称 102 //如果集群名称在配置文件中被修改了,那么在这需要显式定义一下 103 //es集群名称默认是 elasticsearch sniff嗅; 发现; 104 .put("client.transport.sniff", true)//开启集群的嗅探功能,只需要指定集群中一个节点信息即可获取到集群中的所有节点信息 105 //开启集群的嗅探功能,这样可以保证es会自动把集群中的其他节点信息添加到transportClient里面 106 //开启嗅探功能后 只要指定集群中的任意一个可用节点就可以了.当把代码运行之后TransportClient里面会把集群中所有节点的信息都拿到,能识别集群中的所有节点. 107 .build(); 108 109 //获取TransportClient,来操作es,//通过TransportClient可以和es集群交互 110 TransportClient transportClient = TransportClient.builder().settings(settings).build(); 111 //需要使用9300端口,指定es集群中的节点信息, 这个地方指定的端口是节点和节点之间的通信端口是9300,不是Http请求的端口9200. 112 TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("192.168.80.10"), 9300); 113 //添加节点信息,最少指定集群内的某一个节点即可操作这个es集群 114 transportClient.addTransportAddress(transportAddress); 115 116 //获取client链接到的节点信息, //获取当前transportClient连接到了集群多少个节点 117 List<DiscoveryNode> connectedNodes = transportClient.connectedNodes(); 118 for (DiscoveryNode discoveryNode : connectedNodes) {//for星型循环,将connectedNodes的值,一一传给DiscoveryNode discoveryNode 119 System.out.println(discoveryNode.getHostName());//打印192.168.80.10;192.168.80.11;192.168.80.12 120 //如果加入transportClient.addTransportAddresses(transportAddress) 只有一个ip,打印的就只有一个. 121 } 122 } 123 124 125 String index = "zhouls";//设置索引库 126 String type = "emp";//设置类型 127 128 //索引index(四种格式:json,map,bean,es helper) 129 130 /** 131 * index-1 json 132 * 实际工作中使用 133 * @throws Exception 134 */ 135 @Test 136 public void test4() throws Exception { 137 String jsonStr = "{\"name\":\"tom zhang\",\"age\":19}";//需要转义下 //向索引库中传入一个String字符串,还可以接受其他类型 138 IndexResponse indexResponse = transportClient.prepareIndex(index, type, "1")//添加一个id=1的数据 139 .setSource(jsonStr)//设值,这是json格式的 140 .get(); 141 //.execute().actionGet(); 这个和上面的get()方法是一样的,get()就是对.execute().actionGet() 进行了封装 142 System.out.println(indexResponse.getVersion()); 143 //得到这个数据的version,如果version=1代表是新添加的数据 144 } 145 146 147 148 /** 149 * index-2 hashmap 150 * 实际工作中使用 151 * @throws Exception 152 */ 153 @Test 154 public void test5() throws Exception {//把hashmap类型的数据放入index库 155 HashMap<String, Object> hashMap = new HashMap<String, Object>(); 156 //HashMap<String, Object> hashMap是迭代器变量 157 hashMap.put("name", "tom"); 158 hashMap.put("age", 15); 159 IndexResponse indexResponse = transportClient.prepareIndex(index, type, "2")//添加一个id=2的数据 160 .setSource(hashMap)//设值 161 .get(); 162 //.execute().actionGet(); 这个和上面的get()方法是一样的,get()就是对.execute().actionGet() 进行了封装 163 System.out.println(indexResponse.getVersion()); 164 } 165 166 167 168 169 /** 170 * index-3 bean 171 * 实际工作中使用 172 * 使用对象的时候需要把对象中的属性转化成json字符串 173 * @throws Exception 174 */ 175 176 // <dependency> 177 // <groupId>com.fasterxml.jackson.core</groupId> 178 //

资源下载

更多资源
优质分享App

优质分享App

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

用户登录
用户注册
修改密码
忘记密码