PUT test{"settings": {"index": {"number_of_shards": "1","number_of_replicas": "0"}},"mappings": {"doc": { // ES 7.x版本之后创建索引时不需要指定type,会有一个默认且唯一的`_doc` type"properties": {"k1": {"type": "long"},"k2": {"type": "date"},"k3": {"type": "keyword"},"k4": {"type": "text","analyzer": "standard"},"k5": {"type": "float"}}}}}
OST /_bulk{"index":{"_index":"test","_type":"doc"}}{ "k1" : 100, "k2": "2020-01-01", "k3": "Trying out Elasticsearch", "k4": "Trying out Elasticsearch", "k5": 10.0}{"index":{"_index":"test","_type":"doc"}}{ "k1" : 100, "k2": "2020-01-01", "k3": "Trying out Doris", "k4": "Trying out Doris", "k5": 10.0}{"index":{"_index":"test","_type":"doc"}}{ "k1" : 100, "k2": "2020-01-01", "k3": "Doris On ES", "k4": "Doris On ES", "k5": 10.0}{"index":{"_index":"test","_type":"doc"}}{ "k1" : 100, "k2": "2020-01-01", "k3": "Doris", "k4": "Doris", "k5": 10.0}{"index":{"_index":"test","_type":"doc"}}{ "k1" : 100, "k2": "2020-01-01", "k3": "ES", "k4": "ES", "k5": 10.0}
CREATE EXTERNAL TABLE `test` // 不指定schema,自动拉取es mapping进行建表ENGINE=ELASTICSEARCHPROPERTIES ("hosts" = "http://192.168.0.1:8200,http://192.168.0.2:8200","index" = "test","type" = "doc","user" = "root","password" = "root");CREATE EXTERNAL TABLE `test` (`k1` bigint(20) COMMENT "",`k2` datetime COMMENT "",`k3` varchar(20) COMMENT "",`k4` varchar(100) COMMENT "",`k5` float COMMENT "") ENGINE=ELASTICSEARCH // ENGINE必须是ElasticsearchPROPERTIES ("hosts" = "http://192.168.0.1:8200,http://192.168.0.2:8200","index" = "test","type" = "doc","user" = "root","password" = "root");
参数 | 说明 |
hosts | ES 集群地址,可以是一个或多个,也可以是 ES 前端的负载均衡地址 |
index | 对应的 ES 的 index 名字,支持 alias,如果使用 doc_value,需要使用真实的名称 |
type | index 的 type,ES 7.x 及以后的版本不传此参数 |
user | ES 集群用户名 |
password | 对应用户的密码信息 |
Doris On ES
一个重要的功能就是过滤条件的下推:过滤条件下推给 ES,这样只有真正满足条件的数据才会被返回,能够显著的提高查询性能和降低 Doris和 Elasticsearch 的 CPU、memory、IO 使用量。enable_new_es_dsl
代表是否使用新版 DSL 生成逻辑,后续 bug 修复和迭代都在新版 DSL 开发, 默认为true
,可在fe.conf
中进行修改。SQL syntax | ES 5.x+ syntax |
= | term query |
in | terms query |
> , < , >= , ⇐ | range query |
and | bool.filter |
or | bool.should |
not | bool.must_not |
not in | bool.must_not + terms query |
is_not_null | exists query |
is_null | bool.must_not + exists query |
esquery | ES 原生 json 形式的 QueryDSL |
Doris/ES | byte | short | integer | long | float | double | keyword | text | date |
tinyint | ✓ | - | - | - | - | - | - | - | - |
smallint | ✓ | ✓ | - | - | - | - | - | - | - |
int | ✓ | ✓ | ✓ | - | - | - | - | - | - |
bigint | ✓ | ✓ | ✓ | ✓ | - | - | - | - | - |
float | - | - | - | - | ✓ | - | - | - | - |
double | - | - | - | - | - | ✓ | - | - | - |
char | - | - | - | - | - | - | ✓ | ✓ | - |
varchar | - | - | - | - | - | - | ✓ | ✓ | - |
date | - | - | - | - | - | - | - | - | ✓ |
datetime | - | - | - | - | - | - | - | - | ✓ |
CREATE EXTERNAL TABLE `test` (`k1` bigint(20) COMMENT "",`k2` datetime COMMENT "",`k3` varchar(20) COMMENT "",`k4` varchar(100) COMMENT "",`k5` float COMMENT "") ENGINE=ELASTICSEARCHPROPERTIES ("hosts" = "http://192.168.0.1:8200,http://192.168.0.2:8200","index" = "test","user" = "root","password" = "root","enable_docvalue_scan" = "true");
参数 | 说明 |
enable_docvalue_scan | 是否开启通过 ES/Lucene 列式存储获取查询字段的值,默认为 false |
_source
中解析获取。_source
中获取所需的所有列,_source
的存储采用的行式+ json 的形式存储,在批量读取性能上要劣于列式存储,尤其在只需要少数列的情况下尤为明显,只获取少数列的情况下,docvalue 的性能大约是_source
性能的十几倍。text
类型的字段在 ES 中是没有列式存储,因此如果要获取的字段值有text
类型字段会自动降级为从_source
中获取。>= 25
),从docvalue
中获取字段值的性能会和从_source
中获取字段值基本一样。CREATE EXTERNAL TABLE `test` (`k1` bigint(20) COMMENT "",`k2` datetime COMMENT "",`k3` varchar(20) COMMENT "",`k4` varchar(100) COMMENT "",`k5` float COMMENT "") ENGINE=ELASTICSEARCHPROPERTIES ("hosts" = "http://192.168.0.1:8200,http://192.168.0.2:8200","index" = "test","user" = "root","password" = "root","enable_keyword_sniff" = "true");
参数 | 说明 |
enable_keyword_sniff | 是否对 ES 中字符串类型分词类型(text) fields 进行探测,获取额外的未分词(keyword)字段名(multi-fields 机制) |
text
类型的字段又有keyword
类型的字段,这就是 ES 的 multi fields 特性,mapping 如下:"k4": {"type": "text","fields": {"keyword": {"type": "keyword","ignore_above": 256}}}
k4 = "Doris On ES"
"term" : {"k4": "Doris On ES"}
text
,在数据导入的时候就会根据 k4 设置的分词器(如果没有设置,就是 standard 分词器)进行分词处理得到 doris、on、es 三个 Term,如下 ES analyze API 分析:POST /_analyze{"analyzer": "standard","text": "Doris On ES"}
{"tokens": [{"token": "doris","start_offset": 0,"end_offset": 5,"type": "<ALPHANUM>","position": 0},{"token": "on","start_offset": 6,"end_offset": 8,"type": "<ALPHANUM>","position": 1},{"token": "es","start_offset": 9,"end_offset": 11,"type": "<ALPHANUM>","position": 2}]}
"term" : {"k4": "Doris On ES"}
Doris On ES
这个 term 匹配不到词典中的任何 term,不会返回任何结果,而启用enable_keyword_sniff: true
会自动将k4 = "Doris On ES"
转换成k4.keyword = "Doris On ES"
来完全匹配 SQL 语义,转换后的 ES query DSL 为:"term" : {"k4.keyword": "Doris On ES"}
k4.keyword
的类型是keyword
,数据写入 ES 中是一个完整的 term,所以可以匹配。CREATE EXTERNAL TABLE `test` (`k1` bigint(20) COMMENT "",`k2` datetime COMMENT "",`k3` varchar(20) COMMENT "",`k4` varchar(100) COMMENT "",`k5` float COMMENT "") ENGINE=ELASTICSEARCHPROPERTIES ("hosts" = "http://192.168.0.1:8200,http://192.168.0.2:8200","index" = "test","user" = "root","password" = "root","nodes_discovery" = "true");
参数 | 说明 |
nodes_discovery | 是否开启 EST 节点发现,默认为 true |
true
,默认为 false(http_ssl_enabled=true) 。CREATE EXTERNAL TABLE `test` (`k1` bigint(20) COMMENT "",`k2` datetime COMMENT "",`k3` varchar(20) COMMENT "",`k4` varchar(100) COMMENT "",`k5` float COMMENT "") ENGINE=ELASTICSEARCHPROPERTIES ("hosts" = "http://192.168.0.1:8200,http://192.168.0.2:8200","index" = "test","user" = "root","password" = "root","http_ssl_enabled" = "true");
参数 | 说明 |
http_ssl_enabled | ES 集群是否开启 HTTPS 访问模式 |
select * from es_table where k1 > 1000 and k3 ='term' or k4 like 'fu*z_'
esquery(field, QueryDSL)
函数将一些无法用 SQL 表述的 query 如 match_phrase、geoshape 等下推给 ES 进行过滤处理,esquery
的第一个列名参数用于关联index
,第二个参数是ES的基本Query DSL
的 json 表述,使用花括号{}
包含,json 的root key
有且只能有一个,如 match_phrase、geo_shape、bool 等。
match_phrase 查询:select * from es_table where esquery(k4, '{"match_phrase": {"k4": "doris on es"}}');
select * from es_table where esquery(k4, '{"geo_shape": {"location": {"shape": {"type": "envelope","coordinates": [[13,53],[14,52]]},"relation": "within"}}}');
select * from es_table where esquery(k4, ' {"bool": {"must": [{"terms": {"k1": [11,12]}},{"terms": {"k2": [100]}}]}}');
+----------------------------------------------+| || Doris +------------------+ || | FE +--------------+-------+| | | Request Shard Location| +--+-------------+-+ | || ^ ^ | || | | | || +-------------------+ +------------------+ | || | | | | | | | || | +----------+----+ | | +--+-----------+ | | || | | BE | | | | BE | | | || | +---------------+ | | +--------------+ | | |+----------------------------------------------+ || | | | | | || | | | | | || HTTP SCROLL | | HTTP SCROLL | |+-----------+---------------------+------------+ || | v | | v | | || | +------+--------+ | | +------+-------+ | | || | | | | | | | | | || | | DataNode | | | | DataNode +<-----------+| | | | | | | | | | || | | +<--------------------------------+| | +---------------+ | | |--------------| | | || +-------------------+ +------------------+ | || Same Physical Node | || | || +-----------------------+ | || | | | || | MasterNode +<-----------------+| ES | | || +-----------------------+ |+----------------------------------------------+
HTTP Scroll
方式流式的从 ES index 的每个分片中并发的从_source
或 docvalue
中获取数据。"dt": {"type": "date","format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"}
date
或 datetime
,也可以设置为 varchar
类型,使用如下 SQL 语句都可以直接将过滤条件下推至 ES:select * from doe where k2 > '2020-06-21';select * from doe where k2 < '2020-06-21 12:00:00';select * from doe where k2 < 1593497011;select * from doe where k2 < now();select * from doe where k2 < date_format(now(), '%Y-%m-%d');
format
,默认的时间类型字段格式为:strict_date_optional_time||epoch_millis
ms
,ES 内部处理时间戳都是按照ms
进行处理的,否则 Doris On ES 会出现显示错误。_id
_id
的情况下 ES 会给每个文档分配一个全局唯一的_id
即主键, 用户也可以在导入时为文档指定一个含有特殊业务意义的_id
;如果需要在 Doris On ES 中获取该字段值,建表时可以增加类型为varchar
的_id
字段:CREATE EXTERNAL TABLE `doe` (`_id` varchar COMMENT "",`city` varchar COMMENT "") ENGINE=ELASTICSEARCHPROPERTIES ("hosts" = "http://127.0.0.1:8200","user" = "root","password" = "root","index" = "doe"}
_id
字段的过滤条件仅支持 =
和 in
两种。_id
字段只能是 varchar
类型。
本页内容是否解决了您的问题?