Great Expectations 与 Spark Connect 兼容性问题分析与解决方案
背景介绍
Great Expectations 是一个开源的数据质量验证工具,它能够帮助数据团队定义、记录和验证数据质量预期。在最新版本中,用户在使用 Spark Connect 数据框架时遇到了兼容性问题,导致无法正常构建批处理请求。
问题现象
当用户尝试使用 Spark 3.5.0 及以上版本中的 Spark Connect DataFrame 时,Great Expectations 1.0.0 版本会抛出错误:"Bad input to build_batch_request: Can not build batch request for dataframe asset without a dataframe"。这是因为新版本 Spark 改变了 DataFrame 的导入路径,从原来的 pyspark.sql.DataFrame 变为了 pyspark.sql.connect.dataframe.DataFrame。
技术分析
根本原因
Great Expectations 在 spark_datasource.py 文件中通过 isinstance() 检查 DataFrame 类型时,仅检查了传统的 pyspark.sql.DataFrame 类型,而没有考虑到 Spark Connect 引入的新 DataFrame 类型。这种严格的类型检查导致了兼容性问题。
版本差异
在 Great Expectations 0.18.19 版本中,这个问题不存在,因为当时的实现方式更加宽松,没有进行严格的类型检查。但随着 1.0.0 版本的重构,引入了更严格的类型验证机制,暴露了与 Spark Connect 的兼容性问题。
解决方案
官方修复
Great Expectations 团队在 1.0.5 版本中修复了这个问题,修改了类型检查逻辑,使其同时支持传统的 pyspark.sql.DataFrame 和新的 pyspark.sql.connect.dataframe.DataFrame 类型。用户只需升级到最新版本即可解决此问题。
迁移指南
对于从旧版本迁移的用户,需要注意以下几点变化:
- 上下文初始化:不再使用
BaseDataContext,而是使用gx.get_context() - 期望配置:
ExpectationConfiguration的使用方式有所变化 - 验证流程:新的验证流程更加简洁,直接通过
batch.validate()方法执行验证
最佳实践
新版本代码示例
以下是使用 Great Expectations 1.0.5+ 验证 Spark DataFrame 的推荐方式:
import great_expectations as gx
from pyspark.sql import SparkSession
# 初始化Spark会话
spark = SparkSession.builder.appName("GEExample").getOrCreate()
# 创建示例DataFrame
data = [("2023-01-10", "A", 40), ("2023-05-15", "A", 60)]
df = spark.createDataFrame(data, ["date", "product", "value"])
# 初始化Great Expectations上下文
context = gx.get_context()
# 配置数据源和数据资产
data_source = context.data_sources.add_spark(name="my_source")
data_asset = data_source.add_dataframe_asset(name="my_asset")
# 创建批处理定义
batch_def = data_asset.add_batch_definition_whole_dataframe("my_batch")
batch_params = {"dataframe": df}
# 获取批处理对象
batch = batch_def.get_batch(batch_parameters=batch_params)
# 定义并执行验证
expectation = gx.expectations.ExpectColumnValuesToBeBetween(
column="value", min_value=50, max_value=100
)
results = batch.validate(expectation)
处理JSON期望配置
对于需要从JSON加载期望配置的场景,可以使用以下方式:
from great_expectations.core import ExpectationSuite
from great_expectations.core.expectation_configuration import ExpectationConfiguration
# 假设json_schema包含期望配置
expectations_list = []
for ele in json_schema["expectations"]:
config = ExpectationConfiguration(
type=ele["expectation_type"],
kwargs=ele["kwargs"]
)
expectations_list.append(config)
# 创建期望套件
suite = ExpectationSuite(
name="my_suite",
expectations=expectations_list
)
context.suites.add(suite)
总结
Great Expectations 1.0.5+ 版本已经解决了与 Spark Connect 的兼容性问题,用户现在可以无缝地在 Spark 3.5.0 及以上版本中使用该工具进行数据质量验证。对于从旧版本迁移的用户,建议仔细阅读官方文档,了解API变化,并按照新的模式重构验证流程。
ERNIE-4.5-VL-28B-A3B-ThinkingERNIE-4.5-VL-28B-A3B-Thinking 是 ERNIE-4.5-VL-28B-A3B 架构的重大升级,通过中期大规模视觉-语言推理数据训练,显著提升了模型的表征能力和模态对齐,实现了多模态推理能力的突破性飞跃Python00
unified-cache-managementUnified Cache Manager(推理记忆数据管理器),是一款以KV Cache为中心的推理加速套件,其融合了多类型缓存加速算法工具,分级管理并持久化推理过程中产生的KV Cache记忆数据,扩大推理上下文窗口,以实现高吞吐、低时延的推理体验,降低每Token推理成本。Python03
Kimi-K2-ThinkingKimi K2 Thinking 是最新、性能最强的开源思维模型。从 Kimi K2 开始,我们将其打造为能够逐步推理并动态调用工具的思维智能体。通过显著提升多步推理深度,并在 200–300 次连续调用中保持稳定的工具使用能力,它在 Humanity's Last Exam (HLE)、BrowseComp 等基准测试中树立了新的技术标杆。同时,K2 Thinking 是原生 INT4 量化模型,具备 256k 上下文窗口,实现了推理延迟和 GPU 内存占用的无损降低。Python00
Spark-Prover-7BSpark-Prover-7B is a 7B-parameter large language model developed by iFLYTEK for automated theorem proving in Lean4. It generates complete formal proofs for mathematical theorems using a three-stage training framework combining pre-training, supervised fine-tuning, and reinforcement learning. The model achieves strong formal reasoning performance and state-of-the-art results across multiple theorem-proving benchmarksPython00
MiniCPM-V-4_5MiniCPM-V 4.5 是 MiniCPM-V 系列中最新且功能最强的模型。该模型基于 Qwen3-8B 和 SigLIP2-400M 构建,总参数量为 80 亿。与之前的 MiniCPM-V 和 MiniCPM-o 模型相比,它在性能上有显著提升,并引入了新的实用功能Python00
Spark-Formalizer-7BSpark-Formalizer-7B is a 7B-parameter large language model by iFLYTEK for mathematical auto-formalization. It translates natural-language math problems into precise Lean4 formal statements, achieving high accuracy and logical consistency. The model is trained with a two-stage strategy combining large-scale pre-training and supervised fine-tuning for robust formal reasoning.Python00
GOT-OCR-2.0-hf阶跃星辰StepFun推出的GOT-OCR-2.0-hf是一款强大的多语言OCR开源模型,支持从普通文档到复杂场景的文字识别。它能精准处理表格、图表、数学公式、几何图形甚至乐谱等特殊内容,输出结果可通过第三方工具渲染成多种格式。模型支持1024×1024高分辨率输入,具备多页批量处理、动态分块识别和交互式区域选择等创新功能,用户可通过坐标或颜色指定识别区域。基于Apache 2.0协议开源,提供Hugging Face演示和完整代码,适用于学术研究到工业应用的广泛场景,为OCR领域带来突破性解决方案。00- HHowToCook程序员在家做饭方法指南。Programmer's guide about how to cook at home (Chinese only).Dockerfile014
Spark-Scilit-X1-13B科大讯飞Spark Scilit-X1-13B基于最新一代科大讯飞基础模型,并针对源自科学文献的多项核心任务进行了训练。作为一款专为学术研究场景打造的大型语言模型,它在论文辅助阅读、学术翻译、英语润色和评论生成等方面均表现出色,旨在为研究人员、教师和学生提供高效、精准的智能辅助。Python00- PpathwayPathway is an open framework for high-throughput and low-latency real-time data processing.Python00