我需要使用两个不同数据集中的段值及其阈值生成一个段数组。在pyspark或hivesql中有没有一种简单的方法可以做到这一点
段值数据集:
--------------------------------------------------
| user_id | seg1 | seg2 | seg3 | seg4 | seg5 |
------------------------------------------------
| 100 | 90 | 20 | 76 | 100 | 30 |
| 200 | 56 | 15 | 67 | 99 | 25 |
| 300 | 87 | 38 | 45 | 97 | 40 |
--------------------------------------------------
段阈值数据集:
---------------------------
|seg_name | seg_threshold |
---------------------------
| seg1 | 83 |
| seg2 | 25 |
| seg3 | 60 |
| seg4 | 98 |
| seg5 | 35 |
---------------------------
如果某段的值高于阈值,则应将用户视为该段的一部分。该用户的段数组应包括段名称(列标题)
预期产出:
-------------------------------------
| user_id| segment_array |
-------------------------------------
| 100 | [seg1, seg3, seg4] |
| 200 | [seg3, seg4] |
| 300 | [seg1, seg2, seg5] |
-------------------------------------
请注意,这只是一个指示性数据集。我有几百个这样的片段
谢谢你的帮助
数百个阈值条目可能是broadcasted。然后可以在UDF中检查值是否高于或低于阈值:
我的结果是
此结果与预期结果略有不同。也许测试数据有问题
@werner的解决方案是完全有效的
在纯sparksql中,有一种方法可以在没有udf的情况下实现这一点
准备数据帧:
现在,您可以使用一个名为stack的边缘但非常有用的函数执行“unpivot”操作:
这是输出:
你提到你有数百段。可以使用循环在堆栈函数内轻松生成表达式
这项技术在spark工具箱中非常有用
相关问题 更多 >
编程相关推荐