2017-08-11 68 views
0

我有型ELEM的RDD:節約RDD [ELEM]到一個XML文件

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()是不是我要找的。

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

然後創建與嵌入在ElemList[Elem]元素的數據節點:

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

然後使用XML.write

回答

0

通過將RDD[Elem]List[Elem]解決它()方法寫入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() 
相關問題