Apache Pig is a tool used to analyze large amounts of data by representing them as data flows. Using the Pig Latin scripting language operations like ETL (Extract, Transform and Load), adhoc data analysis and iterative processing can be easily achieved. Pig is an abstraction over MapReduce. In other words, all Pig scripts internally are converted into Map and Reduce tasks to get the task done. Pig was built to make programming MapReduce applications easier. Before Pig, Java was the only way to process the data stored on HDFS. Pig was first built in Yahoo! and later became a top level Apache project.
Feeding the Pig with XML Its always tough to parse XML, especially when it comes to PIG. Here I am explaining two approaches to parse an XML file in PIG.
For simplicity I am taking a sample XML as shown below. This file should be in HDFS for processing.
<CATALOG> <BOOK> <TITLE>Hadoop Defnitive Guide</TITLE> <AUTHOR>Tom White</AUTHOR> <COUNTRY>US</COUNTRY> <COMPANY>CLOUDERA</COMPANY> <PRICE>24.90</PRICE> <YEAR>2012</YEAR> </BOOK> <BOOK> <TITLE>Programming Pig</TITLE> <AUTHOR>Alan Gates</AUTHOR> <COUNTRY>USA</COUNTRY> <COMPANY>Horton Works</COMPANY> <PRICE>30.90</PRICE> <YEAR>2013</YEAR> </BOOK> </CATALOG>
Using Regular Expressions
I am using the XMLLoader() in piggy bank UDF to load the xml, so ensure that Piggy Bank UDF is registered. Then I am using regular expression to parse the XML.
REGISTER piggybank.jar A = LOAD 'xmls/hadoop_books.xml' using org.apache.pig.piggybank.storage.XMLLoader('BOOK') as (x:chararray); B = foreach A GENERATE FLATTEN(REGEX_EXTRACT_ALL(x,'<BOOK>\\s*<TITLE>(.*)</TITLE>\\s*<AUTHOR>(.*)</AUTHOR>\\s*<COUNTRY>(.*)</COUNTRY>\\s*<COMPANY>(.*)</COMPANY>\\s*<PRICE>(.*)</PRICE>\\s*<YEAR>(.*)</YEAR>\\s*</BOOK>')); dump B;
(Hadoop Defnitive Guide,Tom White,US,CLOUDERA,24.90,2012) (Programming Pig,Alan Gates,USA,Horton Works,30.90,2013)
XPath is a function that allows text extraction from xml. Starting PIG 0.13 , Piggy bank UDF comes with XPath support. It eases the XML parsing in PIG scripts. A sample script using XPath is as shown below.
REGISTER piggybank.jar DEFINE XPath org.apache.pig.piggybank.evaluation.xml.XPath(); A = LOAD 'xmls/hadoop_books.xml' using org.apache.pig.piggybank.storage.XMLLoader('BOOK') as (x:chararray); B = FOREACH A GENERATE XPath(x, 'BOOK/AUTHOR'), XPath(x, 'BOOK/PRICE'); dump B;
(Tom White,24.90) (Alan Gates,30.90)
If you check the XPath UDF source code , you can see that input to Xpath is a Tuple and it always returns a String. But there will be cases in which we might want XPath to return a Tuple instead of String. So there is scope to extend XPath with this feature and contribute to community.
Now start feeding the Pig !