简体   繁体   English

如何将Spark数据框写入xml文件?

[英]How to write Spark data frame to xml file?

Sample : 样品:

scala> Frame.show()

|year| make|model|             comment|blank|
|2012|Tesla|    S|          No comment|    R|
|1997| Ford| E350|Go get one now th...|    L|
|2015|Chevy| Volt|                 Try|    M|

to

<item>
    <'year'>2012<'/year'>
    <'make'>Tesla<'/make'>
    <'model'>S<'/mode'>
</item>

The simplest approach is to use XML writer from spark-xml : 最简单的方法是使用spark-xml

val path: String = ???
df.write.format("com.databricks.spark.xml")
  .option("rootTag", "items")
  .option("rowTag", "item")
  .save(path)

If for some reason it doesn't fit your needs you can dump records individually and saveAsTextFile : 如果由于某种原因它不符合您的需求,您可以单独转储记录并saveAsTextFile

def dumpXML(row: Row): String = ???
df.rdd.map(dumpXML).saveAsTextFile(path)

You can add root element using for example mapPartitions . 您可以使用例如mapPartitions添加根元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将Spark数据框中的每个分区记录写入xml文件 - Write records per partition in spark data frame to a xml file 如何在Spark SCALA中重命名AWS中的spark数据框输出文件 - How to rename spark data frame output file in AWS in spark SCALA 如何使用 Spark/Scala 将这样的嵌套多行 json 文件读入数据帧 - How to read such a nested multiline json file into a data frame with Spark/Scala Spark 如何使用同步 API 从数据写入镶木地板文件 - Spark How to write to parquet file from data using synchronous API 如何为所有列编写withColumnRenamed并在Spark数据帧的自定义分区中加入两个不同的架构 - How to write withColumnRenamed for all columns and join two different schema in custom partition in spark data frame 如何只将空数据帧标头写入csv文件? - How to write empty data frame headers only to csv file? 对于基本数据框创建示例,我应该如何在Spark中编写单元测试? - How should I write unit tests in Spark, for a basic data frame creation example? 火花PSV文件到数据帧的转换错误 - spark psv file to data frame conversion error 如何以csv格式保存Spark XML中的数组数据帧输出 - How to save array data frame output from spark xml in csv format 如何扁平化Apache Spark中的数据框 斯卡拉 - How to flatten a data frame in apache spark | Scala
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM