kafka数据同步clickhouse

154 阅读2分钟

实际工作中,有一部分数据,因为有很强的时间相关属性,因此我们考虑使用clickhouse来存储,比如操作日志什么的

又因为技术栈的不统一,不如不同的团队使用不同的编程语言来开发,因此直接在应用程序中写入clickhouse会比较繁琐,且多个团队存在重复工作,在此基础上维护和升级也会变得很复杂

因此,我们经常考虑异步写入的方式来解耦和统一维护;通常我们将需要写入的数据写入到kafka(通常在ELK体系下,应用无感),然后从kafka同步到clickhouse

本文记录下操作流程

1、准备kafka和clickhouse,本地可以直接使用docker,这里略去

2、创建clickhouse 表

create table test.access_logging
(
    timeLocal             DateTime64(3),
    clientIp              String,
    userId                String,
    requestUri            String,
    host                  String,
    path                  String,
    method                String,
    userAgent             String,
    authorization         String,
    xUnid                 String,
    cfConnectingIp        String,
    cfIpcountry           String,
    origin                String,
    referer               String,
    secChUa               String,
    secChUaMobile         String,
    secChUaPlatform       String,
    timestamp             Nullable(DateTime64(3)),
    timezone              String,
    xAmznTraceId          String,
    xMachineId            String,
    xRequestId            String,
    xVersion              String,
    xTokenInfo            String,
    queryParams           String,
    requestBody           String,
    status                UInt16,
    xTraceId              String,
    respDate              String,
    respException         String,
    respMessage           String,
    respData              String,
    respTimestamp         Nullable(DateTime64(3)),
    respSuccess           Nullable(Bool),
    respStatus            Nullable(UInt64),
    respBody              String,
    responseContentLength UInt32,
    hostname              String,
    upstreamIp            String,
    upstreamResponseTime  UInt32
) Engine=ReplacingMergeTree()

3、创建clicke house queue

create table test.access_logging_queue
(
    timeLocal             DateTime64(3),
    clientIp              String,
    userId                String,
    requestUri            String,
    host                  String,
    path                  String,
    method                String,
    userAgent             String,
    authorization         String,
    xUnid                 String,
    cfConnectingIp        String,
    cfIpcountry           String,
    origin                String,
    referer               String,
    secChUa               String,
    secChUaMobile         String,
    secChUaPlatform       String,
    timestamp             Nullable(DateTime64(3)),
    timezone              String,
    xAmznTraceId          String,
    xMachineId            String,
    xRequestId            String,
    xVersion              String,
    xTokenInfo            String,
    queryParams           String,
    requestBody           String,
    status                UInt16,
    xTraceId              String,
    respDate              String,
    respException         String,
    respMessage           String,
    respData              String,
    respTimestamp         Nullable(DateTime64(3)),
    respSuccess           Nullable(Bool),
    respStatus            Nullable(UInt64),
    respBody              String,
    responseContentLength UInt32,
    hostname              String,
    upstreamIp            String,
    upstreamResponseTime  UInt32
) ENGINE = Kafka
      SETTINGS
          kafka_broker_list'<kafka broker list>',
          kafka_topic_list'topic名称',
          kafka_group_name'consumer group名称',
          kafka_format'JSONEachRow',
          kafka_row_delimiter'\n',
          kafka_schema'',
          kafka_num_consumers1

4、创建物化视图

CREATE MATERIALIZED VIEW IF NOT EXISTS test.access_logging_queue_mv TO test.access_logging AS SELECT * FROM test.access_logging_queue;

执行完第三步,之后,可以查看kafka,此时consumer group已经创建

查询clickhouse,可以发现access_logging表已经有数据

参考文档:juejin.cn/post/707236…

本文使用 文章同步助手 同步