如何在scala spark中添加指定位数的前导零填充?

Joh*_*ung 4 scala apache-spark-sql

我有data.txt如下文件。

12, 345, 6789
Run Code Online (Sandbox Code Playgroud)

现在,我想在参数文件或标准输入的指定字段中使用指定的位数执行前导零填充。自变量文件的指定字段中指定的位数为 8 位数。我该怎么办?

这是我的代码:

import org.apache.spark.sql.types._  
import org.apache.spark.sql.types._
import org.apache.spark.SparkConf
import org.apache.spark.SparkContext
import org.apache.spark.SparkContext._
import org.apache.spark.sql._

//Convert textfile to DF
val conf = new SparkConf().setAppName("ct").setMaster("local").set("spark.driver.allowMultipleContexts", "true")
val sc = SparkContext(conf)
val sparkSess = SparkSession.builder().appName("SparkSessionZipsExample").config(conf).getOrCreate()
val path = "data.txt"
val data = sc.textFile(path)
val colNum = data.first().split(",").size
var schemaString = "key"
for( i <- 1 to colNum - 1) {
 schemaString += " value" + i
}
val fields = schemaString.split(" ").map(fieldName => StructField(fieldName, StringType, nullable=true))
val schema = StructType(fields)
val dfWithSchema = sparkSess.read.option("header", "false").schema(schema).csv(path)
dfWithSchema.show()

//add leading zero padding with the specified number of digits
//The number of digits specified in the specified field of the argument file is 8 digits
val df = dfWithSchema.withColumn("key", format_string("%08d", $"key")).show
val df2 = dfWithSchema.withColumn("value2", format_string("%08d", $"value2")).show
Run Code Online (Sandbox Code Playgroud)

但输出结果不正确。

我想获得如下所需的输出结果。请帮我。

+---------+------+---------+
|key      |value1|value2   |
+---------+------+---------+
| 00000012|   345| 00006789|
+---------+------+---------+
Run Code Online (Sandbox Code Playgroud)

abi*_*sis 6

您可以使用内置lpad函数,如下所示:

import org.apache.spark.sql.functions.lpad

dfWithSchema.select(
  lpad($"key", 8, "0", 
  lpad($"value2", 8, "0"),
  $"value1"
).show
Run Code Online (Sandbox Code Playgroud)

这将在字符串的前面插入最多 8 个字符的 0。

详情请参阅此处