一、ElasticSearch的介绍
1、简介(官网:https://www.elastic.co/)
ElasticSearch是一款基于Luncene的实时分布式搜索和分析引擎。采用java编写,目标是让全文搜索变得简单(应用倒排索引),还可以进行大规模的横向扩展,支持PB级的结构化和非结构化海量数据的处理。(支持json格式文件)
2、ElasticSearch与MySQL对比
MySQL | ElasticSearch |
database(数据库) | index(s索引库) |
table(表) | type(类型) |
row(行) | document(文档) |
column(列) | field(字段) |
3、Rest简介
REST全称Representational State Transfer。是一种软件的架构风格,而不是标准,只是提供了一组设计原则和约束条件。它主要用于客户端和服务器交互类的软件。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。
其实说白了就是类似HTTP的访问,和HTTP非常的相似。
REST操作:
GET:获取对象的当前状态;
PUT:改变对象的状态;
POST:创建对象;
DELETE:删除对象;
HEAD:获取头信息。
资源 |
一组资源的URI,比如: http://example.com/res/ |
单个资源的URI,比如: http://example.com/res/123 |
GET |
列出URI,以及该资源组中每个资源的详细信息(后者可选) |
获取指定的资源的详细信息,格式可以自选一个合适的网络媒体类型(比如:XML、JSON等) |
PUT |
使用给定的一组资源替换当前整组资源 |
替换/创建指定的资源。并将其追加到相应的资源组中。 |
POST |
在本组资源中创建/追加一个新的资源。该操作往往返回新的URL |
把指定的资源当做一个资源组,并在其下创建/追加一个新的元素,使其隶属于当前资源。 |
DELETE |
删除整组资源 |
删除指定的元素 |
4、CURL简介
curl是利用URL语法在命令行方式下工作的开源文件传输工具,使用curl可以简单实现常见的get/post请求。简单的认为是可以在命令行下面访问url的一个工具。
curl:
-X 指定http的请求方法 有HEAD GET POST PUT DELETE
-d 指定要传输的数据
-H 指定http请求头信息
基本命令:
1、创建索引库(相当于创建数据库)
curl -XPUT 'http://qyl01:9200/qyl'
2、创建索引
1)创建index=1的数据(post方式)
curl -H "Content-Type:application/json" -XPOST
'http://qyl01:9200/qyl/product/1' -d
'{"name" : "hadoop", "author" : "Doug Cutting","version":"2.7.6"}'
2)创建index=2的数据(put方式)
curl -H "Content-Type:application/json" -XPUT
'http://qyl01:9200/qyl/product/2' -d
'{"name" : "hive", "author" : "qyl","version":"100.100.100"}'
put和post的区别
1)put是幂等方法,post不是,所以put用于用户数据更新,post用于新增比较合适
2)创建操作可以使用post,也可以使用Put,区别就在于post是作用的一个集合资源之上的,而Put操作是作用在一个具体资源之上的,比如说很多资源使用数据库自增主键作为标识信息,这个时候就需要使用Put了。
3、查询
1)查询qyl索引库下,类型为product的index=1的数据
curl -XGET http://qyl01:9200/qyl/product/1?pretty
2)检索文档中的一部分,显示特定的字段内容
curl -XGET 'http://qyl01:9200/qyl/product/1?_source=name,author&pretty'
3)获取source的数据
curl -XGET 'http://qyl01:9200/qyl/product/1/_source?pretty'
4)查询所有
curl -XGET 'http://qyl01:9200/qyl/product/_search?pretty'
4、更新
1)局部更新 :(如果存在就会替换原来的_source中的数据,_version会加1 )
curl -H "Content-Type:application/json" -XPOS
http://qyl01:9200/qyl/product/1 -d'{"name" : "apache-hadoop"}‘
2)真正的局部更新
curl -H "Content-Type:application/json"
-XPOST http://qyl01:9200/qyl/product/1/_update
-d'{"doc":{"name" : "hadoop", "author" : "qyl","version":"2.7.6"}}'
5、删除
curl -H "Content-Type:application/json" -XDELETE
http://qyl01:9200/qyl/product/3/
6、查看数据
curl -H "Content-Type:application/json" -XGET
'http://qyl01:9200/account/bank/_search?pretty'
二、ElasticSearch的安装
1、流程
前提:必须要安装在普通用户下面,在2.0x之后不支持root安装!
1)解压
tar -zxvf soft/elasticsearch-6.2.0.tar.gz -c app/
2)重命名
mv elasticsearch-6.2.0/ elasticsearch
3)修改配置文件 $ES_HOME/conf/elasticsearch.yml
cluster.name: qyl-1807
node.name: hadoop
node.attr.rack: rack-1805
path.data: /home/qyl/data/elasticsearch
path.logs: /home/qyl/logs/elasticsearch
bootstrap.memory_lock: false
bootstrap.system_call_filter: false
network.host: qyl01
4)启动
bin/elasticsearch -d
2、出现的问题
1)内核版本不行:解决方案,修改内核版本
a.查看内核 : uname -a
b.升级nss : yum -y update nss
c.导入pubic key: sudo rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
d.升级内核版本: sudo rpm -Uvh https://www.elrepo.org/elrepo-release-6-8.el6.elrepo.noarch.rpm
e.编辑grub.conf文件: sudo vim /etc/grub.conf
f.重启虚拟机: reboot
2)启动时,提示内存不够
a. 在limits.conf 添加类似如下内容
sudo vim /etc/security/limits.conf
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096
b.进入/etc/security/limits.d/目录下修改配置文件 90-nproc.conf
c.修改sudo vim /etc/sysctl.conf
添加如下内容: vm.max_map_count=262144
d.生效 : sudo sysctl -p
3.启动集群
1.集群的配置:
修改$ES/config/elasticsearch.yml 文件(每个节点都得改)
# limit.
bootstrap.system_call_filter: false
# Elasticsearch performs poorly when the system is swapping the me
mory.
#
# ---------------------------------- Network ---------------------
--------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
network.host: qyl01
#
# Set a custom port for HTTP:
#
#http.port: 9200
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery --------------------
--------------
#
# Pass an initial list of hosts to perform discovery when new node
is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
node.master: true
node.data: true
discovery.zen.ping.unicast.hosts: ["qyl01", "qyl02", "qyl03"]
#discovery.zen.ping.unicast.hosts: ["host1", "host2"]
#
"elasticsearch.yml" 92L, 2997C 59,1 70%
# on the system and that the owner of the process is allowed to use this
# limit.
bootstrap.system_call_filter: false
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
network.host: qyl01
#
# Set a custom port for HTTP:
#
#http.port: 9200
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when new node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
node.master: true
node.data: true
discovery.zen.ping.unicast.hosts: ["qyl01", "qyl02", "qyl03"]
#discovery.zen.ping.unicast.hosts: ["host1", "host2"]
#
# Prevent the "split brain" by configuring the majority of nodes (total number of master-eligible nodes / 2 + 1):
#
#discovery.zen.minimum_master_nodes:
#
# For more information, consult the zen discovery module documentation.
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
2、启动集群(用elasticSearch-head插件)