SparkSQL---UDF(java)

package com.spark.sparksql.udf

import org.apache.spark.sql.types.{StringType, StructField, StructType}
import org.apache.spark.sql.{Row, SQLContext}
import org.apache.spark.{SparkConf, SparkContext}

/**
  * Created by Administrator on 2017/08/11.
  */
object UDF {
  def main(args: Array[String]) {
    val conf = new SparkConf().setAppName("UDF").setMaster("local")
    val sc = new SparkContext(conf)
    val sqlcontext = new SQLContext(sc)
    val names = Array("yyy-01","yyy-02","yyy-03","yyy-04")
    val namesRDD = sc.parallelize(names ,4)
    val namesRowRDD = namesRDD.map(name => Row(name))
    val struceType = StructType(Array(StructField("name",StringType,true)))
    val namesDF = sqlcontext.createDataFrame(namesRowRDD,struceType)
    namesDF.registerTempTable("names")
    sqlcontext.udf.register("strLength",(str:String)=>str.length)
    sqlcontext.sql("select name,strLength(name) from names").collect().foreach(f => println())
  }
}

你可能感兴趣的:(SparkSQL---UDF(java))