Maps the result of an XQuery expression to a Hive primitive value. Each Hive primitive data type has a separate function named for it:
xml_query_as_bigint
xml_query_as_boolean
xml_query_as_double
xml_query_as_float
xml_query_as_int
xml_query_as_smallint
xml_query_as_string
xml_query_as_tinyint
An XQuery or XPath expression.
The input that the query processes. The value can be an XML STRING or a STRUCT of variable values:
STRING: The string is bound to the initial context item of the query as XML. See Example 1.
STRUCT: A STRUCT with an even number of fields. Each pair of fields defines a variable binding (name, value) for the query. The name fields must be type STRING, and the value fields can be any supported primitive. See "About Data Type Conversions" and Example 2.
The first item in the result of the query is cast to the XML schema type that maps to the primitive type of the function. If the query returns multiple items, then all but the first are ignored.
The result of the query as a Hive primitive value
NULL if the query returns an empty result
This example parses and binds the input XML string to the initial context item of the query "x/y":
> SELECT xml_query_as_string("x/y", "<x><y>hello</y></x>") FROM src LIMIT 1;
"hello"
The following are string binding examples that use other primitive functions:
> SELECT xml_query_as_int("x/y", "<x><y>123</y></x>") FROM src LIMIT 1; 123 > SELECT xml_query_as_double("x/y", "<x><y>12.3</y></x>") FROM src LIMIT 1; 12.3 > SELECT xml_query_as_boolean("x/y", "<x><y>true</y></x>") FROM src LIMIT 1; true
In this example, the second argument is a STRUCT that defines two query variables, $data and $value. The values of the variables in the STRUCT are converted to XML schema types as described in "About Data Type Conversions."
> SELECT xml_query_as_string( "fn:parse-xml($data)/x/y[@id = $value]", struct( "data", "<x><y id='1'>hello</y><z/><y id='2'>world</y></x>", "value", 2 ) ) FROM src LIMIT 1; "world"
This example returns only the first item (hello) from the query. The second item (world) is discarded.
> SELECT xml_query_as_string("x/y", "<x><y>hello</y><z/><y>world</y></x>") FROM src LIMIT 1;
"hello"
This example returns NULL because the result of the query is empty:
> SELECT xml_query_as_string("x/foo", "<x><y>hello</y><z/><y>world</y></x>") FROM src LIMIT 1;
NULL
These examples use the fn:serialize function to return complex XML elements as a STRING value:
> SELECT xml_query_as_string("fn:serialize(x/y[1])", "<x><y>hello</y><z/><y>world</y></x>") FROM src LIMIT 1; "<y>hello</y>" > SELECT xml_query_as_string( "fn:serialize(<html><head><title>{$desc}</title></head><body>Name: {$name}</body></html>)", struct( "desc", "Employee Details", "name", "John Doe" ) ) FROM src LIMIT 1; "<html><head><title>Employee Details</title></head><body>Name: John Doe</body></html>"
This example adds a file named test.xml to the distributed cache, and then queries it using the fn:doc function. The file contains the value <x><y>hello</y><z/><y>world</y></x>.
> ADD FILE test.xml; > SELECT xml_query_as_string("fn:doc('test.xml')/x/y[1]", NULL) FROM src LIMIT 1; "hello"
This example returns NULL because </invalid is missing an angle bracket. An XML parsing error is written to the log:
> SELECT xml_query_as_string("x/y", "<x><y>hello</invalid") FROM src LIMIT 1;
NULL
This example returns NULL because foo cannot be cast as xs:float. A cast error is written to the log:
> SELECT xml_query_as_float("x/y", "<x><y>foo</y></x>") FROM src LIMIT 1;
NULL