2017-12-21 3 views
1

UDF를 만들었지 만 UDF 내에서 함수를 호출해야합니다. 현재 null을 반환합니다. 누군가이 오류가 발생하는 이유를 설명해주세요.PySpark - UDF 내에서 함수 호출하기

a= spark.createDataFrame([("A", 20), ("B", 30), ("D", 80)],["Letter", "distances"]) 
def get_number(num): 
    return range(num) 
from pyspark.sql.functions import udf 
def cate(label): 
    if label == 20: 
     counting_list = get_number(4) 
     return counting_list 
    else: 
     return [0] 

udf_score=udf(cate, ArrayType(FloatType())) 
a.withColumn("category_list", udf_score(a["distances"])).show(10) 

아웃 : cate 정수하지 수레의 배열을 반환 이후 UDF에 대한

+------+---------+--------------------+ 
|Letter|distances|  category_list| 
+------+---------+--------------------+ 
|  A|  20|[null, null, null...| 
|  B|  30|    [null]| 
|  D|  80|    [null]| 
+------+---------+--------------------+ 
+0

파이썬이 파이썬 3를 사용하는 경우는 언급하지 않는다? 'range()'는 파이썬 2와 비교하여 파이썬 3에서 다르게 동작합니다. –

+0

파이썬 2를 사용하고 있습니다. –

답변

2

데이터 유형이 올바르지 않습니다. 당신은 변경하십시오 수 :

udf_score=udf(cate, ArrayType(FloatType())) 

에 :이 도움이

udf_score=udf(cate, ArrayType(IntegerType())) 

희망!

편집 : @Shane Halloran을이 range 파이썬 3.x를 다르게 동작 코멘트에 언급으로 이후에 대한 파이썬 2.x에서 range 가정

관련 문제