需求:员工id正常为8位,对于不满8位的员工id左侧用0补齐
import org.apache.spark.sql.{DataFrame, SparkSession}
object DataSetCreate {
def main(args: Array[String]): Unit = {
val spark = SparkSession
.builder()
.appName("test")
.master("local[4]")
.getOrCreate()
import spark.implicits._
val list = List(
("1001","zhangsan",10),
("44","zhang2",66),
("32432","zhang3",70),
("6757","zhang4",22),
("1","zhang4",22))
val df: DataFrame = list.toDF("id","name","age")
//需求:员工id正常为8位,对于不满8位的员工id左侧用0补齐
//println(myLpad("h",8,"0"))
spark.udf.register("xxx",myLpad _);
df.selectExpr("xxx(id,8,'0') id","name","age").show()
df.createOrReplaceTempView("data")
spark.sql(
"""
|select
|xxx(id,8,'0') id,
|name,
|age
|from data
|""".stripMargin).show()
}
/**
*
* @param str 待补齐的字符串
* @param len 最终补齐的长度
* @param pad 使用pad填充
* @return
*/
def myLpad(str:String,len:Int,pad:String):String={
//字符串长度
val strLen: Int = str.length
//需要补齐的长度
val padLen: Int = len-strLen
s"${pad * padLen}${str}"
}
}