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:

  • compile
    This is the default scope, used if none is specified. Compile dependencies are available in all classpaths of a project. Furthermore, those dependencies are propagated to dependent projects.
  • provided
    This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.
  • runtime
    This scope indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath.
  • test
    This scope indicates that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases. This scope is not transitive.
  • system
    This scope is similar to provided except that you have to provide the JAR which contains it explicitly. The artifact is always available and is not looked up in a repository.
  • import (only available in Maven 2.0.9 or later)
    This scope is only supported on a dependency of type pom in the <dependencyManagement> section. It indicates the dependency to be replaced with the effective list of dependencies in the specified POM's <dependencyManagement> section. Since they are replaced, dependencies with a scope of import do not actually participate in limiting the transitivity of a dependency.

转自: 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱知菜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值