Great Expectations 0.18.9结合PySpark条件验证报错求助
问题原因与解决思路
问题根源
Great Expectations 0.18.9版本中,SparkDFDataset的expect_column_values_to_not_be_null方法并未实现row_condition和condition_parser参数,这两个参数当时仅在部分Dataset(如PandasDataset)或后续版本的Spark支持中存在,直接传入会触发参数不匹配的TypeError。
可行解决方案
方案1:使用expect_row_condition_to_be_met实现跨列条件验证
这个expectation类型专门用于自定义行级条件判断,适合你需要的"colB为NULL时colA必须为NULL"规则。修改后的expectation配置如下:
expectations_json = { "expectation_suite_name": "name", "expectations": [ { "expectation_type": "expect_row_condition_to_be_met", "kwargs": { # 规则逻辑:不存在"colB为NULL但colA不为NULL"的行 "condition": "NOT (col(\"colB\").isNull() AND col(\"colA\").isNotNull())", "condition_parser": "great_expectations__experimental__" } } ] } geDF = SparkDFDataset(df) expectation_suite = ExpectationSuite(**expectations_json) dq_json_result = geDF.validate(expectation_suite)
如果习惯用Spark SQL语法,也可以将condition_parser改为"spark_sql",条件写成SQL风格:
"condition": "NOT (colB IS NULL AND colA IS NOT NULL)", "condition_parser": "spark_sql"
方案2:手动过滤数据集后单独验证
先过滤出colB为NULL的子数据集,再验证该子集中colA全为NULL:
geDF = SparkDFDataset(df) # 过滤colB为NULL的行 filtered_geDF = SparkDFDataset(df.filter(df.colB.isNull())) # 验证这些行的colA必须全为NULL result = filtered_geDF.expect_column_values_to_be_null(column="colA")
方案3:升级Great Expectations版本
如果业务允许,升级到0.19.x及以上版本,新版本的SparkDFDataset已支持在单个列expect方法中传入row_condition和condition_parser参数,你的原始代码可以直接生效。
内容的提问来源于stack exchange,提问作者Nidutt
相关产品推荐
相关产品推荐

