引言
上一篇主要讲解的是:传统数据库的索引、Elasticsearch的倒排索引、Elasticsearch与关系型数据库对比。本篇主要讲解的是:elasticsearch的图形化管理插件head。
上一篇中我介绍了,ES是基于http协议的,操作ES时需要使用Rest Api的方式。想管理elasticsearch集群中所有的主机,看看它们的运行状态,集群数量,还得一台一台机器远程连接进去。这个时候,如果有一个图形管理界面,可以集中并很直观管理我们的所有数据和集群,那这个工具太方便了!它来了它来了elasticsearch-head插件。
安装head插件
1、安装Git
[root@hadoop102 ~]# yum -y install git
[root@hadoop102 ~]# git --version
若出现以上版本号,则代表已经安装了git。
2、安装NodeJs
[root@hadoop102 ~]# mkdir -p /usr/local/node
[root@hadoop102 ~]# tar xvf /tmp/node-v12.13.1-linux-x64.tar.xz -C /usr/local/node
[root@hadoop102 ~]# cd /usr/local/node/node-v12.13.1-linux-x64/
[root@hadoop102 node-v12.13.1-linux-x64]# pwd
/usr/local/node/node-v12.13.1-linux-x64
[root@hadoop102 ]# vi /etc/profile #追加如下
#NODE_HOME
export NODE_HOME=/usr/local/node/node-v12.13.1-linux-x64
export PATH=$PATH:$NODE_HOME/bin
[root@hadoop102 ~]# source /etc/profile
[root@hadoop102 ~]# node -v
v12.13.1
[root@hadoop102 ~]# npm -v
6.12.1
[root@hadoop102 ~]# npm install -g cnpm --registry=https://registry.npm.taobao.org
3、从git拉取并配置head
[root@hadoop102 ~]# cd /usr/local/elasticsearch/
[root@hadoop102 ]# mkdir plugins
[root@hadoop102 ]# cd plugins/
[root@hadoop102 plugins]# git clone git://github.com/mobz/elasticsearch-head.git
[root@hadoop102 plugins]# cd elasticsearch-head/
[root@hadoop102 elasticsearch-head]# vi Gruntfile.js
在黄框位置的第1行,增加1个选项,可以远程访问
4、开启head
head它是前端项目,运行在node上,运行前要安装依赖
[root@hadoop102 elasticsearch-head]# cnpm install
方式1、前台启动head
[root@hadoop102 elasticsearch-head]# cnpm run start
方式2、后台启动head
[es@hadoop102]$ cd /usr/local/elasticsearch/plugins/elasticsearch-head
[es@hadoop102]$ nohup cnpm run start &
5、关闭head
需要操作的时候,再关闭head插件
[es@hadoop102 elasticsearch-head]$ ps -ef|grep "cnpm run start"
[es@hadoop102]$ kill 上一步查询出的进程号
6、浏览访问head
http://192.168.8.102:9100/
连接成功后,如下图
集群的状态说明
green:所有主要分片和复制分片都可用
yellow:所有主要分片可用,但不是所有复制分片都可用。高可用不牢靠。但不会丢失数据
red:不是所有的主要分片都可用。数据有极大风险。数据不能用
7、修改head默认的连接
默认head插件连接的IP是localhost,可认配置一下,让它自动连接指定IP
[root@hadoop102 ~]# cd /usr/local/elasticsearch/plugins/elasticsearch-head/_site/
[root@hadoop102 _site]# vi app.js
#找到如下内容#
this.base_uri = this.config.base_uri || this.prefs.get("app-base_uri") || "http://localhost:9200";
这行中的localhost改成你elasticsearch服务所在IP地址(如果安装head和es安装在同一台主机,就不需要修改了)
使用head操作es
1、创建索引
总分片数=(副本数+1)*分片数,所有分片会平均分配到各节点上。
成功创建了一个新的索引,它的名字:index
以前没有分片的话,数据是直接保存在各节点上的。我如论是存还是取,都要去节点上操作。现在有了分片,把数据保存在分片上的,分片保存在节点上。
我现在有10个分片,保存了2倍的数据。在上图中框架加粗的就是主分片,其它的是副本分片。
当前所有的分片全是完整的,没有任何损坏,集群状态就是green。
假设某个节点上的分片数据有损坏了,现在要查询的数据保存在5个分片中。但是我通过node-1的1\3\4分片和node-2的0\2分片,能找到完整的数据,集群状态就是yellow。
假设某个节点上的分片数据有损坏了,现在要查询的数据保存在5个分片中。但是我所了所有节点上的分片,还是找不到完整的数据,集群状态就是red。
2、通过浏览器查看
http://192.168.8.102:9200/_cluster/health?pretty
3、创建document
http://192.168.8.102:9200/index/_create/1/
{
"name": "zhangsan",
"age": "18"
}
4、根据id值查询document
5、查询所有的document
Rest Api查询方式:http://192.168.8.102:9200/index/_doc/_search
6、更新document中id=2
7、更新document的id=1
8、再次查询所有的document
9、不指定document的id增加数据
10、删除document的id=1的
再次查询document的id=1的
11、基本查询(指定索引、条件)
扩展之head史上最简单的安装方式
1、解压elasticsearch-head-chrome-plugin.rar包
2、操作谷歌浏览器
点击 设置按钮
打开开发者模式,并加载已解压的扩展程序
找到你本地的解压目录
可以看到head插件已经集成了
点击 扩展程序 按钮,找到Head插件
写上elasticsearch所在服务器的IP,点击连接按钮
成功连接到elasticsearch
结束语
至此,Elasticsearch掰开揉碎系列的第4篇就结束了,本篇文章中主要要讲解的是:安装elasticsearch的图形化管理插件head、使用head操作elasticsearch、扩展之head史上最简单的安装方式。
下一篇我带来的是:专为Elasticsearch设计的开源分析和可视化平台Kibana。后续的内容更精彩,敬请期待,感谢兄弟们的关注!!