【ElasticSearch篇】--ElasticSearch從初識到安裝和應用
一、前述
ElasticSearch是一個基於Lucene的搜索服務器。它提供了一個分布式多用戶能力的全文搜索引擎,基於RESTful web接口,在企業中全文搜索時,特別常用。
二、常用概念
cluster代表一個集群,集群中有多個節點,其中有一個為主節點,這個主節點是可以通過選舉產生的,主從節點是對於集群內部來說的。es的一個概念就是去中心化,字面上理解就是無中心節點,這是對於集群外部來說的,因為從外部來看es集群,在邏輯上是個整體,你與任何一個節點的通信和與整個es集群通信是等價的。只需要在同一個網段之內啟動多個es節點,就可以自動組成一個集群。默認情況下es會自動發現同一網段內的節點,自動組成集群。
shards
代表索引分片,es可以把一個完整的索引分成多個分片,這樣的好處是可以把一個大的索引拆分成多個,分布到不同的節點上。構成分布式搜索。分片的數量只能在索引創建前指定,並且索引創建後不能更改。
replicas
代表索引副本,es可以設置多個索引的副本,副本的作用一是提高系統的容錯性,當某個節點某個分片損壞或丟失時可以從副本中恢復。二是提高es的查詢效率,es會自動對搜索請求進行負載均衡。
recovery
代表數據恢復或叫數據重新分布,es在有節點加入或退出時會根據機器的負載對索引分片進行重新分配,掛掉的節點重新啟動時也會進行數據恢復。
river
代表es的一個數據源,也是其它存儲方式(如:數據庫)同步數據到es的一個方法。它是以插件方式存在的一個es服務,通過讀取river中的數據並把它索引到es中,官方的river有couchDB的,RabbitMQ的,Twitter的,Wikipedia的。
gateway
代表es索引快照的存儲方式,es默認是先把索引存放到內存中,當內存滿了時再持久化到本地硬盤。gateway對索引快照進行存儲,當這個es集群關閉再重新啟動時就會從gateway中讀取索引備份數據。es支持多種類型的gateway,有本地文件系統(默認),分布式文件系統,Hadoop的HDFS和amazon的s3雲存儲服務。
如果需要將數據落地到hadoop的hdfs需要先安裝插件elasticsearch/elasticsearch-hadoop,然後再elasticsearch.yml配置
gateway: type: hdfsgateway: hdfs: uri: hdfs://localhost:9000
discovery.zen
代表es的自動發現節點機制,es是一個基於p2p的系統,它先通過廣播尋找存在的節點,再通過多播協議來進行節點之間的通信,同時也支持點對點的交互。
Transport
代表es內部節點或集群與客戶端的交互方式,默認內部是使用tcp協議進行交互,同時它支持http協議(json格式)、thrift、servlet、memcached、zeroMQ等的傳輸協議(通過插件方式集成)。
索引(index)
一個索引就是一個擁有幾分相似特征的文檔的集合。比如說,你可以有一個客戶數據的索引,另一個產品目錄的索引,還有一個訂單數據的索引。一個索引由一個名字來標識(必須全部是小寫字母的),並且當我們要對對應於這個索引中的文檔進行索引、搜索、更新和刪除的時候,都要使用到這個名字。在一個集群中,如果你想,可以定義任意多的索引。
類型(type)
在一個索引中,你可以定義一種或多種類型。一個類型是你的索引的一個邏輯上的分類/分區,其語義完全由你來定。通常,會為具有一組共同字段的文檔定義一個類型。比如說,我們假設你運營一個博客平臺並且將你所有的數據存儲到一個索引中。在這個索引中,你可以為用戶數據定義一個類型,為博客數據定義另一個類型,當然,也可以為評論數據定義另一個類型。
文檔(document)
一個文檔是一個可被索引的基礎信息單元。比如,你可以擁有某一個客戶的文檔,某一個產品的一個文檔,當然,也可以擁有某個訂單的一個文檔。文檔以JSON(Javascript Object Notation)格式來表示,而JSON是一個到處存在的互聯網數據交互格式。 在一個index/type裏面,只要你想,你可以存儲任意多的文檔。註意,盡管一個文檔,物理上存在於一個索引之中,文檔必須被索引/賦予一個索引的type。
三、安裝教程
1、前提
只允許普通用戶操作,不允許root用戶
註意:因為elasticsearch有遠程執行腳本的功能所以容易中木馬病毒,所以不允許用root用戶啟動,root用戶是起不來的,賦權限,用一般的用戶啟動
要配置network.host才能別的機器或者網卡訪問,否則只能是127.0.0.1或者localhost訪問,這裏配置成自己的局域網ip
註意配置yml結尾的配置文件都需要冒號後面加空格才行
2、先要安裝一個解壓工具:
yum install unzip
3、不同節點間創建相同目錄和用戶
下載elasticsearch-2.0.1.tar.zip,不要用root解壓,需要切換用戶
root共同創建 es 目錄(共享模式):
mkdir /opt/soft/es
3個節點同時添加(共享命令模式)用戶並提供密碼:
useradd sxt
echo sxt | passwd --stdin sxt
改變目錄權限(共享命令模式):
chown sxt:sxt es
4、解壓並配置
切換用戶為sxt
註意配置yml結尾的配置文件都需要冒號後面加空格才行
使用sxt這個用戶解壓並進入es 目錄的config配置目錄修改配置文件config/elasticsearch.yml:註意:這個文件的所有行的:的後面,一定要有一個空格!
如果要配置集群需要兩個節點上的elasticsearch配置的cluster.name相同,都啟動可以自動組成集群,這裏如果不改cluster.name則默認是cluster.name=elasticsearch,
nodename隨意取但是集群內的各節點不能相同。
主機和端口
添加防腦裂配置: 如果不配不知道具體數量,不好控制腦裂
discovery.zen.ping.multicast.enabled: false
discovery.zen.ping.unicast.hosts: ["192.168.133.101","192.168.133.102", "192.168.133.103"]
discovery.zen.ping_timeout: 120s
client.transport.ping_timeout: 60s
完整配置如下:
# ======================== Elasticsearch Configuration ========================= # # NOTE: Elasticsearch comes with reasonable defaults for most settings. # Before you set out to tweak and tune the configuration, make sure you # understand what are you trying to accomplish and the consequences. # # The primary way of configuring a node is via this file. This template lists # the most important settings you may want to configure for a production cluster. # # Please see the documentation for further information on configuration options: # <http://www.elastic.co/guide/en/elasticsearch/reference/current/setup-configuration.html> # # ---------------------------------- Cluster ----------------------------------- # # Use a descriptive name for your cluster: # cluster.name: CKL_elasticsearch # # ------------------------------------ Node ------------------------------------ # # Use a descriptive name for the node: # node.name: node01 # # Add custom attributes to the node: # # node.rack: r1 # # ----------------------------------- Paths ------------------------------------ # # Path to directory where to store the data (separate multiple locations by comma): # # path.data: /path/to/data # # Path to log files: # # path.logs: /path/to/logs # # ----------------------------------- Memory ----------------------------------- # # Lock the memory on startup: # # bootstrap.mlockall: true # # Make sure that the `ES_HEAP_SIZE` environment variable is set to about half the memory # available on the system and that the owner of the process is allowed to use this limit. # # Elasticsearch performs poorly when the system is swapping the memory. # # ---------------------------------- Network ----------------------------------- # # Set the bind address to a specific IP (IPv4 or IPv6): # # network.host: 192.168.0.1 # # Set a custom port for HTTP: # http.port: 9200 # # For more information, see the documentation at: # <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-network.html> # # --------------------------------- 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]"] # # discovery.zen.ping.unicast.hosts: ["host1", "host2"] # # Prevent the "split brain" by configuring the majority of nodes (total number of nodes / 2 + 1): # # discovery.zen.minimum_master_nodes: 3 # # For more information, see the documentation at: # <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-discovery.html> # # ---------------------------------- Gateway ----------------------------------- # # Block initial recovery after a full cluster restart until N nodes are started: # # gateway.recover_after_nodes: 3 # # For more information, see the documentation at: # <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-gateway.html> # # ---------------------------------- Various ----------------------------------- # # Disable starting multiple nodes on a single system: # # node.max_local_storage_nodes: 1 # # Require explicit names when deleting indices: # # action.destructive_requires_name: true discovery.zen.ping.multicast.enabled: false discovery.zen.ping.unicast.hosts: ["192.168.31.101","192.168.133.102", "192.168.133.103"] discovery.zen.ping_timeout: 120s client.transport.ping_timeout: 60s
5、分發節點並更改不同節點的es配置中namenode名稱
把配置和文件都分發給別的節點,註意也必須以普通用戶身份:
如果你是用root分發,那邊會以root身份寫入磁盤系統。
以sxt用戶身份登錄
如果出現如下問題,是因為對方節點目錄不是sxt用戶 用chown sxt:sxt es更改即可
6、安裝插件
1、es 是rest方式,采用Jason格式,可讀性不強,添加head插件,以圖表方式顯示:
es目錄原來沒有plugins,從互聯網下載:
bin/plugin install mobz/elasticsearch-head
註意:從解壓es到操作這個包都必須是普通用戶,因為這個過程會創建plugins目錄,如果是你root創建,這個就成了root用戶權限控制了,會有問題
下載後:
2、以上沒有成功則使用第二種方式安裝
wget https://github.com/mobz/elasticsearch-head/archive/master.zip
unzip master.zip
mv elasticsearch-head-master /opt/soft/es/elasticsearch-2.2.1/plugins下面
然後將整個目錄移動到Es的plugins文件下
測試插件效果
http://ip:9200/_plugin/head
http://192.168.31.101:9200/_plugin/head?pretty
【ElasticSearch篇】--ElasticSearch從初識到安裝和應用