5

我有以下设置:

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 流式传输数据的表,因为 Kafka 引擎不支持ALTER查询。

    DROP TABLE request_income_buffer

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

    如果不存在则创建表 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' , '组', 'JSONEachRow');

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

    ALTER TABLE `.inner.request_income` 在主机之后添加列 ip 字符串;

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

  6. 附加视图

    附加表 request_income

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

4

2 回答 2

8

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

  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. 将修改后的视图附加回来

    附表请求收入;

于 2018-05-31T12:23:15.860 回答
2

试试这个:

  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;
    
于 2021-07-27T08:28:11.167 回答