• 技术文章 >Python技术 >Python基础教程

    python 连接hive的包是什么

    爱喝马黛茶的安东尼爱喝马黛茶的安东尼2019-10-10 14:23:16原创2558

    python连接hive的几种方式

    ·基于pyhive连接hive。

    ·基于impyla连接hive。

    方法一:使用PyHive库

    安装依赖包:其中sasl安装可能会报错,可以去https://www.lfd.uci.edu/~gohlke/pythonlibs/#sasl下载对应版本安装。

    pip install sasl
    pip install thrift
    pip install thrift-sasl
    pip install PyHive

    相关推荐:《Python基础教程

    Python脚本代码操作:

    from pyhive import hive   # or import hive
    conn = hive.Connection(host='****', port=****, username='****', database='****')
    cursor.execute(''SELECT * FROM my_awesome_data LIMIT 10'')
    for i in range(****):
        sql = "INSERT INTO **** VALUES ({},'username{}')".format(value, str(username))
        cursor.execute(sql)
      
    # 下面是官网代码:
    from pyhive import presto  # or import hive
    cursor = presto.connect('localhost').cursor()
    cursor.execute('SELECT * FROM my_awesome_data LIMIT 10')
    print(cursor.fetchone())
    print(cursor.fetchall())

    方法二:使用impyla库

    impyla依赖包:

    pip install six
    pip install bit-array
    pip install thriftpy

    为了支持Hive还需要以下两个包:

    pip install sasl
    pip install thrift-sasl

    可在Python PyPI中下载impyla及其依赖包的源码

    Python脚本代码:

    from impala.dbapi import connect 
    conn = connect(host ='****',port = ****)
    cursor = conn.cursor()
    cursor.execute('SELECT * FROM mytable LIMIT 100')
    print cursor.description   # 打印结果集的schema 
    results = cursor.fetchall()
    专题推荐:python 连接 hive 包
    上一篇:python 类和函数的区别有哪些 下一篇:python如何判断文件有多少行

    相关文章推荐

    • python怎么卸载包• python中simhash包怎么用

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网