-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-31331][SQL][DOCS] Document Spark integration with Hive UDFs/UDAFs/UDTFs #28104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
177a01a
3901cf6
100cea4
52269f2
bc87b37
207cae8
946e417
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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() | ||
|
||
| // +-----+ | ||
| // |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> | ||
Uh oh!
There was an error while loading. Please reload this page.