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()