Python for Hive

384阅读 0评论2012-09-27 物连天涯
分类:


当使用hadoop成为web日志或生产日志的存储池后,我们可以使用./hive登录到hive shell中
进行查询操作。但为了方便,在查询方面用python进行封装好相应方法进行使用,用java进行我们的统计分析。

使用hive客户端的相关功能只需要将${hive_home}/lib/py路径加入但python的运行环境路径中就行了。
如以下脚本:


#/usr/bin/env python
#-*- coding: utf-8 -*-

import os,sys

dir = os.path.dirname(os.path.realpath(sys.argv[0]))

sys.path.insert(0, dir + '/lib/py')  ####引入相关python for hive 的相关环境


from hive_service import ThriftHive
from hive_service.ttypes import HiveServerException
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol


def hiveExe(sql):
    try:
        transport = TSocket.TSocket('192.168.237.13', 10000)
        transport = TTransport.TBufferedTransport(transport)
        protocol = TBinaryProtocol.TBinaryProtocol(transport)
        client = ThriftHive.Client(protocol)
        transport.open()
        client.execute(sql)
        print "The return value is : "+client.fetchOne()
        transport.close()
    except Thrift.TException, tx:
        print '%s' % (tx.message)

if __name__ == '__main__':
     print hiveExe("select * from img_ana limit 2")
上一篇:Hive基础
下一篇:hadoop资源