【问题标题】:Add leading zeros to Columns in a Spark Data Frame [duplicate]将前导零添加到 Spark 数据框中的列 [重复]
【发布时间】:2018-04-26 16:00:28
【问题描述】:

简而言之,我正在利用 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")

我得到的示例输出

+------+---+--------------------+
|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 |
+------+---+--------------------+

期望的输出

+--------+----+--------------------+
|iD      |val |Code                |
+--------+----+--------------------+
|001     |044 |9022070536692784476 |
|002     |066 |-5138930048185086175|
|003     |025 |805582856291361761  |
|004     |017 |-9107885086776983000|
|005     |018 |1993794295881733178 |
|006     |031 |-2867434050463300064|
|007     |088 |-4692317993930338046|
|008     |044 |-4039776869915039812|
|009     |020 |-5786627276152563542|
|0010    |012 |7614363703260494022 |
+--------+----+--------------------+

【问题讨论】:

  • 谢谢,这很有帮助。

标签: scala apache-spark spark-dataframe


【解决方案1】:

这已经为我解决了,谢谢大家的帮助

 val df2 = df
        .withColumn("idLong", format_string("%03d", $"iD"))

【讨论】:

  • 完美!谢谢。供那些在我之后访问的人参考这里是format_string的文档链接
【解决方案2】:

您可以通过使用concat 内置函数来简单地做到这一点

df.withColumn("iD", concat(lit("00"), col("iD")))
           .withColumn("val", concat(lit("0"), col("val")))

【讨论】:

  • 谢谢,成功了。我也尝试了另一种方式,我发布了它
  • 那真是太好了 :) 感谢您的接受
猜你喜欢
  • 1970-01-01
  • 2020-06-07
  • 1970-01-01
  • 1970-01-01
  • 2014-03-04
  • 2017-06-16
  • 1970-01-01
  • 2018-12-21
  • 2016-11-24
相关资源
最近更新 更多