Clickhouse改变物化视图的选择

时间:2018-05-31 10:46:08

标签: clickhouse

我有以下设置:

CREATE TABLE IF NOT EXISTS request_income_buffer (
    timestamp UInt64,
    timestamp_micro Float32,
    traceId Int64,
    host String,
    type String,
    service String,
    message String,
    caller String,
    context String
) ENGINE = Kafka('kafka:9092', 'request_income', 'group', 'JSONEachRow');

CREATE MATERIALIZED VIEW IF NOT EXISTS request_income
ENGINE = MergeTree(date, microtime, 8192) AS
    SELECT
        toDate(toDateTime(timestamp)) AS `date`,
        toDateTime(timestamp) as `date_time`,
        timestamp,
        timestamp_micro AS `microtime`,
        traceId,
        host,
        type,
        service,
        message,
        caller,
        context
    FROM
        request_income_buffer;

我想添加新列,例如。 ipmy request_income表。 根据文档,为了做到这一点,我将需要遵循下一步:

  1. 分离视图以停止接收来自Kafka的消息。

      

    DETACH TABLE request_income;

  2. 由于Kafka引擎不支持ALTER查询,因此从Kafka流式传输数据的表格。

      

    DROP TABLE request_income_buffer

  3. 使用新字段重新创建从Kafka传输数据的表。

      

    CREATE TABLE IF NOT NOT EXISTS request_income_buffer(   时间戳UInt64,   timestamp_micro Float32,   traceId Int64,   主机字符串,   ip String,   类型字符串,   服务字符串,   消息字符串,   调用者字符串,   context字符串   )ENGINE = Kafka('kafka:9092','request_income','group','JSONEachRow');

  4. 根据分离的物化视图的这个post更新.inner表

      

    ALTER TABLE` .inner.request_income` ADD COLUMN ip String AFTER host;

  5. 根据上述更新视图中的帖子选择查询

  6. 附加视图
      

    ATTACH TABLE request_income

  7. 问题是如何更新视图的选择查询?

2 个答案:

答案 0 :(得分:4)

所以看来更新物化视图的选择查询的方法如下:

  1. 获取视图元数据的路径
      

    SELECT metadata_path FROM system.tables WHERE name ='request_income';

  2. 使用您喜欢的文本编辑器修改视图的sql。在我的情况下,编辑的sql看起来像

      

    ATTACH MATERIALIZED VIEW request_income(       日期日期,       date_time DateTime,       时间戳UInt64,       microtime Float32,       traceId Int64,       主机字符串,        ip String,       类型字符串,       服务字符串,       消息字符串,       调用者字符串,       context String)ENGINE = MergeTree(date,microtime,8192)AS SELECT       toDate(toDateTime(timestamp))AS日期,       toDateTime(timestamp)AS date_time,       时间戳,       timestamp_micro AS microtime,       traceId,       主办,       的 IP 下,       类型,       服务,       信息,       呼叫者,       context FROM default.request_income_buffer

  3. 附加修改后的视图

      

    ATTACH TABLE request_income;

答案 1 :(得分:0)

试试这个:

  1. DETACH TABLE request_income;
  2. ALTER TABLE `.inner.request_income` ADD COLUMN ip String AFTER host;
  3. ATTACH MATERIALIZED VIEW request_income 
    ENGINE = MergeTree(date, microtime, 8192) AS
     SELECT
         toDate(toDateTime(timestamp)) AS `date`,
         toDateTime(timestamp) as `date_time`,
         timestamp,
         timestamp_micro AS `microtime`,
         traceId,
         host,
         ip,
         type,
         service,
         message,
         caller,
         context
     FROM
         request_income_buffer;