• python连接hiveserver2


    sudo pip install pyhs2

    网上找的例子:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # hive util with hive server2
    """
    @author:knktc
    @create:2014-04-08 16:55
    """
    __author__ = 'knktc'
    __version__ = '0.1'
    
    import pyhs2
    
    class HiveClient:
        def __init__(self, db_host, user, password, database, port=10000, authMechanism="PLAIN"):
            """
            create connection to hive server2
            """
            self.conn = pyhs2.connect(host=db_host,
                                      port=port,
                                      authMechanism=authMechanism,
                                      user=user,
                                      password=password,
                                      database=database,
                                      )
    
        def query(self, sql):
    
            """
            query
            """
    
            with self.conn.cursor() as cursor:
    
                cursor.execute(sql)
    
                return cursor.fetch()
    
        def close(self):
    
            """
            close connection
            """
            self.conn.close()
    
    
    
    
    
    
    
    def main():
    
        """
    
        main process
    
        @rtype:
    
        @return:
    
        @note:
    
    
    
    
        """
    
        hive_client = HiveClient(db_host='221.143.68.22', port=10000, user='hdfs', password='mypass',
    
                                 database='default', authMechanism='PLAIN')
    
        result = hive_client.query('select * from test limit 10')
    
        print result
    
        hive_client.close()
    
    
    
    
    
    
    
    if __name__ == '__main__':
    
        main()

    官网例子:

    
    import pyhs2
    
    
    
    with pyhs2.connect(host='localhost',
    
                       port=10000,
    
                       authMechanism="PLAIN",
    
                       user='root',
    
                       password='test',
    
                       database='default') as conn:
    
        with conn.cursor() as cur:
    
            #Show databases
    
            print cur.getDatabases()
    
    
    
            #Execute query
    
            cur.execute("select * from table")
    
    
    
            #Return column info from query
    
            print cur.getSchema()
    
    
    
            #Fetch table results
    
            for i in cur.fetch():
    
                print i

    https://cwiki.apache.org/confluence/display/Hive/Setting+Up+HiveServer2#SettingUpHiveServer2-PythonClientDriver

  • 相关阅读:
    20145220&20145209&20145309信息安全系统设计基础实验报告
    20145209 《信息安全系统设计基础》第8周学习总结
    R574
    gym102219
    102222F
    luogu 1337
    luogu 2503 & bzoj 2428
    18 BJ J
    poj 1981
    101992 I
  • 原文地址:https://www.cnblogs.com/ggzone/p/10121210.html
Copyright © 2020-2023  润新知