注:ES是Java开源项目,预先安装Jre和NodeJS。html
Elasticsearch是基于Apache Lucene的开源搜索引擎,目前被认为是最早进、性能最好、功能最全的搜索引擎。node
分片:集群中节点存放文档的地方,分片保存在不一样节点可用于数据恢复,每一个分片占用的CPU、RAM、IO越高索引速度就越快python
index(索引): 相似数据库,多个索引就表明多个数据库git
type(类型): 相似表名github
mapping :表结构redis
doc(文档):数据,一条Json数据为一个文档数据库
ES Json :ES API请求模板,用于索引数据,格式ES有严格规定(不一样版本有区别)npm
filter(过滤):ES有俩种查询模式,一是根据条件查询(速度慢),二所有查询后再条件过滤bootstrap
aggs(聚合):相似数据库的group by
,可多个聚合嵌套使用服务器
如下为单节点配置:
一、下载 ES压缩包,解压到本地。
二、打开/ES/config/下 elasticsearch.yml
为了显示整洁,去掉了注释和没使用的配置项
# ---------------------------------- Cluster ----------------------------------- cluster.name: elasticsearch #ES根据此名将节点放到集群中 # ------------------------------------ Node ------------------------------------ node.name: node-master #节点名称,集群需更改!!! # ----------------------------------- Paths ------------------------------------ #path.data: /path/to/data #path.logs: /path/to/logs # ----------------------------------- Memory ----------------------------------- #bootstrap.memory_lock: true # ---------------------------------- Network ----------------------------------- network.host: 127.0.0.1 #节点绑定的ip transport.tcp.port: 9301 #集群需更改!!! http.port: 9401 #集群需更改!!! # --------------------------------- Discovery ---------------------------------- #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: 1 #至少1个主节点 # ---------------------------------- Gateway ----------------------------------- #gateway.recover_after_nodes: 3 # ---------------------------------- Various ----------------------------------- #action.destructive_requires_name: true
一、命令行到/ES/bin/下,运行 elasticsearch
或 elasticsearch -d
隐藏运行
二、非隐藏运行可以使用 Ctrl+C
关闭。隐藏模式可以使用 ps -ef | grep elastic
或 jps
查看进程号
三、当集群中的节点出现红色Unassigned,则检查处理问题(节点状态可以使用下面的ES插件进行观察等其它操做)
(1)查看集群相关信息
curl "localhost:9401/_nodes/process?pretty"
(2)找出 UNASSIGNED 相关信息
curl -XGET localhost:9401/_cat/shards|grep UNASSIGNED
(3)依次修改以上UNASSIGNED
curl -XPOST 'localhost:9401/_cluster/reroute' -d '{ "commands" : [ { "allocate" : { "index" : "graylog_83", "shard" : 1, "node" : "Auq82gfGQVWgOBw6S7ajRQ", "allow_primary" : true } }] }'
一、下载开源项目 elasticsearch-head
二、进入到elasticsearch-head下,命令行 npm install grunt-cli
安装grunt客户端
三、在elasticsearch-head下打开Gruntfile.js
四、运行监控插件及结果
{ "student": { "properties": { "no": { "type": "string", "fielddata": true, "index": "analyzed" }, "name": { "type": "string", "index": "analyzed" }, "age": { "type": "integer" }, "birth": { "type": "date", "format": "yyyy-MM-dd" }, "isLeader": { "type": "boolean" } } } }
而后用REST方式调用ES接口建立索引和类型:
ES监控插件上显示:
bulk API 容许在单个步骤中进行屡次 create 、 index 、 update 或 delete 请求。
curl -XPOST "http://172.16.13.4:9401/_bulk?pretty" -d ' {"delete": {"_index": "megacorp", "_type": "employee", "_id": "2"}} {"create": {"_index": "megacorp", "_type": "employee", "_id": "2"}} {"name": "first"} {"index": {"_index": "megacorp", "_type": "employee"}}\n
分析器包括三个功能:字符过滤器(过滤掉HTML,特殊符号转换)、分词器也叫分析器(标准分析器、简单、空格、语言分析器)、token过滤器(删除改变无用词)。具体详见这章 ES分析器。
配置很简单就不作详细说明了,原理跟redis集群差很少,判断节点超时、投票选取主节点。
#####################################主节点1##################################### # ---------------------------------- Cluster ----------------------------------- cluster.name: alex-es # ------------------------------------ Node ------------------------------------ node.name: node1 node.master: true node.data: true # ----------------------------------- Path ------------------------------------ path.data: /path/to/data path.logs: /path/to/logs # ----------------------------------- Memory ----------------------------------- bootstrap.memory_lock: true # ---------------------------------- Network ----------------------------------- network.host: 172.16.13.4 transport.tcp.port: 9301 transport.tcp.compress: true http.port: 9401 http.max_content_length: 100mb http.enabled: true http.cors.enabled: true http.cors.allow-origin: "*" # --------------------------------- Discovery ---------------------------------- discovery.zen.minimum_master_nodes: 2 discovery.zen.ping.unicast.hosts: ["172.16.13.4:9301", "172.16.13.4:9302"] # ---------------------------------- Gateway ----------------------------------- gateway.recover_after_nodes: 3 gateway.recover_after_time: 5m gateway.expected_nodes: 3
#####################################主节点2##################################### # ---------------------------------- Cluster ----------------------------------- cluster.name: alex-es # ------------------------------------ Node ------------------------------------ node.name: node2 node.master: true node.data: true # ----------------------------------- Path ------------------------------------ path.data: /path/to/data2 path.logs: /path/to/logs2 # ----------------------------------- Memory ----------------------------------- bootstrap.memory_lock: true # ---------------------------------- Network ----------------------------------- network.host: 172.16.13.4 transport.tcp.port: 9302 transport.tcp.compress: true http.port: 9402 http.max_content_length: 100mb http.enabled: true http.cors.enabled: true http.cors.allow-origin: "*" # --------------------------------- Discovery ---------------------------------- discovery.zen.minimum_master_nodes: 2 discovery.zen.ping.unicast.hosts: ["172.16.13.4:9301", "172.16.13.4:9302"] # ---------------------------------- Gateway ----------------------------------- gateway.recover_after_nodes: 3 gateway.recover_after_time: 5m gateway.expected_nodes: 3
#####################################子节点###################################### # ---------------------------------- Cluster ----------------------------------- cluster.name: alex-es # ------------------------------------ Node ------------------------------------ node.name: node3 node.master: false node.data: true # ----------------------------------- Path ------------------------------------ path.data: /path/to/data3 path.logs: /path/to/logs3 # ----------------------------------- Memory ----------------------------------- bootstrap.memory_lock: true # ---------------------------------- Network ----------------------------------- network.host: 172.16.13.4 transport.tcp.port: 9303 transport.tcp.compress: true http.port: 9403 http.max_content_length: 100mb http.enabled: true http.cors.enabled: true http.cors.allow-origin: "*" # --------------------------------- Discovery ---------------------------------- discovery.zen.minimum_master_nodes: 2 discovery.zen.ping.unicast.hosts: ["172.16.13.4:9301", "172.16.13.4:9302"] # ---------------------------------- Gateway ----------------------------------- gateway.recover_after_nodes: 3 gateway.recover_after_time: 5m gateway.expected_nodes: 3
以上配置信息不能包含空格,配置好后,所有启动,在ES-head上监控显示:
官方提供了基于Python、Java等语言的客户端,其中实现了对es链接池轮训、查询、索引、批量等操做。
因为最近在用多进程并发查询es的功能,当请求数量在一段时间内增长时,会有多个进程的响应超时的问题。
通过调查,已排查掉如下可能存在的问题:
一、Java GC机制问题(包括并发GC、FullGC、GCone等),由于根据GC的机制不一样,会影响es的性能 二、es队列大小 三、进程池,基本上是同一时间异步调用es查询,因此这个不存在问题 四、CPU内存及es配置优化等
最后在服务器上抓包发现,部分请求要通过必定时间才能传到es上,并且随着请求数量加大,时间间隔有递增趋势,这样问题就定位在es客户端发送请求那。
通过一番研究,多是es客户端所采用的传输协议会致使请求时间延长,最后决定用Python的 pycurl 来代替es客户端,下面是代码,能够本身实现es轮训:
import pycurl import StringIO import random def es_pool(): return ["ip:port", "ip:port"] # curl请求 def curl_req(index='', rtype='', body=''): s = StringIO.StringIO() c = pycurl.Curl() es_hosts = es_pool() host = es_hosts[random.randint(0, len(es_hosts)) % len(es_hosts)] # 根据es池大小随机选择 url = host + '/' + index + '/' + rtype + '/_search' c.setopt(pycurl.URL, url) c.setopt(pycurl.POST, 1) c.setopt(pycurl.POSTFIELDS, body) c.setopt(pycurl.WRITEFUNCTION, s.write) c.perform() c.close() return s.getvalue()