如何在Java中执行Hive命令或HiveQL

如何在Java中执行Hive命令或HiveQL,第1张

Java在15过后提供了ProcessBuilder根据运行时环境启动一个Process调用执行运行时环境下的命令或应用程序(15以前使用Runtime),关于ProcessBuilder请参考Java相关文档。调用代码如下:

String sql="show tables; select from test_tb limit 10";

List<String> command = new ArrayList<String>();

commandadd("hive");

commandadd("-e");

commandadd(sql);

List<String> results = new ArrayList<String>();

ProcessBuilder hiveProcessBuilder = new ProcessBuilder(command);

hiveProcess = hiveProcessBuilderstart();

BufferedReader br = new BufferedReader(new InputStreamReader(

hiveProcessgetInputStream()));

String data = null;

while ((data = brreadLine()) != null) {

resultsadd(data);

}

其中command可以是其它Hive命令,不一定是HiveQL。

hive配置远程metastore的方法:

1)首先配置hive使用本地MySQL存储metastore(服务器A 1111212123)(也可以使用远程mysql存储)

2)配置完成后,在服务器A启动服务:/etc/initd/Hadoop-hive-metastore start (默认监听端口是:9083)

3)配置hive客户端,修改hive-sitexml:(服务器B-需要有hadoop环境)

<property>

<name>hivemetastorelocal</name>

<value>false</value>

<description>controls whether to connect to remote metastore server or open a new metastore server in Hive Client JVM</description>

</property>

<property>

<name>hivemetastoreuris</name>

<value>thrift://127001:9083</value>

<description></description>

</property>

4)hive 执行测试hql

由于版本的不同,Python 连接 Hive 的方式也就不一样。

在网上搜索关键字 python hive 的时候可以找到一些解决方案。大部分是这样的,首先把hive 根目录下的$HIVE_HOME/lib/py拷贝到 python 的库中,也就是 site-package 中,或者干脆把新写的 python 代码和拷贝的 py 库放在同一个目录下,然后用这个目录下提供的 thrift 接口调用。示例也是非常简单的。类似这样:

import sys

from hive_service import ThriftHive

from hive_servicettypes import HiveServerException

from thrift import Thrift

from thrifttransport import TSocket

from thrifttransport import TTransport

from thriftprotocol import TBinaryProtocol

def hiveExe(sql):

try:

transport = TSocketTSocket('127001', 10000)

transport = TTransportTBufferedTransport(transport)

protocol = TBinaryProtocolTBinaryProtocol(transport)

client = ThriftHiveClient(protocol)

transportopen()

clientexecute(sql)

print "The return value is : "

print clientfetchAll()

print ""

transportclose()

except ThriftTException, tx:

print '%s' % (txmessage)

if __name__ == '__main__':

hiveExe("show tables")1234567891011121314151617181920212223242526272812345678910111213141516171819202122232425262728

或者是这样的:

#!/usr/bin/env python

import sys

from hive import ThriftHive

from hivettypes import HiveServerException

from thrift import Thrift

from thrifttransport import TSocket

from thrifttransport import TTransport

from thriftprotocol import TBinaryProtocol

try:

transport = TSocketTSocket('1418154188', 10000)

transport = TTransportTBufferedTransport(transport)

protocol = TBinaryProtocolTBinaryProtocol(transport)

client = ThriftHiveClient(protocol)

transportopen()

clientexecute("CREATE TABLE r(a STRING, b INT, c DOUBLE)")

clientexecute("LOAD TABLE LOCAL INPATH '/path' INTO TABLE r")

clientexecute("SELECT FROM test1")

while (1):

row = clientfetchOne()

if (row == None):

break

print rowve

clientexecute("SELECT FROM test1")

print clientfetchAll()

transportclose()

except ThriftTException, tx:

print '%s' % (txmessage)

12345678910111213141516171819202122232425262728293031323334351234567891011121314151617181920212223242526272829303132333435

但是都解决不了问题,从 netstat 中查看可以发现 TCP 连接确实是建立了,但是不执行 hive 指令。也许就是版本的问题。

还是那句话,看各种中文博客不如看官方文档。

项目中使用的 hive 版本是013,此时此刻官网的最新版本都到了121了。中间间隔了120、110、100、0140。但是还是参考一下官网的方法试试吧。

首先看官网的 setting up hiveserver2

可以看到启动 hiveserver2 可以配置最大最小线程数,绑定的 IP,绑定的端口,还可以设置认证方式。(之前一直不成功正式因为这个连接方式)然后还给了 python 示例代码。

import pyhs2

with pyhs2connect(host='localhost',

port=10000,

authMechanism="PLAIN",

user='root',

password='test',

database='default') as conn:

with conncursor() as cur:

#Show databases

print curgetDatabases()

#Execute query

curexecute("select from table")

#Return column info from query

print curgetSchema()

#Fetch table results

for i in curfetch():

print i123456789101112131415161718192021123456789101112131415161718192021

在拿到这个代码的时候,自以为是的把认证信息给去掉了。然后运行发现跟之前博客里介绍的方法结果一样,建立了 TCP 连接,但是就是不执行,也不报错。这是几个意思?然后无意中尝试了一下原封不动的使用上面的代码。结果可以用。唉。。。

首先声明一下,hive-sitexml中默认关于 hiveserver2的配置我一个都没有修改,一直是默认配置启动 hiveserver2。没想到的是默认配置是有认证机制的。

然后再写一点,在安装 pyhs2的时候还是遇到了点问题,其实还是要看官方文档的,我只是没看官方文档直接用 pip安装导致了这个问题。安装 pyhs2需要确定已经安装了几个依赖包。直接看在 github 上的 wiki 吧。哪个没安装就补上哪一个就好了。

To install pyhs2 on a clean CentOS 64 64-bit desktop

(as root or with sudo)

get ez_setuppy from >

以上就是关于如何在Java中执行Hive命令或HiveQL全部的内容,包括:如何在Java中执行Hive命令或HiveQL、远程连接元数据库hive.metastore.uris怎么写、windows下怎么用python连接hive数据库等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/9624527.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-30
下一篇 2023-04-30

发表评论

登录后才能评论

评论列表(0条)

保存