首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Hive添加第三方Jar包方式总结

一、在 Hive Shell中加入---add jar

hdfs dfs -put HelloUDF-1.0.jar /tmp

beeline -u "jdbc:hive2://test.bigdata.com:10000" -n "song" -p ""

add jar hdfs:///tmp/HelloUDF-1.0.jar;

create function HelloUDF as 'org.example.HelloUDF' USING JAR 'hdfs:///tmp/HelloUDF-1.0.jar';

select HelloUDF("Hive");

add jar 建议制定一个HDFS路径,jar包上传到HDFS上,这样比较方便。如果指定是本地路径,则需要确保每个hive shell节点上的对应位置上都放置了需要的jar包,这样操作起来比较麻烦。如果hiveserver2是使用的vip,则需要使用beeline连接每台hiveserver2上去,然后执行一遍。

二、Jar放入${HIVE_HOME}/auxlib目录

在${HIVE_HOME}中创建文件夹auxlib,然后将自定义jar文件放入该文件夹中。

此方法添加不需要重启Hive。而且比较便捷。

hive-env.sh中的HIVE.AUX.JARS.PATH配置对服务器无效,仅对当前hive shell有效,不同的hive shell相互不影响,每个hive shell都需要配置,可以配置成文件夹形式。

HIVE.AUX.JARS.PATH和hive.aux.jars.path仅支持本地文件。可配置成文件,也可配置为文件夹。

在${HIVE_HOME}/conf/hive-env.sh下配置:

#本地文件路径,不支持HDFS路径

export HIVE_AUX_JARS_PATH=/home/hadoop/lib/mysql-connector-java-5.1.7-bin.jar

hive-site.xml的hive.aux.jars.path对服务器有效,对当前hive shell无效。

可以在${HIVE_HOME}/conf/hive-site.xml下配置:

