I Have an HBase table, which has the Row Key and CF:CQ that holds the base64 encoded xml
I created a Hive table using the HBase Handler with two columns rowdy and xml_string.
Now I created a view in Hive with rowkey and unbase64(xml_string). The xml_string is nested structure.
Now I need to extract attributes from xml_string along with the rowkey reference. Can someone help me with that. All I am seeing are the examples of reading xml files using HiveXMLSerde [com.ibm.spss.hive.serde2.xml.XmlSerDe] none for parsing an existing XML string inside a hive table / view.
Information about the UDF (from the link I provided):
The xpath family of UDFs are wrappers around the Java XPath library
javax.xml.xpath provided by the JDK. The library is based on the XPath 1.0 specification. Please refer tohttp://java.sun.com/javase/6/docs/api/javax/xml/xpath/package-summary.html for detailed information on the Java XPath library.
All functions follow the form:
xpath_*(xml_string, xpath_expression_string). The XPath expression string is compiled and cached. It is reused if the expression in the next input row matches the previous. Otherwise, it is recompiled. So, the xml string is always parsed for every input row, but the xpath expression is precompiled and reused for the vast majority of use cases.
Backward axes are supported. For example:
Each function returns a specific Hive type given the XPath expression:
xpathreturns a Hive array of strings.
xpath_stringreturns a string.
xpath_booleanreturns a boolean.
xpath_shortreturns a short integer.
xpath_intreturns an integer.
xpath_longreturns a long integer.
xpath_floatreturns a floating point number.
xpath_double,xpath_numberreturns a double-precision floating point number (
xpath_numberis an alias for
The UDFs are schema agnostic - no XML validation is performed. However, malformed xml (e.g.,
<a><b>1</b></aa>) will result in a runtime exception being thrown.
Following are specifics on each xpath UDF variant.
xpath() function always returns a hive array of strings. If the expression results in a non-text value (e.g., another xml node) the function will return an empty array. There are 2 primary uses for this function: to get a list of node text values or to get a list of attribute values.
Non-matching XPath expression:
Get a list of node text values:
Get a list of values for attribute 'id':
Get a list of node texts for nodes where the 'class' attribute equals 'bb':
xpath_string() function returns the text of the first matching node.
Get the text for node 'a/b':
Get the text for node 'a'. Because 'a' has children nodes with text, the result is a composite of text from the children.
Non-matching expression returns an empty string:
Gets the text of the first node that matches '//b':
Gets the second matching node:
Gets the text from the first node that has an attribute 'id' with value 'b_2':
Returns true if the XPath expression evaluates to true, or if a matching node is found.
No match found:
No match found:
These functions return an integer numeric value, or the value zero if no match is found, or a match is found but the value is non-numeric. Mathematical operations are supported. In cases where the value overflows the return type, then the maximum value for the type is returned.
Similar to xpath_short, xpath_int and xpath_long but with floating point semantics. Non-matches result in zero. However,
non-numeric matches result in NaN. Note that
xpath_number() is an alias for
A very large number:
Thank You Sunile.
I have explored the same based on information available on apache.hive site.
My bad, I may not have provided the detail on what I am trying to do. The XML String I am trying to parse is a nested string and second part is I am trying to create multiple records based on number of times each node repeats with proper associations to child elements.
I have a situation here, my xml is having multiple nodes with same tag hence I have used xpath(tags/tag/innertag/) and it is returning array of a string now I have to use the multiple explode function - which is creating problem as I'm getting Cartesian product values :( I should get
but I'm getting
This is the reason my hive table data increased unnecessary and also I'm not getting correct results. Any UDF of other approach which may help me please advice.