首页 > 编程语言 >python连接hive

python连接hive

时间:2022-11-16 18:34:13浏览次数:42  
标签:__ python self hive cursor init config 连接 def

安装

"""
pip install pyhs2

等待这个模块安装完成之后不要关闭命令行,接着在新的一行去执行命令。此时这个命令的作用是开启hive服务,否则python程序无法成功连接,命令如下: 

hive --service hiveserver
这个是启动hive
"""

python3.7 利用pyhive 连接上hive

pip install sasl

pip install thrift

pip install thrift-sasl

pip install PyHive

使用

# 连接hive 注意端口 这里是hiveserver2的端口 默认为10000
from pyhive import hive
conn = hive.Connection(host='10.8.13.120', port=10000, username='hdfs', database='default')
cursor = conn.cursor()
cursor.execute('show tables')

for result in cursor.fetchall():
    print(result)
cursor.close()
conn.close()

示例

# -*- coding: utf-8 -*-


from __future__ import print_function
import thrift
import requests
import sys
import json
import sqlite3
import pymssql
import pandas as pd
#import impala.dbapi as hive
from pyhive import hive
from pyhive import presto


PY3 = 1 if sys.version > '3' else 0


class BaseConn(object):

    def __init__(self, **config):
        self.config = config

    def build_connection(self):
        pass

    def init_from_json(self, json_file):
        with open(json_file) as f:
            self.config = json.load(f)
        self.build_connection()

    def read_df(self, sql):
        return pd.read_sql(sql, self.con)

    def execute(self, sql):
        cursor = self.con.cursor()
        try:
            cursor.execute(sql)
            res = cursor.fetchall()
            return res
        except Exception as e:
            raise e

    def iter_execute(self, sql):
        """将结果做成一个生成器"""
        cursor = self.con.cursor()
        try:
            cursor.execute(sql)
            while True:
                one = cursor.fetchone()
                if one is None:
                    break
                else:
                    yield one
        except Exception as e:
            raise e

    def __del__(self):
        if self.con:
            # print("close the connection")
            self.con.close()


class PrestoConn(BaseConn):

    def __init__(self, **config):
        if PY3:
            super().__init__(**config)
        else:
            super(PrestoConn, self).__init__(**config)

    def build_connection(self):
        self.con = presto.connect(**self.config)


class HiveConn(BaseConn):

    def __init__(self, **config):
        if PY3:
            super().__init__(**config)
        else:
            super(HiveConn, self).__init__(**config)

    def build_connection(self):
        self.con = hive.connect(**self.config)


class SqliteConn(BaseConn):

    def __init__(self, **config):
        if PY3:
            super().__init__(**config)
        else:
            super(SqliteConn, self).__init__(**config)

    def build_connection(self):
        self.con = sqlite3.connect(**self.config)


class MssqlConn(BaseConn):

    def __init__(self, **config):
        if PY3:
            super().__init__(**config)
        else:
            super(MssqlConn, self).__init__(**config)

    def build_connection(self):
        self.con = pymssql.connect(**self.config)


标签:__,python,self,hive,cursor,init,config,连接,def
From: https://www.cnblogs.com/xiaofubase/p/16897090.html

相关文章

  • python连接hbase
    前提条件已安装Python-3.6。已经有搭建好的完全分布式集群,并已经成功启动Hadoop,Zookeeper和HBase。笔者当前搭建好的集群是Hadoop-3.0.3,Zookeeper-3.4.13和HBase-2.1.0......
  • python操作hdfs
    安装安装hadoop关于hadoop的安装配置会在另一篇文章中介绍,这里只介绍python的hdfs库的安装.安装hdfs库所有python的三方模块均采用pip来安装.pipinstallhdfshdfs......
  • 20221115-Python列表与元组
    1.列表的概念:  列表是可变对象  2.列表元素的新增与删除    3.列表的下标和切片同字符串一致4.元组   ......
  • ArcGIS Python API可视化及分析系列教程(一):入门与简介(2)安装与配置
    前文再续,本节主要讲安装……前置要求:1、有Python软件安装的经验。2、离线安装的话,需要有ArcGISJavascriptAPI部署经验和能力。如果这两个都从来没有弄过的话,就用在线的......
  • Python实验报告——第10章 文件及目录操作
    Python实验报告——第10章文件及目录操作 实验报告【实验目的】 1.掌握Python自带的函数进行基本文件操作。2.掌握Python内置的os模块及其子模块os.path进行目......
  • Python实验报告——第8章 模块
    Python实验报告——第8章模块 实验报告【实验目的】 1.掌握Python内置的标准模块和第三方模块的使用。【实验条件】1.PC机或者远程编程环境。 【实验内容......
  • python JSON模块
    一、JSON介绍JSON(JavaScriptObjectNotation)是一种轻量级的数据交换格式,易于人阅读和编写。二、常用方法方法描述json.loads()将JSON字符串转化为Python对......
  • 11月16日内容总结——OSI传输层之TCP与UDP协议、应用层简介、socket模块介绍及代码优
    目录一、传输层之TCP与UDP协议1.TCP协议(重要)三次握手建链接四次挥手断连接2.UDP协议3.tcp和udp的对比二、应用层简介三、socket模块1、简介2、基于文件类型的套接字家族3......
  • python垃圾回收机制
    python垃圾回收机制主要分为:1.引用计数2.标记清除3.分代回收python的引用计数机制:python是根据对象的引用计数是否为0,来进行垃圾回收,释放内......
  • python的文件操作
    步骤1、打开文件:使用内置函数open2、进行操作(读或者写)读:read方法或者写:write方法3、关闭文件close方法#1、打开文件,返回文件的句柄f=open(file="xxx",mode=......