pyspark中的UDF SQL上下文将数据作为列发送

时间:2016-06-01 11:43:19

标签: python-2.7 apache-spark pyspark udf

我在pyspark写了一个udf,如下所示:

df1 = df.where(point_inside_polygon(latitide,longitude,polygonArr))

df1和df是火花数据帧

该功能如下:

def point_inside_polygon(x,y,poly):


latt = float(x)
long = float(y)
if ((math.isnan(latt)) or (math.isnan(long))):
    point = sh.geometry.Point(latt, long)
    polygonArr = poly
    polygon=MultiPoint(polygonArr).convex_hull
    if polygon.contains(point):
        return True
    else:
        return False
else:
    return False

但是当我尝试检查纬度和经度的数据类型时,它是一类列。 数据类型为Column

有没有办法迭代每个元组并使用它们的值,而不是采用数据类型列。 我不想使用for循环,因为我有一个巨大的记录集,它违背了使用SPARK的目的。

有没有办法将列值传递给float,或者在函数内部转换它们?

1 个答案:

答案 0 :(得分:1)

使用udf包装它:

from pyspark.sql.types import BooleanType
from pyspark.sql.functions import udf

point_inside_polygon_ = udf(point_inside_polygon, BooleanType())
df1 = df.where(point_inside_polygon(latitide,longitude,polygonArr))