欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python远程访问hive

程序员文章站 2023-11-19 12:26:22
#!/usr/bin/python import sys sys.path.append('/home/zhoujie/downloads/hive-0.7.0-cdh...

#!/usr/bin/python
import sys
sys.path.append('/home/zhoujie/downloads/hive-0.7.0-cdh3u0/lib/py')
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


hive_server_ip='127.0.0.1'
hive_server_port=10000
#hive_sql='select count(*) from ssp_factbids'
hive_sql="select count(*) from ssp_factbids where logdate_id='20121101'"


def hiveexe(sql):
    try:
        transport = tsocket.tsocket(hive_server_ip, hive_server_port)
        transport = ttransport.tbufferedtransport(transport)
        protocol = tbinaryprotocol.tbinaryprotocol(transport)
        client = thrifthive.client(protocol)
        transport.open()

        client.execute(sql)

        print "the return value is : "
        print client.fetchall()
        print "............"
        transport.close()
    except thrift.texception, tx:
        print '%s' % (tx.message)

if __name__ == '__main__':
    hiveexe(hive_sql)