pyspark 中的 UDF SQL 将数据作为列发送的上下文
UDF in pyspark SQL Context sending data as columns
我在 pyspark 中写了一个 udf,如下所示:
df1 = df.where(point_inside_polygon(latitide,longitude,polygonArr))
df1 和 df 是 spark 数据帧
函数如下:
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
但是当我尝试检查纬度和经度的数据类型时,它是一个 class 列。
数据类型为 Column
有没有办法遍历每个元组并使用它们的值,而不是采用数据类型列。
我不想使用 for 循环,因为我有一个巨大的记录集,它违背了使用 SPARK 的目的。
有没有办法将列值作为浮点数传递,或者在函数内转换它们?
使用 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))
我在 pyspark 中写了一个 udf,如下所示:
df1 = df.where(point_inside_polygon(latitide,longitude,polygonArr))
df1 和 df 是 spark 数据帧
函数如下:
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
但是当我尝试检查纬度和经度的数据类型时,它是一个 class 列。 数据类型为 Column
有没有办法遍历每个元组并使用它们的值,而不是采用数据类型列。 我不想使用 for 循环,因为我有一个巨大的记录集,它违背了使用 SPARK 的目的。
有没有办法将列值作为浮点数传递,或者在函数内转换它们?
使用 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))