Elasticsearch简单介绍

Elasticsearch是大数据家族非常重要的一员, 有着强大的数据分析能力。 下面简要总结下优缺点:

  • 优点:
  • 1,基于lucene,使用倒排索引,有非常强大数据搜索能力.能快速从海量数据中抓取敏感数据
  • 2,默认dsl表达能力非常强大,灵活性非常高
  • 3,分布式,以及无额外依赖
  • ....
  • 缺点:
  • 1, dsl强大但是学习成本以及门槛高(测试和业务方等介入成本高),随意有NLPChina和ex6.x的sql解决方案. 但是支持有限,统计能力柔弱
  • 2, es自身工具杂乱原因 数据迁移到其他平台成本高(官方自己提供了 es on hadoop系列工具包),索引重建等繁琐
  • 3, es6.x sql和 NLPChina-Sql 不支持join和复杂统计等

我们期望有一款能使用sql做即席查询,满足大数据统计的负责性,同时不要丢失es搜索强大和灵活性.

这里我们首先在spark impala presto drill等大数据生态中 流行的即席sql查询系统中 选择了presto

presto 对比其他的优点:

  • 1, facebook开源有强大质量保证,国内有京东等巨头带头建设中文社区
  • 2, 开放的社区,完善的文档,使用java开发, 定制插件(执行器,udf udaf等)非常简单方便
  • 3, 无依赖,无捆绑,安装部署运维简单
  • 4, sql支持非常前面,很适合OLAP统计场景

我们基于presto平台 开发了Elasticsearch的执行器,支持es2.x es5.x es6.x的等版本。github地址: https://github.com/harbby/presto-runners

核心思路是先通过where 尽量把一些搜索过滤条件交给es来做,然后将搜索过滤出的数据 拉取到presto中进行分布式 join group window等复杂操作

join操作 还可以把es拉取的数据与mysql等其他数据数据进行join

下面演示下一些有趣操作:

SQL Usage

SELECT * FROM test1 WHERE age >30 AND city = 'world'

desc test1

Beyond SQL

  • Search select * from test1 where _name = match_query('Lucy')
  • Search match_phrase select * from test1 where _name = match_phrase('Lucy')
  • 下退搜索条件给es
select * from test1 where _dsl = '{"query":{"match":{"city":{"query":"world"}}}}' and _name = match_query('Lucy')

可以进行完全的dsl搜索过滤.做到不牺牲es dsl强大的表达力

  • 迁移数据
create table index1 as select * from hive.db.table1 where ...

从hive迁移数据到es 反获取也可以导出数据到别的数据源

```
	 insert into index1 select * from mysql.db.table1 where ....
```
  • join
select id from es.index1 as tb1  join mysql.db.table1 as tb2 on  tb1.id = tb2.id

下面是一样常见操作支持情况

  • insert into(支持)
  • create table as (支持)
  • create table (支持)
  • drop table (支持)
  • update (不支持)
  • delete (不支持,防止发生->从删库到跑路事件)

安装

  • 1, 先安装presto推荐0.205+
  • 2,下载插件代码编译 https://github.com/harbby/presto-runners
  • 3, 将编译好的presto-elasticsearch6-0.205.zip等文件 解压到presto根目录的plugins中
  • 4, 在presto_home/etc/catalog/ 目录中添加 es6.properties 文件内容如下(注意 同时只能使用一个es版本)
connector.name=elasticsearch6   #使用es6 plugins里面对应放入的es6插件

elasticsearch.cluster.name=ideal-bigdata
elasticsearch.transport.hosts=localhost:9300

5, 启动好presto集群后, 使用presto cli查询

# cli 下载地址
https://repo1.maven.org/maven2/com/facebook/presto/presto-cli/0.205/presto-cli-0.205-executable.jar
# 启动cli
presto-cli --server localhost:16060 --catalog es6 --schema default --debug
# 检查安装情况
show catalogs    #查看es的执行器是否安装成功
show tables        #查看es集群中所有index