• Spark SQL UDF示例


    UDF即用户自定函数,注册之后,在sql语句中使用。

    基于scala-sdk-2.10.7,Spark2.0.0。

    package UDF_UDAF
    
    import java.util
    
    import org.apache.spark.sql.{RowFactory, SparkSession}
    import org.apache.spark.SparkConf
    import org.apache.spark.sql.api.java.UDF1
    import org.apache.spark.sql.types.{DataTypes, StructField}
    
    // 自定义一个继承自 UDF1(或UDF2,UDF3,UDF4...)的类
    class UDF extends UDF1[String,Int]{ override def call(t1: String): Int = { t1.length } } object UDF{ def main(args: Array[String]): Unit = { val warehouseLocation = "/code/VersionTest/spark-warehouse" //必须是相对路径 val conf = new SparkConf().setMaster("local").setAppName("udf") val sparkSession = SparkSession.builder() .config(conf) .config("spark.sql.warehouse.dir", warehouseLocation) //设置warehouse .getOrCreate() val sc = sparkSession.sparkContext val parallize = sc.parallelize(Array("zhangsan","lisi","wangwu")) val rowRDD = parallize.map(s=>RowFactory.create(s)) val fields = new util.ArrayList[StructField]() fields.add(DataTypes.createStructField("name",DataTypes.StringType,true)) val schema = DataTypes.createStructType(fields) val df = sqlSession.createDataFrame(rowRDD, schema) df.createOrReplaceTempView("user") sparkSession.udf.register("StrLen", new UDF(),DataTypes.IntegerType) sparkSession.sql("select name, StrLen(name) as length from user").show() sparkSession.stop() } }

    结果

  • 相关阅读:
    浅谈移动前端的最佳实践
    Android系统以及Activity启动讲解
    IntelliJ IDEA快捷键大全
    Gson 防止被混淆
    APP崩溃重启
    Green Dao数据库升级
    Expected resource of type anim [ResourceType]
    android studio "android() not found"
    BroadcastReceiver动态广播夸类,夸界面使用
    关于shape使用
  • 原文地址:https://www.cnblogs.com/144823836yj/p/10768888.html
Copyright © 2020-2023  润新知