Got it

Hive:Case 2: UDFs

Latest reply: Aug 29, 2018 09:14:32 608 1 1 0 0

1.1 Case 2: UDFs

1.1.1 Scenarios

Applicable Versions

FusionInsight HD V100R002C70 and FusionInsight HD V100R002C80

Scenario Description

When internal functions of Hive cannot meet requirements, you can compile UDFs and use them for query.

According to implementation methods, UDFs are classified as follows:

l   Common UDFs: used to perform operations on a single data row and export a single data row.

l   User-defined aggregating functions (UDAFs): used to input multiple data rows and export a single data row.

l   User-defined table-generating functions (UDTFs): used to perform operations on a single data row and export multiple data rows.

According to usage methods, UDFs are classified as follows:

l   Temporary functions: used only in the current session and must be recreated after a session restarts.

l   Permanent function: used in multiple sessions and do not have to be created every time a session restarts.

The following uses AddDoublesUDF as an example to describe how to compile and use UDFs.

1.1.2 Development Idea

AddDoublesUDF is used to add two or multiple floating point values. The following example describes how to compile and use UDFs.

note

l  A normal UDF must be originated from org.apache.hadoop.hive.ql.exec.UDF.

l  A normal UDF must implement at least one evaluate() method. The evaluate function supports overloading.

1.1.3 Example Code Description

Example Codes

The following is a UDF example:

package com.huawei.bigdata.hive.example.udf;

import org.apache.hadoop.hive.ql.exec.UDF;

public class AddDoublesUDF extends UDF { 

 public Double evaluate(Double... a) { 

    Double total = 0.0; 

    // Processing logic. 

    for (int i = 0; i < a.length; i++) 

      if (a != null) 

        total += a

    return total; 

  } 

}

How to Use

                               Step 1      Package the preceding program into AddDoublesUDF.jar, and upload it to a directory on the HDFS (such as /user/hive_examples_jars/). The user who creates the UDF and the user who uses the UDF function must have read right on this JAR file. Example statements:

hdfs dfs -put ./hive_examples_jars /user/hive_examples_jars

hdfs dfs -chmod 777 /user/hive_examples_jars

                               Step 2      Log in to the Beeline client as a user with admin user rights and run the following command:

kinit Hive service user

beeline

set role admin;

                               Step 3      Define the function in Hive Server. Run the following SQL statement to create a permanent function:

CREATE FUNCTION addDoubles AS 'com.huawei.bigdata.hive.example.udf.AddDoublesUDF' using jar 'hdfs://hacluster/user/hive_examples_jars/AddDoublesUDF.jar';

addDoubles indicates the function alias that is used for SELECT query.

Run the following statement to create a temporary function:

CREATE TEMPORARY FUNCTION addDoubles AS 'com.huawei.bigdata.hive.example.udf.AddDoublesUDF' using jar 'hdfs://hacluster/user/hive_examples_jars/AddDoublesUDF.jar';

l   addDoubles indicates the function alias that is used for SELECT query.

l   TEMPORARY indicates that the function is used only in the current session with the Hive server

                               Step 4      Run the following SQL statement to use the function in the Hive server:

SELECT addDoubles(1,2,3);

note

If [Error 10011] is displayed when you log in to the client again, run the reload function; command and then use this function.

                               Step 5      Run the following SQL statement to delete the function from the Hive server:

DROP FUNCTION addDoubles;

----End

1.1.4 Obtaining Example Codes

For details about the codes, see Example Codes.

 


This post was last edited by chz at 2018-08-29 10:01.

This article contains more resources

You need to log in to download or view. No account? Register

x

welcome
View more
  • x
  • convention:

Comment

You need to log in to comment to the post Login | Register
Comment

Notice: To protect the legitimate rights and interests of you, the community, and third parties, do not release content that may bring legal risks to all parties, including but are not limited to the following:
  • Politically sensitive content
  • Content concerning pornography, gambling, and drug abuse
  • Content that may disclose or infringe upon others ' commercial secrets, intellectual properties, including trade marks, copyrights, and patents, and personal privacy
Do not share your account and password with others. All operations performed using your account will be regarded as your own actions and all consequences arising therefrom will be borne by you. For details, see " User Agreement."

My Followers

Block
Are you sure to block this user?
Users on your blacklist cannot comment on your post,cannot mention you, cannot send you private messages.
Reminder
Please bind your phone number to obtain invitation bonus.