以下是结合 MySQL(OLTP) 和 ClickHouse(OLAP) 的 Great Expectations 完整落地流程,针对商超线上 / 线下场景设计,包含环境搭建、数据生成、质量验证及结果分析:
1. 环境准备与安装
1.1. 环境准备

1.2. 安装依赖工具
# 创建虚拟环境 python3 -m venv ge_env source ge_env/bin/activate # Linux/macOS # 或 ge_env\Scripts\activate # Windows # 安装核心依赖(使用清华数据源) pip3 install urllib3==1.26.6 cryptography==38.0.4 great_expectations==0.17.19 -i https://pypi.tuna.tsinghua.edu.cn/simple # Great Expectations本体 pip3 install mysql-connector-python -i https://pypi.tuna.tsinghua.edu.cn/simple # 连接MySQL pip3 install clickhouse-connect -i https://pypi.tuna.tsinghua.edu.cn/simple # 连接ClickHouse pip3 install pandas faker -i https://pypi.tuna.tsinghua.edu.cn/simple # 数据生成工具
pip3 install "sqlalchemy==1.4.46" -i https://pypi.tuna.tsinghua.edu.cn/simple # sqlalchemy 依赖 ,不能安装2.0以上版本
2. 步骤 1:创建 MySQL 和 ClickHouse 测试表
2.1. MySQL(OLTP)表设计(线上订单表)
-- 连接MySQL(示例命令) mysql -u root -p -- 创建数据库 CREATE DATABASE supermarket_oltp; USE supermarket_oltp; -- 线上订单表(线上环境) CREATE TABLE online_orders ( order_id INT PRIMARY KEY AUTO_INCREMENT, user_id INT NOT NULL, product_id VARCHAR(20) NOT NULL, quantity INT NOT NULL, unit_price DECIMAL(10,2) NOT NULL, total_amount DECIMAL(10,2) NOT NULL, payment_status ENUM('unpaid', 'paid', 'refunded') NOT NULL, order_time DATETIME NOT NULL, delivery_addr VARCHAR(255) );
2.2. ClickHouse(OLAP)表设计(线下销售汇总表)
-- 连接ClickHouse(示例命令) clickhouse-client --host localhost --port 9000 -- 创建数据库 CREATE DATABASE IF NOT EXISTS supermarket_olap; USE supermarket_olap; -- 线下销售汇总表(线下环境,按日分区) CREATE TABLE offline_sales ( sale_date Date NOT NULL, store_id INT NOT NULL, product_id VARCHAR(20) NOT NULL, total_quantity INT NOT NULL, total_revenue DECIMAL(15,2) NOT NULL, avg_price DECIMAL(10,2) NOT NULL, sale_count INT NOT NULL -- 当日销售笔数 ) ENGINE = MergeTree() PARTITION BY sale_date ORDER BY (store_id, product_id);
3. 步骤 2:Python 脚本生成测试数据
运行脚本生成数据
# 替换脚本中的数据库密码后执行
python generate_test_data.py
4. 步骤 3:使用 Great Expectations 验证数据质量
4.1. 初始化 Great Expectations 项目
mkdir ge_supermarket && cd ge_supermarket great_expectations init # 生成项目结构
[root@zb-yunweitest-mysql-204-200 ge_supermarket]# great_expectations --version great_expectations, version 0.17.19 [root@zb-yunweitest-mysql-204-200 ge_supermarket]# [root@zb-yunweitest-mysql-204-200 ge_supermarket]# tree gx gx ├── checkpoints ├── expectations ├── great_expectations.yml ├── plugins │ └── custom_data_docs │ ├── renderers │ ├── styles │ │ └── data_docs_custom_styles.css │ └── views ├── profilers └── uncommitted ├── config_variables.yml ├── data_docs └── validations 11 directories, 3 files [root@zb-yunweitest-mysql-204-200 ge_supermarket]#
4.2. 配置数据源(MySQL 和 ClickHouse)
4.3. 创建期望套件(数据质量规则)
(1)线上订单数据规则(MySQL)
(2)线下销售数据规则(ClickHouse)
4.4 执行数据质量验证-命令行
[root@zb-yunweitest-mysql-204-200 gx]# great_expectations checkpoint run supermarket_checkpoint Calculating Metrics: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 28/28 [00:00<00:00, 93.19it/s] Validation failed! Suite Name Status Expectations met - online_orders_suite ✖ Failed 1 of 3 (33.33 %)
生成HTML文件
[root@zb-yunweitest-mysql-204-200 gx]# great_expectations docs build The following Data Docs sites will be built: - local_site: file:///data/ge_supermarket/gx/uncommitted/data_docs/local_site/index.html Would you like to proceed? [Y/n]: Y Building Data Docs... Done building Data Docs [root@zb-yunweitest-mysql-204-200 gx]# cd /data/ge_supermarket/gx/uncommitted/data_docs/local_site/ [root@zb-yunweitest-mysql-204-200 local_site]# ll total 40K drwxr-xr-x 5 root root 77 Aug 14 15:12 . drwxr-xr-x 3 root root 24 Aug 14 15:12 .. drwxr-xr-x 2 root root 38 Aug 14 15:12 expectations -rw-r--r-- 1 root root 37K Aug 14 15:14 index.html drwxr-xr-x 5 root root 47 Aug 14 15:12 static drwxr-xr-x 3 root root 33 Aug 14 15:12 validations [root@zb-yunweitest-mysql-204-200 local_site]#
4.4. 执行数据质量验证-Python执行
运行验证脚本
5. 步骤 4:数据质量结果输出与分析
5.1. 查看 HTML 报告
# Linux/macOS open great_expectations/uncommitted/data_docs/local_site/index.html # Windows start great_expectations/uncommitted/data_docs/local_site/index.html

posted on
浙公网安备 33010602011771号