udf

from pyspark.sql import SparkSession
from pyspark.sql.types import StructField, StructType, StringType, IntegerType, LongType,ArrayType
from pyspark.sql import functions as F

spark = SparkSession.builder.appName("get_app_category").enableHiveSupport().\
    config("spark.driver.host", "localhost").config("spark.debug.maxToStringFields", "100").getOrCreate()
df = spark.createDataFrame([('数学', '张三', 88), ('语文', '张三', 92), ('英语', '张三', 77),
                            ('数学', '王五', 65), ('语文', '王五', 87), ('英语', '王五', 90),
                            ('数学', '李雷', 67), ('语文', '李雷', 33), ('英语', '李雷', 24),
                            ('数学', '宫九', 77), ('语文', '宫九', 87), ('英语', '宫九', 90)
                            ], ['subject', 'name', 'score'])
df.show()

spark.udf.register("is_good",lambda x:'good' if x>=85 else 'bad',StringType())
df.createOrReplaceTempView('t1')
spark.sql('select subject,name,score,is_good(score) as is_good from t1').show()

 

posted @ 2020-07-14 14:21  muyue123  阅读(332)  评论(0编辑  收藏  举报