ES不香嗎,為啥還要ClickHouse?
點(diǎn)擊關(guān)注公眾號(hào),Java干貨及時(shí)送達(dá)
作者:Gang Tao 編輯:陶家龍
來(lái)源:zhuanlan.zhihu.com/p/353296392
Elasticsearch是一個(gè)實(shí)時(shí)的分布式搜索分析引擎,它的底層是構(gòu)建在 Lucene 之上的。簡(jiǎn)單來(lái)說(shuō)是通過(guò)擴(kuò)展 Lucene 的搜索能力,使其具有分布式的功能。
架構(gòu)和設(shè)計(jì)的對(duì)比


Client Node,負(fù)責(zé) API 和數(shù)據(jù)的訪問(wèn)的節(jié)點(diǎn),不存儲(chǔ)/處理數(shù)據(jù)。
Data Node,負(fù)責(zé)數(shù)據(jù)的存儲(chǔ)和索引。
Master Node,管理節(jié)點(diǎn),負(fù)責(zé) Cluster 中的節(jié)點(diǎn)的協(xié)調(diào),不存儲(chǔ)數(shù)據(jù)。

查詢對(duì)比實(shí)戰(zhàn)
https://github.com/gangtao/esvsch

version:?'3.7'
services:
??elasticsearch:
????image:?docker.elastic.co/elasticsearch/elasticsearch:7.4.0
????container_name:?elasticsearch
????environment:
??????-?xpack.security.enabled=false
??????-?discovery.type=single-node
????ulimits:
??????memlock:
????????soft:?-1
????????hard:?-1
??????nofile:
????????soft:?65536
????????hard:?65536
????cap_add:
??????-?IPC_LOCK
????volumes:
??????-?elasticsearch-data:/usr/share/elasticsearch/data
????ports:
??????-?9200:9200
??????-?9300:9300
????deploy:
??????resources:
????????limits:
??????????cpus:?'4'
??????????memory:?4096M
????????reservations:
??????????memory:?4096M
??kibana:
????container_name:?kibana
????image:?docker.elastic.co/kibana/kibana:7.4.0
????environment:
??????-?ELASTICSEARCH_HOSTS=http://elasticsearch:9200
????ports:
??????-?5601:5601
????depends_on:
??????-?elasticsearch
volumes:
??elasticsearch-data:
????driver:?local
version:?"3.7"
services:
??clickhouse:
????container_name:?clickhouse
????image:?yandex/clickhouse-server
????volumes:
??????-?./data/config:/var/lib/clickhouse
????ports:
??????-?"8123:8123"
??????-?"9000:9000"
??????-?"9009:9009"
??????-?"9004:9004"
????ulimits:
??????nproc:?65535
??????nofile:
????????soft:?262144
????????hard:?262144
????healthcheck:
??????test:?["CMD",?"wget",?"--spider",?"-q",?"localhost:8123/ping"]
??????interval:?30s
??????timeout:?5s
??????retries:?3
????deploy:
??????resources:
????????limits:
??????????cpus:?'4'
??????????memory:?4096M
????????reservations:
??????????memory:?4096M
??tabixui:
????container_name:?tabixui
????image:?spoonest/clickhouse-tabix-web-client
????environment:
??????-?CH_NAME=dev
??????-?CH_HOST=127.0.0.1:8123
??????-?CH_LOGIN=default
????ports:
??????-?"18080:80"
????depends_on:
??????-?clickhouse
????deploy:
??????resources:
????????limits:
??????????cpus:?'0.1'
??????????memory:?128M
????????reservations:
??????????memory:?128M
CREATE?TABLE?default.syslog(
????application?String,
????hostname?String,
????message?String,
????mid?String,
????pid?String,
????priority?Int16,
????raw?String,
????timestamp?DateTime('UTC'),
????version?Int16
)?ENGINE?=?MergeTree()
????PARTITION?BY?toYYYYMMDD(timestamp)
????ORDER?BY?timestamp
????TTL?timestamp?+?toIntervalMonth(1);
[sources.in]
??type?=?"generator"
??format?=?"syslog"
??interval?=?0.01
??count?=?100000
[transforms.clone_message]
??type?=?"add_fields"
??inputs?=?["in"]
??fields.raw?=?"{{?message?}}"
[transforms.parser]
??#?General
??type?=?"regex_parser"
??inputs?=?["clone_message"]
??field?=?"message"?#?optional,?default
??patterns?=?['^<(?P\d*)>(?P \d)?(?P \d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z)?(?P \w+\.\w+)?(?P \w+)?(?P \d+)?(?P ID\d+)?-?(?P .*)$']
[transforms.coercer]
??type?=?"coercer"
??inputs?=?["parser"]
??types.timestamp?=?"timestamp"
??types.version?=?"int"
??types.priority?=?"int"
[sinks.out_console]
??#?General
??type?=?"console"
??inputs?=?["coercer"]?
??target?=?"stdout"?
??#?Encoding
??encoding.codec?=?"json"?
[sinks.out_clickhouse]
??host?=?"http://host.docker.internal:8123"
??inputs?=?["coercer"]
??table?=?"syslog"
??type?=?"clickhouse"
??encoding.only_fields?=?["application",?"hostname",?"message",?"mid",?"pid",?"priority",?"raw",?"timestamp",?"version"]
??encoding.timestamp_format?=?"unix"
[sinks.out_es]
??#?General
??type?=?"elasticsearch"
??inputs?=?["coercer"]
??compression?=?"none"?
??endpoint?=?"http://host.docker.internal:9200"?
??index?=?"syslog-%F"
??#?Encoding
??#?Healthcheck
??healthcheck.enabled?=?true
source.in:生成 syslog 的模擬數(shù)據(jù),生成 10w 條,生成間隔和 0.01 秒。
transforms.clone_message:把原始消息復(fù)制一份,這樣抽取的信息同時(shí)可以保留原始消息。
transforms.parser:使用正則表達(dá)式,按照 syslog 的定義,抽取出 application,hostname,message,mid,pid,priority,timestamp,version 這幾個(gè)字段。
transforms.coercer:數(shù)據(jù)類型轉(zhuǎn)化。
sinks.out_console:把生成的數(shù)據(jù)打印到控制臺(tái),供開(kāi)發(fā)調(diào)試。
sinks.out_clickhouse:把生成的數(shù)據(jù)發(fā)送到Clickhouse。
sinks.out_es:把生成的數(shù)據(jù)發(fā)送到 ES。
運(yùn)行 Docker 命令,執(zhí)行該流水線:
docker?run?\
????????-v?$(mkfile_path)/vector.toml:/etc/vector/vector.toml:ro?\
????????-p?18383:8383?\
????????timberio/vector:nightly-alpine
#?ES
{
??"query":{
????"match_all":{}
??}
}
#?Clickhouse?
"SELECT?*?FROM?syslog"
#?ES
{
??"query":{
????"match":{
??????"hostname":"for.org"
????}
??}
}
#?Clickhouse?
"SELECT?*?FROM?syslog?WHERE?hostname='for.org'"
#?ES
{
??"query":{
????"multi_match":{
??????"query":"up.com?ahmadajmi",
????????"fields":[
??????????"hostname",
??????????"application"
????????]
????}
??}
}
#?Clickhouse、
"SELECT?*?FROM?syslog?WHERE?hostname='for.org'?OR?application='ahmadajmi'"
#?ES
{
??"query":{
????"term":{
??????"message":"pretty"
????}
??}
}
#?Clickhouse
"SELECT?*?FROM?syslog?WHERE?lowerUTF8(raw)?LIKE?'%pretty%'"
#?ES
{
??"query":{
????"range":{
??????"version":{
????????"gte":2
??????}
????}
??}
}
#?Clickhouse
"SELECT?*?FROM?syslog?WHERE?version?>=?2"
#?ES
{
??"query":{
????"exists":{
??????"field":"application"
????}
??}
}
#?Clickhouse
"SELECT?*?FROM?syslog?WHERE?application?is?not?NULL"
#?ES
{
??"query":{
????"regexp":{
??????"hostname":{
????????"value":"up.*",
??????????"flags":"ALL",
????????????"max_determinized_states":10000,
??????????????"rewrite":"constant_score"
??????}
????}
??}
}
#?Clickhouse
"SELECT?*?FROM?syslog?WHERE?match(hostname,?'up.*')"
#?ES
{
??"aggs":{
????"version_count":{
??????"value_count":{
????????"field":"version"
??????}
????}
??}
}
#?Clickhouse
"SELECT?count(version)?FROM?syslog"
#?ES
{
??"aggs":{
????"my-agg-name":{
??????"cardinality":{
????????"field":"priority"
??????}
????}
??}
}
#?Clickhouse
"SELECT?count(distinct(priority))?FROM?syslog?"


總結(jié)
往 期 推 薦
1、Log4j2維護(hù)者吐槽沒(méi)工資還要挨罵,GO安全負(fù)責(zé)人建議開(kāi)源作者向公司收費(fèi)
3、2021年程序員們都在用的神級(jí)數(shù)據(jù)庫(kù)
4、Windows重要功能被閹割,全球用戶怒噴數(shù)月后微軟終于悔改
點(diǎn)分享
點(diǎn)收藏
點(diǎn)點(diǎn)贊
點(diǎn)在看






