0 votes
1 view
in Big Data Hadoop & Spark by (19k points)

Scenario: My Input will be multiple small XMLs and am Supposed to read these XMLs as RDDs. Perform join with another dataset and form an RDD and send the output as an XML.

Is it possible to read XML using spark, load the data as RDD? If it is possible how will the XML be read.

Sample XML:















How will this be loaded into the RDD?

1 Answer

0 votes
by (33.2k points)

You can perform XML processing in Spark, but you need different dependencies for that.

If your file is small, then the simplest solution is to load the data using SparkContext.wholeTextFiles. It will load data as RDD, where the first element should be path and second should be file content.

But for larger files, you can use Hadoop input formats.

If the structure is simple, then you can split records using textinputformat.record.delimiter. Input is not an XML but it should give you an idea of how to proceed.

Mahout also provides XmlInputFormat

Finally, it is possible to read the file using SparkContext.textFile and adjust later for record spanning between partitions. 

Simply use mapPartitionsWithIndex partitions to identify records broken between partitions, collect broken records.

You can use the second mapPartitionsWithIndex to repair broken records

There is a relatively new spark-XML package which allows you to extract specific records by tag:

val df = sqlContext.read


   .option("rowTag", "foo")


Hope this answer helps you!

Welcome to Intellipaat Community. Get your technical queries answered by top developers !