-
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 all 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,90 @@ 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`. | ||
|
|
||
| {% highlight sql %} | ||
| -- 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., ADD JAR yourHiveUDF.jar; | ||
| CREATE TEMPORARY FUNCTION testUDF AS 'org.apache.hadoop.hive.ql.udf.generic.GenericUDFAbs'; | ||
|
|
||
| SELECT * FROM t; | ||
| +-----+ | ||
| |value| | ||
| +-----+ | ||
| | -1.0| | ||
| | 2.0| | ||
| | -3.0| | ||
| +-----+ | ||
|
|
||
| SELECT testUDF(value) FROM t; | ||
| +--------------+ | ||
| |testUDF(value)| | ||
| +--------------+ | ||
| | 1.0| | ||
| | 2.0| | ||
| | 3.0| | ||
| +--------------+ | ||
| {% endhighlight %} | ||
|
|
||
|
|
||
| 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). | ||
|
|
||
| {% highlight sql %} | ||
| -- Register `GenericUDTFExplode` and use it in Spark SQL | ||
| CREATE TEMPORARY FUNCTION hiveUDTF | ||
| AS 'org.apache.hadoop.hive.ql.udf.generic.GenericUDTFExplode'; | ||
|
|
||
| SELECT * FROM t; | ||
| +------+ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. quick question. Why did we use: format over the Hive string format (which is produced by
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, seems like we should comment these output out.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm.. I see. I double checked other references such as https://docs.snowflake.com/en/sql-reference/constructs/join.html, https://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_10002.htm, https://www.postgresql.org/docs/10/sql-select.html. Looks they don't add leading two spaces at least(?). I don't have a strong opinion on this yet. Can we at least remove leading two spaces?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Not sure to comment out the output or not. In SQL syntax section, we didn't comment out any of the output. But in the UDAF SQL example, I commented out the output to be consistent with the scala and java examples.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea, removing the spaces looks fine. I personally think the most important thing is just to keep the almost same format over the documents. So, I think we can update each rule in the current format if we have a better one. Anyway, thanks for the check, @HyukjinKwon
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, thank you guys. It's not urgent but let's remove the two leading spaces. I think that looks more consistent with other references at least. |
||
| | value| | ||
| +------+ | ||
| |[1, 2]| | ||
| |[3, 4]| | ||
| +------+ | ||
|
|
||
| SELECT hiveUDTF(value) FROM t; | ||
| +---+ | ||
| |col| | ||
| +---+ | ||
| | 1| | ||
| | 2| | ||
| | 3| | ||
| | 4| | ||
| +---+ | ||
| {% endhighlight %} | ||
|
|
||
| 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`. | ||
|
|
||
| {% highlight sql %} | ||
| -- Register `GenericUDAFSum` and use it in Spark SQL | ||
| CREATE TEMPORARY FUNCTION hiveUDAF | ||
| AS 'org.apache.hadoop.hive.ql.udf.generic.GenericUDAFSum'; | ||
|
|
||
| SELECT * FROM t; | ||
| +---+-----+ | ||
| |key|value| | ||
| +---+-----+ | ||
| | a| 1| | ||
| | a| 2| | ||
| | b| 3| | ||
| +---+-----+ | ||
|
|
||
| SELECT key, hiveUDAF(value) FROM t GROUP BY key; | ||
| +---+---------------+ | ||
| |key|hiveUDAF(value)| | ||
| +---+---------------+ | ||
| | b| 3| | ||
| | a| 3| | ||
| +---+---------------+ | ||
| {% endhighlight %} | ||
Uh oh!
There was an error while loading. Please reload this page.