流批一体架构
数据建模入门数据建模
问题
如何设计同时支持实时和离线分析的流批一体数据架构?
答题要点
Lambda架构 vs Kappa架构:
Lambda架构(双链路)
┌─────────────┐
│ 数据源 │
│(Kafka/Log) │
└──────┬──────┘
│
┌──────────┴──────────┐
│ │
┌──────▼──────┐ ┌──────▼──────┐
│ 批处理层 │ │ 流处理层 │
│(Spark Batch)│ │(Flink/Spark │
│ │ │ Streaming) │
└──────┬──────┘ └──────┬──────┘
│ │
┌──────▼──────┐ ┌──────▼──────┐
│ Batch View │ │ Real-time │
│(Hive/Delta) │ │ View(Redis/ │
│ │ │ ClickHouse) │
└──────┬──────┘ └──────┬──────┘
│ │
└──────────┬──────────┘
│
┌──────▼──────┐
│ 服务层 │
│(API/BI Tool)│
└─────────────┘
-- 批处理层: 每日T+1数据
-- Hive/Spark SQL
CREATE TABLE batch_daily_sales
STORED AS PARQUET
AS
SELECT
DATE(order_timestamp) AS order_date,
product_id,
SUM(sales_amount) AS total_sales,
COUNT(DISTINCT customer_id) AS customer_count
FROM raw_orders
WHERE DATE(order_timestamp) = CURRENT_DATE - INTERVAL 1 DAY
GROUP BY DATE(order_timestamp), product_id;
-- 流处理层: 实时数据(Flink SQL)
CREATE TABLE realtime_sales (
order_timestamp TIMESTAMP(3),
product_id STRING,
sales_amount DECIMAL(10, 2),
customer_id STRING,
WATERMARK FOR order_timestamp AS order_timestamp - INTERVAL '10' SECOND
) WITH (
'connector' = 'kafka',
'topic' = 'orders',
'properties.bootstrap.servers' = 'localhost:9092'
);
INSERT INTO clickhouse_realtime_sales
SELECT
TUMBLE_START(order_timestamp, INTERVAL '5' MINUTE) AS window_start,
product_id,
SUM(sales_amount) AS total_sales,
COUNT(DISTINCT customer_id) AS customer_count
FROM realtime_sales
GROUP BY TUMBLE(order_timestamp, INTERVAL '5' MINUTE), product_id;
-- 服务层: 合并批和流视图
CREATE VIEW unified_sales AS
-- 历史数据(T-1及以前)
SELECT order_date, product_id, total_sales, customer_count
FROM batch_daily_sales
WHERE order_date < CURRENT_DATE
UNION ALL
-- 今日实时数据
SELECT
DATE(window_start) AS order_date,
product_id,
SUM(total_sales) AS total_sales,
SUM(customer_count) AS customer_count
FROM clickhouse_realtime_sales
WHERE DATE(window_start) = CURRENT_DATE
GROUP BY DATE(window_start), product_id;
Kappa架构(纯流)
┌─────────────┐
│ 数据源 │
│(Kafka) │
└──────┬──────┘
│
┌──────▼──────┐
│ 流处理层 │
│(Flink) │
│ │
└──────┬──────┘
│
┌──────────┴──────────┐
│ │
┌──────▼──────┐ ┌──────▼──────┐
│ 状态存储 │ │ 结果存储 │
│(RocksDB) │ │(ClickHouse) │
└─────────────┘ └──────┬──────┘
│
┌──────▼──────┐
│ 服务层 │
└─────────────┘
-- Flink SQL: 流批统一
-- 同一套SQL逻辑处理流和批
CREATE TABLE unified_sales (
order_date DATE,
product_id STRING,
total_sales DECIMAL(12, 2),
customer_count BIGINT,
PRIMARY KEY (order_date, product_id) NOT ENFORCED
) WITH (
'connector' = 'upsert-kafka',
'topic' = 'sales_aggregates',
'key.format' = 'json',
'value.format' = 'json'
);
-- 流模式: 实时更新聚合
SET 'execution.runtime-mode' = 'streaming';
INSERT INTO unified_sales
SELECT
DATE(order_timestamp) AS order_date,
product_id,
SUM(sales_amount) AS total_sales,
COUNT(DISTINCT customer_id) AS customer_count
FROM raw_orders
GROUP BY DATE(order_timestamp), product_id;
-- 批模式: 历史数据回填(同一套SQL)
SET 'execution.runtime-mode' = 'batch';
SET 'table.exec.source.idle-timeout' = '0'; -- 批模式不等待
INSERT INTO unified_sales
SELECT
DATE(order_timestamp) AS order_date,
product_id,
SUM(sales_amount) AS total_sales,
COUNT(DISTINCT customer_id) AS customer_count
FROM raw_orders
WHERE order_timestamp BETWEEN '2024-01-01' AND '2024-05-31'
GROUP BY DATE(order_timestamp), product_id;
完整解析在题库里看
核心知识点、易错点和面试官想听到什么;练过的题会进入记录,答错的进错题本。
去练这道题