Book Image

Apache Hive Cookbook

Book Image

Apache Hive Cookbook

Overview of this book

Hive was developed by Facebook and later open sourced in Apache community. Hive provides SQL like interface to run queries on Big Data frameworks. Hive provides SQL like syntax also called as HiveQL that includes all SQL capabilities like analytical functions which are the need of the hour in today’s Big Data world. This book provides you easy installation steps with different types of metastores supported by Hive. This book has simple and easy to learn recipes for configuring Hive clients and services. You would also learn different Hive optimizations including Partitions and Bucketing. The book also covers the source code explanation of latest Hive version. Hive Query Language is being used by other frameworks including spark. Towards the end you will cover integration of Hive with these frameworks.
Table of Contents (19 chapters)
Apache Hive Cookbook
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Preface
Index

Creating custom User-Defined Functions (UDF)


Built-in functions in Hive sometimes do not fit the requirements of a business use case or when data analytics required some custom manipulation of data based on certain conditions. For such cases, the user needs to define custom logic as a UDF and run it over the data.

How to do it…

For writing a custom function in Hive, you will have to extend a Hive class: org.apache.hadoop.hive.ql.exec.UDF.

Let's understand the concept of creating a custom UDF with the example of creating a function to reverse a string.

The following are the steps to create a custom UDF:

  1. Create a new Java project using any IDE, such as Eclipse. Give this any name; let's say "HiveUDF".

  2. When writing a custom UDF, there should be two libraries in the classpath of the project. To do so:

    1. Create a folder "lib" under the project.

    2. Add the following two JAR files to the lib folder:

      • hadoop-common-2.6.0

      • hive-exec-1.2.1

    3. Add these JAR files to the classpath of the project. Right-click on Project...