基于映射表校验数据条目并筛选未匹配记录
需求:校验颜色与对象的映射关系并筛选违规记录
需将数据表格中Color与Object的组合条目,和映射表中MappingKey(对应Color)与MappingValue(对应Object)的允许映射关系进行校验,筛选出不在允许映射范围内的数据记录。
数据表格(Data Table)
| ID | Color | Object |
|---|---|---|
| 001 | Green | Grass |
| 002 | Green | Tree |
| 003 | Green | Sky |
| 004 | Green | Apple |
| 005 | Red | Apple |
| 006 | Red | Poppy |
| 007 | Red | Water |
允许映射表(Allowed Mappings)
| MappingID | MappingKey | MappingValue |
|---|---|---|
| M001 | Green | Grass |
| M002 | Green | Tree |
| M003 | Green | Apple |
| M004 | Red | Apple |
| M005 | Red | Poppy |
预期输出结果
| ID | Color | Object |
|---|---|---|
| 003 | Green | Sky |
| 007 | Red | Water |
Spark DataFrame 定义
data_df = spark.createDataFrame( [ ("001", "Green", "Grass"), ("002", "Green", "Tree"), ("003", "Green", "Sky"), ("004", "Green", "Apple"), ("005", "Red", "Apple"), ("006", "Red", "Poppy"), ("007", "Red", "Water") ], ["ID", "Color", "Object"], ) mapping_df = spark.createDataFrame( [ ("M001", "Green", "Grass"), ("M002", "Green", "Tree"), ("M003", "Green", "Apple"), ("M004", "Red", "Apple"), ("M005", "Red", "Poppy") ], ["MappingID", "MappingKey", "MappingValue"], )
内容的提问来源于stack exchange,提问作者hbit
相关产品推荐
相关产品推荐

