How to Access Hive via Python?

后端 未结 16 884
小蘑菇
小蘑菇 2020-11-30 17:11

https://cwiki.apache.org/confluence/display/Hive/HiveClient#HiveClient-Python appears to be outdated.

When I add this to /etc/profile:

export PYTHONP         


        
16条回答
  •  囚心锁ツ
    2020-11-30 17:47

    You can use hive library,for that you want to import hive Class from hive import ThriftHive

    Try This example:

    import sys
    
    from hive import ThriftHive
    from hive.ttypes import HiveServerException
    
    from thrift import Thrift
    from thrift.transport import TSocket
    from thrift.transport import TTransport
    from thrift.protocol import TBinaryProtocol
    
    try:
      transport = TSocket.TSocket('localhost', 10000)
      transport = TTransport.TBufferedTransport(transport)
      protocol = TBinaryProtocol.TBinaryProtocol(transport)
      client = ThriftHive.Client(protocol)
      transport.open()
      client.execute("CREATE TABLE r(a STRING, b INT, c DOUBLE)")
      client.execute("LOAD TABLE LOCAL INPATH '/path' INTO TABLE r")
      client.execute("SELECT * FROM r")
      while (1):
        row = client.fetchOne()
        if (row == None):
           break
        print row
    
      client.execute("SELECT * FROM r")
      print client.fetchAll()
      transport.close()
    except Thrift.TException, tx:
      print '%s' % (tx.message)
    

提交回复
热议问题