浙江省高等学校教师教育理论培训

微信搜索“毛凌志岗前心得”小程序

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

用python写hiveclient - 改善 - ITeye技术网站


第一步:

hive-0.7.0\lib\py 中所有py文件拷贝到你自己的Python工程中。

第二步:

把下面的代码拷贝一份新建一个py文件,修改IP,port ,输入要执行的SQL。

 

 

Python代码  收藏代码
  1. #encoding=utf-8  
  2. from hive_service import ThriftHive  
  3. from hive_service.ttypes import HiveServerException  
  4. from thrift import Thrift  
  5. from thrift.transport import TSocket  
  6. from thrift.transport import TTransport  
  7. from thrift.protocol import TBinaryProtocol  
  8.   
  9. def hiveExe(sql):  
  10.     try:  
  11.         transport = TSocket.TSocket('119.188.7.xx'10000)   
  12.         transport = TTransport.TBufferedTransport(transport)  
  13.         protocol = TBinaryProtocol.TBinaryProtocol(transport)  
  14.         client = ThriftHive.Client(protocol)  
  15.         transport.open()  
  16.   
  17.         client.execute(sql)  
  18.   
  19.         print "The return value is : "   
  20.         print client.fetchOne()  
  21.         print "............"  
  22.     except Thrift.TException, tx:  
  23.         print '%s' % (tx.message)  
  24.     finally:  
  25.         transport.close()  
  26.   
  27. if __name__ == '__main__':  
  28.     hiveExe("select count(*) from pokes")  

 

 第三步:


hive --service hiveserver (启动hive server)
posted on 2012-09-25 13:26  lexus  阅读(438)  评论(0编辑  收藏  举报