Clickhouse 改变物化视图的 select

Clickhouse altering materialized view's select

我有以下设置:

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 table。 根据文档,为了这样做,我需要执行后续步骤:

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

    DETACH TABLE request_income;

  2. 删除从 Kafka 流式传输数据的 table,因为 Kafka 引擎不支持 ALTER 查询。

    DROP TABLE request_income_buffer

  3. 重新创建 table 使用新字段从 Kafka 流式传输数据。

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

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

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

  5. 根据post从上面更新视图的select查询

  6. 附加视图

    ATTACH TABLE request_income

问题是如何更新视图的 select 查询?

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

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

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

  2. 使用您最喜欢的文本编辑器修改视图的 sql。在我的例子中,编辑后的 ​​sql 看起来像

    ATTACH MATERIALIZED VIEW request_income ( date Date, date_time DateTime, timestamp UInt64, microtime Float32, traceId Int64, host String, ip String, type String, service String, message String, caller String, context String ) 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 default.request_income_buffer

  3. 附上修改后的视图

    ATTACH TABLE request_income;

试试这个:

  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;