Skip to content
Closed
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 91 additions & 1 deletion docs/sql-ref-functions-udf-hive.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,94 @@ license: |
limitations under the License.
---

Integration with Hive UDFs/UDAFs/UDTFs
### Description

Spark SQL supports integration of Hive UDFs, UDAFs and UDTFs. Similar to Spark UDFs and UDAFs, Hive UDFs work on a single row as input and generate a single row as output, while Hive UDAFs operate on multiple rows and return a single aggregated row as a result. In addition, Hive also supports UDTFs (User Defined Tabular Functions) that act on one row as input and return multiple rows as output. To use Hive UDFs/UDAFs/UTFs, the user should register them in Spark, and then use them in Spark SQL queries.

### Examples

Hive has two UDF interfaces: [UDF](https://github.com/apache/hive/blob/master/udf/src/java/org/apache/hadoop/hive/ql/exec/UDF.java) and [GenericUDF](https://github.com/apache/hive/blob/master/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDF.java).
An example below uses [GenericUDFAbs](https://github.com/apache/hive/blob/master/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFAbs.java) derived from `GenericUDF`.

<pre><code>
// Register `GenericUDFAbs` and use it in Spark SQL.
// Note that, if you use your own programmed one, you need to add a JAR containig it into a classpath,
// e.g., `spark.sql("ADD JAR yourHiveUDF.jar")`.
spark.sql("CREATE TEMPORARY FUNCTION testUDF AS 'org.apache.hadoop.hive.ql.udf.generic.GenericUDFAbs'")

spark.sql("SELECT * FROM hiveUDFTestTable").show()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ur, my bad. nit: hiveUDFTestTable -> t.
btw, any reason to write this doc by Scala? Could we follow the SQL format here, too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. Will convert to SQL

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea, thanks!

// +-----+
// |value|
// +-----+
// | -1.0|
// | 2.0|
// | -3.0|
// +-----+

spark.sql("SELECT testUDF(value) FROM t").show()
// +--------------+
// |testUDF(value)|
// +--------------+
// | 1.0|
// | 2.0|
// | 3.0|
// +--------------+
</code></pre>

An example below uses [GenericUDTFExplode](https://github.com/apache/hive/blob/master/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDTFExplode.java) derived from [GenericUDTF](https://github.com/apache/hive/blob/master/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDF.java).

<pre><code>
// Register `GenericUDTFExplode` and use it in Spark SQL
spark.sql(
"""
|CREATE TEMPORARY FUNCTION hiveUDTF
| AS 'org.apache.hadoop.hive.ql.udf.generic.GenericUDTFExplode'
""".stripMargin)

spark.sql("SELECT * FROM t").show()
// +------+
// | value|
// +------+
// |[1, 2]|
// |[3, 4]|
// +------+

spark.sql("SELECT hiveUDTF(value) FROM t").show()
// +---+
// |col|
// +---+
// | 1|
// | 2|
// | 3|
// | 4|
// +---+
</code></pre>

Hive has two UDAF interfaces: [UDAF](https://github.com/apache/hive/blob/master/udf/src/java/org/apache/hadoop/hive/ql/exec/UDAF.java) and [GenericUDAFResolver](https://github.com/apache/hive/blob/master/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDAFResolver.java).
An example below uses [GenericUDAFSum](https://github.com/apache/hive/blob/master/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDAFSum.java) derived from `GenericUDAFResolver`.

<pre><code>
// Register `GenericUDAFSum` and use it in Spark SQL
spark.sql(
"""
|CREATE TEMPORARY FUNCTION hiveUDAF
| AS 'org.apache.hadoop.hive.ql.udf.generic.GenericUDAFSum'
""".stripMargin)

spark.sql("SELECT * FROM t").show()
// +---+-----+
// |key|value|
// +---+-----+
// | a| 1|
// | a| 2|
// | b| 3|
// +---+-----+

spark.sql("SELECT key, hiveUDAF(value) FROM t GROUP BY key").show()
// +---+---------------+
// |key|hiveUDAF(value)|
// +---+---------------+
// | b| 3|
// | a| 3|
// +---+---------------+
</code></pre>