Hive UDF 环境搭建(Eclipse+Maven)

Posted on 2018-05-18 17:38:00

Hive UDF 环境搭建(Eclipse+Maven)

  1. 安装Maven (https://blog.csdn.net/rav009/article/details/79469303)
  2. 安装Eclipse
  3. 安装Eclipse的Maven插件 m2e

使用Eclipse创建Maven项目

Group ID一般是org.yourname.projectname, Group ID会变成你代码中类的前缀

Artifact ID是Projectname, 就是项目名称

创建项目后找到pom.xml, 在dependencies节点里添加:

    <dependency>
        <groupId>org.apache.hive</groupId>
        <artifactId>hive-exec</artifactId>
        <version>2.3.2</version>
    </dependency>

版本号根据hive的情况修改, 我写这篇文章的时候 hive已经有2.3.3了

来到项目目录下, 这个目录里应该有pom.xml, 运行命令行

mvn install

如果命令行报错 ,说某个jar包 invalid LOC header (bad signature), 就去repository里删掉这个jar包的文件夹,重新运行命令行, 会自动重新下载. 对于ubuntu来说repository在~/.m2

在src/main/java下添加新文件HelloWorld.java,代码如下:

package cn.pywei.HiveUDF;

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

@Description(name="HelloWorld",value="_FUNC_(input), return the string \"HelloWorld\".",extended ="E.g. \n select hello(1);")


public class HelloWorld extends UDF {


    public String evaluate(String s) {
        return "HelloWorld";
    }
    
    public String evaluate(int s) {
        return "HelloWorld";
    }
    
    public String evaluate(boolean s) {
        return "HelloWorld";
    }
}

export成jar文件

在Hive中导入jar文件:

add jar /path/name.jar;

在Hive中创建临时函数:

create temporary function hello as 'cn.pywei.HiveUDF.HelloWorld';

执行:

select hello(1);
select hello('abc');
select hello(True);
describe function hello;
describe function extended hello;

此外还可以用以下命令操作jar包:

list jar;
delete jar /path/name.jar;
delete jar; --delete all jar;

maven的一些小问题:

Import cannot be resolved in eclispe project using maven?

Go to Project => check Build automatically and Clean.

If this doesn't solve the problem..

Right click the "Maven Dependencies" => "Build Path" => "Remove from the build path";
Right click the project, go to "Maven" => "Update project";

转自: https://stackoverflow.com/questions/29206772/import-cannot-be-resolved-in-eclispe-project-using-maven

Pom.xml中scope项 compile 和 provided 的区别?

Dependency Scope

Dependency scope is used to limit the transitivity of a dependency, and also to affect the classpath used for various build tasks.

There are 6 scopes available:

转自: https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

参考链接:

https://blog.csdn.net/u010376788/article/details/50532166

https://www.jianshu.com/p/7ebc8f9c9b78

http://www.crazyant.net/2160.html