您现在的位置是:首页 > 文章详情

Elasticsearch hadoop使用示例 & 运维实战之集群规划 &presto-elasticsearch connecto...

日期:2016-09-15点击:506

elasticsearch-hadoop使用示例

在elasticsearch-hadoop的具体使用中碰到了几个问题,有必要记录一下,避免下次遇到时又要重新研究。

利用spark读取es数据源的简单示例

import org.elasticsearch.spark.sql._ val esOptions = Map("es.nodes"->"192.168.1.2,192.168.1.3", "es.scroll.size"->"1000", "es.field.read.as.array.include"->"SampleField") val esDF = sqlContext.read.format("org.elasticsearch.spark.sql").options(esOptions).load("sampleindex/es-spark") esDF.registerTempTable("esdemotbl")

es.scroll.size 一次性读入的记录数,默认是10, 如果不设置为大一点的值,要从es中读取1亿条数据,那将是一个漫长的过程

es.field.read.as.array.include 有的字段在es中是以string类型存储,但其中包含逗号(,), spark默认认为这是数组类型,如果读取这种字段的话,就会报错,怎么办,那就用es.field.read.as.array.include来显式指明

spark读取es中数据的时候,partition数目取决于es中指定index的shard数目,为了获得比较高的并发读取性能,建议适当设置shard数目,为什么是适当,因为具体取决于集群规模等多种因素。

字段名的大小写问题

在hive中,字段名是_大小写不敏感_的, 但在ES中是大小写敏感的

你说,这又怎么样。 呵呵, 这意味着不做特殊处理,永远无法读出es中大写字段名的内容,你看到的将是满屏的_NULL_

这该怎么破,很简单,指定 es.mapping.names

比如在es中,字段名为DemoField, 要读出其中的内容,hive表的字义就该这样写

create external table es_demo_tbl( demofield string) STORED BY 'org.elasticsearch.hadoop.hive.EsStorageHandler' TBLPROPERTIES('es.nodes'='192.168.1.2,192.168.1.3', 'es.resource'='demoindex/sample', 'es.mapping.names'='demofield:DemoField')

注意是先hive中的字段名,然后是es中的字段名

Elasticsearch 运维实战之集群规划

规划一个可用于生产环境的elasticsearch集群。

集群节点划分

整个集群的节点分为以下三种主要类型

  1. Master nodes -- 负责维护集群状态,不保存index数据, 硬件要求: 一般性的机器就可以,给es进程分配16g内存
  2. Data Nodes -- 只保存index的数据,不被选举为Master nodes 硬件要求: 配置要求越高越好,使用大硬盘,有条件可以上SSD硬盘
  3. Client Nodes -- 主要用于负载均衡,不被选举为Master node, 也不保存index数据 硬件要求: 24核CPU, 64G内存或更高

一个合理的集群应该包含三个master nodes, 1到多个data nodes, 最少一个client node

安装与配置

通用配置,以centos为例,使用rpm安装包

sudo rpm -ivh elasticsearch-version.rpm sudo chkconfig --add elasticsearch

修改/etc/sysconfig/elasticsearch, 修改ES_HEAP_SIZE和JAVA_OPTS的内容,注意elasticsearch建议使用的最大内存是32G,

ES_HEAP_SIZE=32g JAVA_OPTS="-Xms32g"

修改/etc/security/limits.conf, 添加如下内容

* hard memlock unlimited * soft memlock unlimited

/etc/elasticsearch/elasticsearch.yml 内容配置

  • master节点
node.master: true node.data: false discovery.zen.ping.unicast.hosts: ["master1","master2","master3"] network.host: ${HOSTNAME}
  • data节点
node.master: false node.data: true discovery.zen.ping.unicast.hosts: ["master1","master2","master3"] network.host: ${HOSTNAME}

如果为elasticsearch配置了多块硬盘,可以修改 DATA_DIR 的值,多个目录使用逗号(,)分开

  • client节点
node.master: false node.data: false discovery.zen.ping.unicast.hosts: ["master1","master2","master3"] network.host: ${HOSTNAME}

启动elasticsearch

sudo service elasticsearch start

需要注意的是elasticsearch在centos中使用service elasticsearch restart有时不能达到效果,需要分开来做

sudo kill -9 `pgrep -f elasticsearch` sudo service elasticsearch start

nginx反向代理

为了记录针对集群的查询内容,建议使用nginx来做反向代理,nginx安装在client node上,conf.d/default.conf 最简单的配置如下

upstream elasticsearch { server 127.0.0.1:9200; } server { gzip on; access_log /var/log/nginx/access.log combined; listen 80 default_server; server_name _; #charset koi8-r; #access_log logs/host.access.log main; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { root /usr/share/nginx/html; index index.html index.htm; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_pass http://elasticsearch; } error_page 404 /404.html; location = /404.html { root /usr/share/nginx/html; } # redirect server error pages to the static page /50x.html error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } }

插件安装

建议安装如下插件

  • kopf 兼容es 1.x, 2.x

kopf

./elasticsearch/bin/plugin install lmenezes/elasticsearch-kopf/{branch|version}
  • head 兼容es 1.x
  • bigdesk 兼容es 1.x

presto-elasticsearch connector

elasticsearch搜索功能强劲,就是查询语法复杂,presto提供了非常open的plugin机制,我改进了下原有的presto-elasticsearch connector,现发布于github

功能改进点

  • 自动加载elasticsearch schema
  • 支持分片数据加载
  • predication pushdown,注意由于presto spi接口的原因,目前只支持and类型的过滤器下推,对于OR表达式不支持,不支持like类型的过滤器下推
  • 避免重复加载schema

运行办法
在etc/catalog目录下添加es.properties即可,内容如下

connector.name=elasticsearch elasticsearch-server=localhost elasticsearch-port=9300 elasticsearch-clustername=elasticsearch_demo

时间仓促,错误在所难免,欢迎批评指正。

原文链接:https://yq.aliyun.com/articles/60649
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章