对于表1中的每一行,我都在尝试统计表2中的行,并根据表1中的值满足条件
表1中的年龄应介于表2的起始年龄和结束年龄之间,或等于起始年龄和结束年龄
是否可以使用udf和WITH列进行此操作?我尝试了几种方法,比如使用withColumn和withColumn与UDF,但两种方法都失败了
def counter(a):
return table2.where((table2.StartAge <= a) & (table2.EndAge >=a)).count()
counter_udf = udf(lambda age: counter(age), IntegerType())
table1 = table1.withColumn('Count', counter_udf('Age ID'))
这有意义吗? 谢谢
输入和输出示例:
如果要在脚本中使用自定义项,必须首先向spark注册它
使用这行代码有助于修复错误:
看看这个。您可以使用spark sql实现它
相关问题 更多 >
编程相关推荐