<name>hive.aux.jars.path<!--name></P><P>    <value>/home/hadoop/lib/mysql-connector-java-5.1.7-bin.jar <!--value></P><P><!--property></P><P></code></pre><p data-tool="mdnice编辑器" style="overflow-x: auto;"></p><blockquote data-tool="mdnice编辑器" style="margin-top: 20px;margin-bottom: 20px;padding: 10px 10px 10px 20px;border-top: 3px none rgba(0, 0, 0, 0.4);border-bottom: 3px none rgba(0, 0, 0, 0.4);border-right: 1px solid rgb(150, 84, 181);border-left-color: rgb(150, 84, 181);border-radius: 0px;background: none 0% 0% / auto no-repeat scroll padding-box border-box rgb(251, 249, 253);width: auto;height: auto;box-shadow: rgba(0, 0, 0, 0) 0px 0px 0px 0px;overflow: auto;"><p style="text-indent: 0em;padding-top: 8px;padding-bottom: 8px;color: rgb(90, 90, 90);line-height: 1.8em;letter-spacing: 0.02em;">如果 Hive Metastore 与 HiveServer2 不在同另一台主机上,那么要在这两台机器上创建相同的目录,但是 JAR 文件需要上传到 HiveServer2 主机上。</p></blockquote><h2 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;align-items: unset;background-attachment: scroll;background-clip: border-box;background-image: none;background-origin: padding-box;background-position: 0% 0%;background-repeat: no-repeat;background-size: auto;border-style: none;border-width: 1px;border-color: rgb(0, 0, 0);border-radius: 0px;box-shadow: none;display: flex;flex-direction: unset;float: unset;height: auto;justify-content: center;line-height: 1.5em;overflow: unset;text-shadow: none;transform: none;width: 100%;-webkit-box-reflect: unset;">四、直接将Jar加入${HIVE_HOME}/lib目录</h2><p data-tool="mdnice编辑器" style="overflow-x: auto;"></p><h2 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;align-items: unset;background-attachment: scroll;background-clip: border-box;background-image: none;background-origin: padding-box;background-position: 0% 0%;background-repeat: no-repeat;background-size: auto;border-style: none;border-width: 1px;border-color: rgb(0, 0, 0);border-radius: 0px;box-shadow: none;display: flex;flex-direction: unset;float: unset;height: auto;justify-content: center;line-height: 1.5em;overflow: unset;text-shadow: none;transform: none;width: 100%;-webkit-box-reflect: unset;">五、可重载辅助jar目录</h2><p data-tool="mdnice编辑器" style="color: rgb(90, 90, 90);font-size: 15px;line-height: 1.8em;letter-spacing: 0.02em;text-indent: 0em;margin-top: 10px;margin-bottom: 10px;padding-top: 8px;padding-bottom: 8px;">和配置hive.aux.jars.path一样,只需配置参数<code break-all;"="" consolas,="" menlo,="" monaco,="" mono",="" monospace;word-break:="" operator="" style="color: rgb(150, 84, 181);font-size: 14px;line-height: 1.8em;letter-spacing: 0em;background: none 0% 0% / auto no-repeat scroll padding-box border-box rgba(27, 31, 35, 0.05);width: auto;height: auto;margin-left: 2px;margin-right: 2px;padding: 2px 4px;border-style: none;border-width: 3px;border-color: rgb(0, 0, 0) rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4);border-radius: 4px;font-family: ">hive.reloadable.aux.jars.path</code>即可。</P><P>在 <code break-all;"="" consolas,="" menlo,="" monaco,="" mono",="" monospace;word-break:="" operator="" style="color: rgb(150, 84, 181);font-size: 14px;line-height: 1.8em;letter-spacing: 0em;background: none 0% 0% / auto no-repeat scroll padding-box border-box rgba(27, 31, 35, 0.05);width: auto;height: auto;margin-left: 2px;margin-right: 2px;padding: 2px 4px;border-style: none;border-width: 3px;border-color: rgb(0, 0, 0) rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4);border-radius: 4px;font-family: ">${HIVE_HOME}/conf/hive-site.xml</code>下配置:</p><pre data-tool="mdnice编辑器" style="border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;margin-top: 10px;margin-bottom: 10px;"><code style=""><property></P><P>    <name>hive.aux.jars.path<!--name></P><P>    <value>/usr/local/hive/auxjar<!--value></P><P><!--property></P><P></code></pre><p data-tool="mdnice编辑器" style="color: rgb(90, 90, 90);font-size: 15px;line-height: 1.8em;letter-spacing: 0.02em;text-indent: 0em;margin-top: 10px;margin-bottom: 10px;padding-top: 8px;padding-bottom: 8px;"><strong>重启HiveServer2</strong>使配置生效。</p><p data-tool="mdnice编辑器" style="overflow-x: auto;"></p><h2 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;align-items: unset;background-attachment: scroll;background-clip: border-box;background-image: none;background-origin: padding-box;background-position: 0% 0%;background-repeat: no-repeat;background-size: auto;border-style: none;border-width: 1px;border-color: rgb(0, 0, 0);border-radius: 0px;box-shadow: none;display: flex;flex-direction: unset;float: unset;height: auto;justify-content: center;line-height: 1.5em;overflow: unset;text-shadow: none;transform: none;width: 100%;-webkit-box-reflect: unset;">总结</h2><pre data-tool="mdnice编辑器" style="border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;margin-top: 10px;margin-bottom: 10px;"><code style="">create function my_fun as ‘com.github.xxx.MyFun’ using jar ‘hdfs:///xxxx.jar’;</P><P></code></pre><p data-tool="mdnice编辑器" style="color: rgb(90, 90, 90);font-size: 15px;line-height: 1.8em;letter-spacing: 0.02em;text-indent: 0em;margin-top: 10px;margin-bottom: 10px;padding-top: 8px;padding-bottom: 8px;">这样注册的funtion是全局可见的,如果不想扩大到这样的作用域,可以追加temporary修饰,并在用的的sql前追加声明就可以了,例如:</p><pre data-tool="mdnice编辑器" style="border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;margin-top: 10px;margin-bottom: 10px;"><code style="">create temporary function my_fun as ‘com.github.xxx.MyFun’ using jar ‘hdfs:///xxxx.jar’;</P><P>select my_fun(t.a) from mytable t where …</P><P></code></pre><p data-tool="mdnice编辑器" style="color: rgb(90, 90, 90);font-size: 15px;line-height: 1.8em;letter-spacing: 0.02em;text-indent: 0em;margin-top: 10px;margin-bottom: 10px;padding-top: 8px;padding-bottom: 8px;">但是使用create function xxxx using jar xxxx有一个限制,那就是当UDF中依赖到第三方的jar包时,就会面临两种选择:</p><P class="list-paddingleft-1" data-tool="mdnice编辑器" style="margin-top: 8px;margin-bottom: 8px;padding-left: 25px;"><P><p style="margin-top: 5px;margin-bottom: 5px;color: rgb(90, 90, 90);font-size: 15px;line-height: 1.8em;letter-spacing: 0.02em;">要么将有的依赖包打成一个jar</p></P></P><h2 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;align-items: unset;background-attachment: scroll;background-clip: border-box;background-image: none;background-origin: padding-box;background-position: 0% 0%;background-repeat: no-repeat;background-size: auto;border-style: none;border-width: 1px;border-color: rgb(0, 0, 0);border-radius: 0px;box-shadow: none;display: flex;flex-direction: unset;float: unset;height: auto;justify-content: center;line-height: 1.5em;overflow: unset;text-shadow: none;transform: none;width: 100%;-webkit-box-reflect: unset;">实战:CDH中Hive整合ES添加自定义Jar</h2><h3 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;display: flex;">配置Hive辅助Jar目录(Hive Server生效)</h3><p data-tool="mdnice编辑器" style="color: rgb(90, 90, 90);font-size: 15px;line-height: 1.8em;letter-spacing: 0.02em;text-indent: 0em;margin-top: 10px;margin-bottom: 10px;padding-top: 8px;padding-bottom: 8px;">比如hue中使用到了hive查询编辑器,在里面写HQL要用到自定义的JAR,那就必须设置这个地方。</p><figure data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;display: flex;flex-direction: column;justify-content: center;align-items: center;"><img class="rich_pages wxw-img" data-imgfileid="100001951" data-ratio="0.37051282051282053" data-src="https://inews.gtimg.com/om_bt/O-wm9BAQW92FRqU0kcoVzzqHWYt9aUo9fyigmxrJPNIhIAA/641" data-type="png" data-w="780" src="https://inews.gtimg.com/om_bt/O-wm9BAQW92FRqU0kcoVzzqHWYt9aUo9fyigmxrJPNIhIAA/641" style="display: block;margin-right: auto;margin-left: auto;border-style: none;border-width: 3px;border-color: rgba(0, 0, 0, 0.4);border-radius: 0px;object-fit: fill;box-shadow: rgba(0, 0, 0, 0) 0px 0px 0px 0px;"/></figure><p data-tool="mdnice编辑器" style="color: rgb(90, 90, 90);font-size: 15px;line-height: 1.8em;letter-spacing: 0.02em;text-indent: 0em;margin-top: 10px;margin-bottom: 10px;padding-top: 8px;padding-bottom: 8px;">将自定义Jar包放入Hive辅助JAR目录中(HiveServer2, Hive Metastore Server, Hive CLI中都需要放置)</P><P>然后<strong>重启</strong>HiveServer2, Hive Metastore Server。</p><h3 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;display: flex;"><strong>配置hive-env.sh 的 Gateway 客户端环境高级配置代码段(安全阀)</strong></h3><figure data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;display: flex;flex-direction: column;justify-content: center;align-items: center;"><img class="rich_pages wxw-img" data-imgfileid="100001952" data-ratio="0.34602829162132753" data-src="https://inews.gtimg.com/om_bt/OUo_0f7izVuiys7cfuZ1Q8dkoa5rOQRr63DiVvnq98Uu8AA/641" data-type="png" data-w="919" src="https://inews.gtimg.com/om_bt/OUo_0f7izVuiys7cfuZ1Q8dkoa5rOQRr63DiVvnq98Uu8AA/641" style="display: block;margin-right: auto;margin-left: auto;border-style: none;border-width: 3px;border-color: rgba(0, 0, 0, 0.4);border-radius: 0px;object-fit: fill;box-shadow: rgba(0, 0, 0, 0) 0px 0px 0px 0px;"/></figure><blockquote data-tool="mdnice编辑器" style="margin-top: 20px;margin-bottom: 20px;padding: 10px 10px 10px 20px;border-top: 3px none rgba(0, 0, 0, 0.4);border-bottom: 3px none rgba(0, 0, 0, 0.4);border-right: 1px solid rgb(150, 84, 181);border-left-color: rgb(150, 84, 181);border-radius: 0px;background: none 0% 0% / auto no-repeat scroll padding-box border-box rgb(251, 249, 253);width: auto;height: auto;box-shadow: rgba(0, 0, 0, 0) 0px 0px 0px 0px;overflow: auto;"><p style="text-indent: 0em;padding-top: 8px;padding-bottom: 8px;color: rgb(90, 90, 90);line-height: 1.8em;letter-spacing: 0.02em;">GATEWAY DEFAULT GROUP下面的设置的路径是会影响所有要使用HIVE客户端的节点,比如你直接在SHELL控制台上使用HIVE的话,必须配置这个地方。<strong>CM需要重新部署客户端配置</strong>。</p></blockquote></p><p></P><P></p><p style="display: none;"><mp-style-type data-value="3"></mp-style-type></p>

  • 发表于:
  • 原文链接https://page.om.qq.com/page/OwvxDfvSwsUdD0CrSscoyTSA0
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券