将RDD[Elem]保存到XML文件
我有一个Elem类型的RDD:将RDD[Elem]保存到XML文件,xml,scala,rdd,Xml,Scala,Rdd,我有一个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> <
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>
请注意,
.saveAsTextFile()
不是我要找的。通过将RDD[Elem]
转换为列表[Elem]
解决了这个问题:
val clientXmlList: List[Elem] = for (address <- clientXml.collect().toSeq.toList) yield {
address
}
然后使用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()
通过将
RDD[Elem]
转换为列表[Elem]
,解决了这个问题:
val clientXmlList: List[Elem] = for (address <- clientXml.collect().toSeq.toList) yield {
address
}
然后使用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()
// 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()