Hive学习四

Hive学习四

标签(空格分隔): Hive

  • Hive学习四
    • 一Hive中UDF编程案例1
    • 二Hive中UDF编程案例2去除字段的双引号

一,Hive中UDF编程–案例1

1, eclipse增加pom.xml依赖

<dependency>
    <groupId>org.apache.hive</groupId>
    <artifactId>hive-jdbc</artifactId>
    <version>0.13.1</version>
</dependency>
<dependency>
    <groupId>org.apache.hive</groupId>
    <artifactId>hive-exec</artifactId>
    <version>0.13.1</version>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>3.8.1</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-client</artifactId>
    <version>2.5.0</version>
</dependency>

2,编写代码:

package org.apache.hadoop.hiveudf;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;
import org.apache.commons.lang.StringUtils;

public class LowerUdf extends UDF{
    public Text evaluate(Text inputString){
        if(null==inputString){
            return null;
        }
        if(StringUtils.isBlank(inputString.toString())){
            return null;
        }
        String lowerString=inputString.toString().toLowerCase();
        Text output=new Text(lowerString);

        return output;
    }
    //test
    public static void main(String[] args){
        Text test=new Text("HELLO");
        System.out.println(new LowerUdf().evaluate(test));
    } 
}

2,导出并加载jar包 注册函数

add jar /home/hivelower.jar;
create temporary function my_lower as 'org.apache.hadoop.hiveudf';

3,测试

select * from my_lower(ename) from emp;

二,Hive中UDF编程–案例2–去除字段的双引号

1, eclipse增加pom.xml依赖

<dependency>
    <groupId>org.apache.hive</groupId>
    <artifactId>hive-jdbc</artifactId>
    <version>0.13.1</version>
</dependency>
<dependency>
    <groupId>org.apache.hive</groupId>
    <artifactId>hive-exec</artifactId>
    <version>0.13.1</version>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>3.8.1</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-client</artifactId>
    <version>2.5.0</version>
</dependency>

2,编写代码:

package org.apache.hadoop.Workudf;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;
import org.apache.commons.lang.StringUtils;

public class Workudf extends UDF{
    public Text evaluate(Text inputString){
        if(null==inputString){
            return null;
        }
        if(StringUtils.isBlank(inputString.toString())){
            return null;
        }
        String newString=inputString.toString().replace("\"","");
        Text output=new Text(newString);

        return output;
    }
    //test
    public static void main(String[] args){
        Text test=new Text("\"HELLO\"");
        System.out.println(new Workudf().evaluate(test));
    } 
}

2,导出并加载jar包 注册函数

add jar /home/Workudf.jar;
create temporary function removequote as 'org.apache.hadoop.Workudf';

3,测试

select * from removequote(ename) from emp;

你可能感兴趣的:(hive)