1. 程式人生 > >ElasticSearch的介紹及安裝

ElasticSearch的介紹及安裝

一、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外掛)