下载源码包
下载安装包对应的源码包。
本次使用 apache-hive-3.1.2-src.tar.gz 。
编译
解压在当前目录后进入,打开命令行窗口,执行:
mvn clean compile -Phadoop-2 -DskipTests
报错:
java.io.IOException: Cannot run program "bash" (in directory "D:\xxx\apache-hive-3.1.2-src\common"): CreateProcess error=2, 系统找不到指定的文件。
这是因为在Windows环境不能执行bash。那么需要能执行bash环境,有一个神器:Git
安装git后,打开Git Bash,即可在Windows系统中执行Linux命令了!
在源码包目录下 Git Bash Here,在git窗口中重新执行
mvn clean compile -Phadoop-2 -DskipTests
Two thousand years later…
使用IDEA打开
1.修改pom.xml文件
经过maven编译后,理论上需要用到的依赖都已经下载,但是IDEA打开后,仍然有少部分失败。
如 提示找不到在org.apache.directory.server依赖中找不到 org.apache.directory.client.ldap:ldap-client-api:0.1-SNAPSNOT
Could not find artifact org.apache.directory.client.ldap:ldap-client-api:pom:0.1-SNAPSHOT in alimaven
而hive-service模块pom.xml中已存在:
<dependency>
<groupId>org.apache.directory.client.ldap</groupId>
<artifactId>ldap-client-api</artifactId>
<version>${apache-directory-clientapi.version}</version>
<scope>test</scope>
</dependency>
查看maven仓库中这个jar也存在。可以大胆的将org.apache.directory.server中找不到依赖的包排除掉:
<dependency>
<groupId>org.apache.directory.server</groupId>
<artifactId>apacheds-server-integ</artifactId>
<version>${apache-directory-server.version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.apache.directory.client.ldap</groupId>
<artifactId>ldap-client-api</artifactId>
</exclusion>
</exclusions>
</dependency>
同样,还有部分依赖是因为指定scope为test而导致。将 <scope>test</scope> 注释即可。
可以参考:https://www.cnblogs.com/huangguoming/p/15779441.html
2.Build
Build前设置一下heap内存,默认的700M在Build过程中容易OOM。
3.创建UDF函数并注册
在ql模块,org.apache.hadoop.hive.ql.udf 包下创建UDF类,注意packge
在org.apache.hadoop.hive.ql.exec.FunctionRegistry中注册刚才创建的 AddRandomUDF
此时。已经可以将这份源码包重新打包,同样在 Git Bash 中打包
mvn clean package -Phadoop-2 -DskipTests
打包后上传至服务器重新部署hive,或替换jar的方式,生效自定义UDF函数就能使用。不做演示。
加下来演示的是,在IDEA中启动Hive,并验证创建的函数。
4.添加配置文件
在cli中创建resousces目录,mark为Resources,并放入相关配置文件
core-site.xml
hive-site.xml
hive-site.xml
配置文件从hive云主机拷贝一份过来即可
忘了放入core-site.xml,则找不到hadoop相关配置会报错:
org.apache.hadoop.io.nativeio.NativeIO$POSIX.stat(Ljava/lang/String;)Lorg/apache/hadoop/io/nativeio/NativeIO$POSIX$Stat
注意 hive-site.xml 要额外加上 hive.metastore.uris 和 datanucleus.schema.autocreateall
<property>
<name>hive.metastore.uris</name>
<value>thrift://ip:9083</value> <!-- 配置hive 中metastore启动ip和端口-->
</property>
<property>
<name>datanucleus.schema.autocreateall</name>
<value>false</value>
</property>
<property>
<name>hive.metastore.local</name>
<value>true</value>
</property>
5.云主机上hive保持meatastore启动监听
hive --service metastore -p 9083 &
6.添加VM参数
-Djline.WindowsTerminal.directConsole=false
7.启动
找到 CliDriver 这个类并启动。
验证创建的addrandom函数。
如果遇到报错 /tmp 权限不足
hdfs dfs -chmod 755 /tmp