小编fle*_*chr的帖子

将前导零添加到Spark数据框中的列

简而言之,我正在利用spark-xml进行XML文件的一些解析。但是,使用此方法会删除我感兴趣的所有值中的前导零。但是,我需要最终输出(即DataFrame)包括前导零。我不确定/无法找到将前导零添加到我感兴趣的列的方法。

val df = spark.read
  .format("com.databricks.spark.xml")
  .option("rowTag", "output")
  .option("excludeAttribute", true)
  .option("allowNumericLeadingZeros", true) //including this does not solve the problem
  .load("pathToXmlFile")
Run Code Online (Sandbox Code Playgroud)

我得到的示例输出

+------+---+--------------------+
|iD    |val|Code                |
+------+---+--------------------+
|1     |44 |9022070536692784476 |
|2     |66 |-5138930048185086175|
|3     |25 |805582856291361761  |
|4     |17 |-9107885086776983000|
|5     |18 |1993794295881733178 |
|6     |31 |-2867434050463300064|
|7     |88 |-4692317993930338046|
|8     |44 |-4039776869915039812|
|9     |20 |-5786627276152563542|
|10    |12 |7614363703260494022 |
+------+---+--------------------+
Run Code Online (Sandbox Code Playgroud)

所需的输出

+--------+----+--------------------+
|iD      |val |Code                |
+--------+----+--------------------+
|001     |044 |9022070536692784476 |
|002     |066 |-5138930048185086175|
|003     |025 |805582856291361761  |
|004 …
Run Code Online (Sandbox Code Playgroud)

scala apache-spark spark-dataframe

2
推荐指数
2
解决办法
4864
查看次数

选择DataFrame中数组的最后一个元素

我正在一个项目上,正在处理具有复杂模式/数据结构的一些嵌套JSON日期。基本上,我想做的是在数据框中过滤掉其中的一列,以便选择数组中的最后一个元素。我完全坚持如何做到这一点。我希望这是有道理的。

以下是我要完成的示例:

val singersDF = Seq(
  ("beatles", "help,hey,jude"),
  ("romeo", "eres,mia"),
  ("elvis", "this,is,an,example")
).toDF("name", "hit_songs")

val actualDF = singersDF.withColumn(
  "hit_songs",
  split(col("hit_songs"), "\\,")
)

actualDF.show(false)
actualDF.printSchema() 

+-------+-----------------------+
|name   |hit_songs              |
+-------+-----------------------+
|beatles|[help, hey, jude]      |
|romeo  |[eres, mia]            |
|elvis  |[this, is, an, example]|
+-------+-----------------------+
root
 |-- name: string (nullable = true)
 |-- hit_songs: array (nullable = true)
 |    |-- element: string (containsNull = true)
Run Code Online (Sandbox Code Playgroud)

输出的最终目标将是以下内容,以选择hit_songs数组中的最后一个“字符串”。

我不担心之后的架构是什么样的。

+-------+---------+
|name   |hit_songs|
+-------+---------+
|beatles|jude     |
|romeo  |mia      |
|elvis  |example  |
+-------+---------+
Run Code Online (Sandbox Code Playgroud)

scala apache-spark apache-spark-sql

1
推荐指数
3
解决办法
2184
查看次数