@rickyChen
2017-12-27T08:06:37.000000Z
字数 3602
阅读 5425
Hangout ClickHouse
当我们部门发现了ClickHouse这么一个优秀数据存储仓库后,经过了一段时间的摸索测试,就把线上大部分与数据分析相关的业务迁移到了ClickHouse上。这篇文章将会介绍我们如何通过Kafka接入Nginx日志到ClickHouse中。当然,其他的应用日志也可以参照以下逻辑将数据接入ClickHouse。
我们最初使用Python脚本清洗日志写入ClickHouse,但是这样开发和维护都有一定的成本。后来我们使用Hangout作为我们的数据清洗工具,Hangout是一个通用的日志分析工具,功能类同Logstash,可以把不同种类的日志处理后写入其他的地方,比如Kafka、Elasticsearch、ClickHouse。
我们假设Nginx日志已经推送到了Kafka。
Hangout已经提供了大量的插件支持我们的日志处理,下面是为了完成一个完整的配置需要另外下载的插件:
下面是我们安装Hangout以及Hangout-output-clickhouse插件的具体步骤:
mkdir hangoutcd hangoutwget https://github.com/childe/hangout/releases/download/0.3.0/hangout-dist-0.3.0-release-bin.zipunzip hangout-dist-0.3.0-release-bin.zipcd moduleswget https://github.com/RickyHuo/hangout-output-clickhouse/releases/download/0.0.2/hangout-output-plugins-clickhouse-0.0.2-jar-with-dependencies.jar
001.cms.msina..sinanode.com`[27/Dec/2017:16:01:03 +0800]`-`"GET /n/front/w636h3606893220.jpg/w720q75apl.webp HTTP/1.1"`"SinaNews/201706071542.1 CFNetwork/758.1.6 Darwin/15.0.0"`200`[127.0.0.1]`-`"-"`0.021`10640`-`127.0.0.1`l.sinaimg.cn`-
Hangout配置包括三个部分:inputs、filters和outputs
如下所示,是一个从Kafka读取数据流的配置
inputs:- Kafka:codec: plainencoding: UTF8 # defaut UTF8topic:comos-proxy: 10consumer_settings:group.id: hangout_bip_cmszookeeper.connect: localhost:2181auto.commit.interval.ms: "60000"socket.receive.buffer.bytes: "1048576"fetch.message.max.bytes: "1048576"
在Filters部分,这里有一系列转化的步骤,包括正则解析、时间转换、类型转换等
filters:- Grok:match:- '%{NOTSPACE:_hostname}`\[%{HTTPDATE:timestamp}\]`%{NOTSPACE:upstream}`"%{NOTSPACE:_method}\s%{NOTSPACE:_uri}\s%{NOTSPACE:httpversion}"`%{QS:_ua}`%{NUMBER:_http_code}`\[%{IP:_remote_addr}\]`%{NOTSPACE:unknow1}`%{QS:_reference}`%{NUMBER:_request_time}`%{NUMBER:_data_size}`%{NOTSPACE:unknow3}`%{IP:_http_x_forwarded_for}`%{NOTSPACE:_domain}`%{DATA:unknow4}$'remove_fields: ['message']- Date:src: timestampformats:- 'dd/MMM/yyyy:HH:mm:ss Z'remove_fields: ['timestamp']target: utc_date- Convert:fields:_request_time:to: float- Add:fields:date: "${(utc_date)?substring(0, 10)}"datetime: "${(utc_date)?substring(0, 10) + ' ' + (utc_date)?substring(11, 19)}"hour: "${(utc_date)?substring(11, 13)}"- Convert:fields:hour:to: integerminute:to: integer_data_size:to: integer
最后我们将处理好的结构化数据写入ClickHouse
outputs:- com.sina.bip.hangout.outputs.Clickhouse:host: localhost:8123database: cmstable: cms_msg_allfields: ['date', 'datetime','hour', '_hostname', '_domain', '_data_size', '_uri', '_request_time', '_ua', '_http_code', '_remote_addr', '_method', '_reference', '_url']replace_include_fields: ['_uri', '_url']bulk_size: 300
当然, ClickHouse存储这些数据的前提是我们已经建立好了这些数据表。具体建表操作如下:
CREATE TABLE cms.cms_msg(date Date,datetime DateTime,hour Int8,_uri String,_url String,_request_time Float32,_http_code String,_hostname String,_domain String,_http_x_forwarded_for String,_remote_addr String,_reference String,_data_size Int32,_method String,_rs String,_rs_time Float32,_ua String) ENGINE = MergeTree(date, (hour, date), 8192)CREATE TABLE cms.cms_msg_all(date Date,datetime DateTime,hour Int8,_uri String,_url String,_request_time Float32,_http_code String,_hostname String,_domain String,_http_x_forwarded_for String,_remote_addr String,_reference String,_data_size Int32,_method String,_ua String) ENGINE = Distributed(bip_ck_cluster, 'cms', 'cms_msg', rand())
在这篇文章中,我们介绍了如何使用Hangout将Nginx日志文件写入ClickHouse中。Hangout从Kafka中读取原始日志,将其转换成为结构化的数据,因此能被我们的Hangout-output-clickhouse插件读取写入ClickHouse中。整个流程还有很多可以自定义和提升的地方,Hangout使用请参照Hangout README,Hangout-output-clickhouse的更多功能请参照README。此外,我们在ClickHouse数据的基础上使用了SuperSet和Grafana作为我们的数据展示和监控工具。