UDF返回数据的例子

spark = SparkSession.builder. \
    appName("wk_stg_10650018_cs"). \
    enableHiveSupport(). \
    config("spark.debug.maxToStringFields", "100"). \
    getOrCreate()

df = spark.createDataFrame([('a', [1, 2, 3],['x','y']), ('b', [5, 2, 3],['x','y'])], ['id', 'c1','c2'])
df.createOrReplaceTempView('t1')

def gen(begin,end):
    return list(range(begin,end))
# spark.udf.register(
'gen', gen, ArrayType(IntegerType())) spark.sql('select id, gen(1,3) as arr from t1').createOrReplaceTempView('t1') spark.sql( "select id,c" " from t1" " lateral view explode(arr) A as c" ).show()

 

posted @ 2020-10-23 16:51  muyue123  阅读(334)  评论(0编辑  收藏  举报