• SQLContext、HiveContext自定义函数注册


    本文简单介绍两种往SQLContext、HiveContext中注册自定义函数方法。

    下边以sqlContext为例,在spark-shell下操作示例:

    scala> sc
    res5: org.apache.spark.SparkContext = org.apache.spark.SparkContext@35d4035f
    scala> sqlContext
    res7: org.apache.spark.sql.SQLContext = org.apache.spark.sql.hive.HiveContext@171b0d3
    scala> val df = sc.parallelize(Seq(("张三", 25), ("李四", 30),("赵六", 27))).toDF("name", "age")
    df: org.apache.spark.sql.DataFrame = [name: string, age: int]
    scala> df.registerTempTable("emp")
    1)外部定义函数:
    scala> def remainWorkYears(age: Int) : Int = {
         |  60 - age
         | }
    remainWorkYears: (age: Int)Int
    scala> sqlContext.udf.register("remainWorkYears", remainWorkYears _)
    res1: org.apache.spark.sql.UserDefinedFunction = UserDefinedFunction(<function1>,IntegerType,List())
    scala> sqlContext.sql("select e.*, remainWorkYears(e.age) as remainedWorkYear from emp e").show
    hiveContext.sql("select e.*, remainWorkYears(e.age) as remainedWorkYear from emp e").show
    +----+---+----------------+
    |name|age|remainedWorkYear|
    +----+---+----------------+
    |  张三| 25|              35|
    |  李四| 30|              30|
    |  赵六| 27|              33|
    +----+---+----------------+
    2)匿名函数:
    scala> sqlContext.udf.register("remainWorkYears_anoymous", (age: Int) => {
         |   60 - age
         | })
    res3: org.apache.spark.sql.UserDefinedFunction = UserDefinedFunction(<function1>,IntegerType,List())
    scala> sqlContext.sql("select e.*, remainWorkYears_anoymous(e.age) as remainedWorkYear from emp e").show
    +----+---+----------------+
    |name|age|remainedWorkYear|
    +----+---+----------------+
    |  张三| 25|              35|
    |  李四| 30|              30|
    |  赵六| 27|              33|
    +----+---+----------------+
  • 相关阅读:
    Window7幻灯片字体显示混乱,难道真的是病毒么
    COCOS2DX 3.0 优化提升渲染速度 Auto-batching
    iOS 打印出视图中全部的子视图的名称
    【linux】学习2
    【编程之美】2.16 求数组的最大递增子序列
    【linux】学习1
    【编程之美】2.15 子数组之和的最大值(二维)
    【编程之美】2.14 求数组的子数组之和的最大值
    【QT】视频播放
    【编程之美】3.5 最短摘要的生成
  • 原文地址:https://www.cnblogs.com/yy3b2007com/p/8110687.html
Copyright © 2020-2023  润新知