【问题标题】:saving RDD[Elem] to an XML file将 RDD[Elem] 保存到 XML 文件
【发布时间】:2017-08-12 04:32:05
【问题描述】:

我有一个 Elem 类型的 RDD:

val clientXml: RDD[Elem] = parsedClient.filter(s => s.isSuccess).map(s => convertToXML.clientToXML(s.get))

这个 RDD 包含一个 Elem 类型元素的集合,每个元素看起来像这样:

<client>
  <first>Alexandra</first>
  <last>Diaz</last>
  <title></title>
  <addresses>
    <address>
      <type>Home</type>
      <addr1>3255 Marsh Elder</addr1>
      <addr2></addr2>
      <city>La Jolla</city>
      <province>CA </province>
      <county>United States</county>
    </address>
  </addresses>
</client>

我想将整个RDD保存到一个XML文件中,格式如下:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>.
    <client>
      <first>Alexandra</first>
      <last>Diaz</last>
      <title></title>
      <addresses>
        <address>
          <type>Home</type>
          <addr1>3255 Marsh Elder</addr1>
          <addr2></addr2>
          <city>La Jolla</city>
          <province>CA </province>
          <county>United States</county>
        </address>
      </addresses>
    </client>

到目前为止,我已经设法使用以下方法保存了一个元素。但我需要将所有元素保存在一个文件中:

val clientElem: Elem = clientXml.treeReduce((a,b) => a) 

XML.save("C:/Temp/Client.xml", clientElem.copy(), "UTF-8", true)

请注意,.saveAsTextFile() 不是我要找的。​​p>

【问题讨论】:

    标签: xml scala rdd


    【解决方案1】:

    通过将RDD[Elem] 转换为List[Elem] 来解决它:

    val clientXmlList: List[Elem] = for (address <- clientXml.collect().toSeq.toList) yield {
          address
        }
    

    然后创建了一个数据节点,其中 List[Elem] 中的元素嵌入在 Elem 中:

    val clientXmlElemData: Elem = <data>
      {clientXmlList.map(p => p.copy())}
    </data>
    

    然后使用 XML.write() 方法写入 XML 文件:

    // create a null DocType so that the docType is not inserted to the output XML file
    val doctype = null
    
    // create a FileWriter which writes to a file "C:/Temp/Client.xml"
    val file = new File("C:/Temp/Client.xml")
    
    // create a BufferedWriter to write to the file "C:/Temp/Client.xml"
    val bw = new BufferedWriter(new FileWriter(file))
    
    // write the clientXmlElemData node to the file setting write xml declaration to true
    XML.write(bw, clientXmlElemData, "UTF-8", true, doctype)
    
    // close the BufferedWriter after the file has been created
    bw.close()
    

    【讨论】:

      猜你喜欢
      • 2011-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-10
      相关资源
      最近更新 更多