如何从KSQL中的单个主题创建不同的表?

时间:2018-08-30 08:43:14

标签: apache-kafka confluent ksql

我有一个Kafka主题,其中包含客户,购买,产品等数据。我想从这个单一主题创建不同的流。

1 个答案:

答案 0 :(得分:0)

让我假设您的Kafka主题的消息(JSON格式)如下:

{
    "customer_id":value,
    "customer_name":value,
    "purchase_id":value,
    "purchase_desc":value,
    "product_id":value,
    "product_name":value
}

并假定主题名称为my_topic

所以您可以这样做:

对于customer流:

CREATE STREAM customer (
    -- which 'customer_id' match the JSON key
    customer_id BIGINT,
    customer_name VARCHAR
) WITH (
    VALUE_FORMAT = 'JSON',
    KAFKA_TOPIC = 'my_topic'
);

对于purchase表:

CREATE STREAM purchase (
    purchase_id BIGINT,
    purchase_desc VARCHAR
) WITH (
    VALUE_FORMAT = 'JSON',
    KAFKA_TOPIC = 'my_topic'
);

对于product表:

CREATE STREAM product (
    product_id BIGINT,
    product_name VARCHAR
) WITH (
    VALUE_FORMAT = 'JSON',
    KAFKA_TOPIC = 'my_topic'
);