Conda 安装Jupyter:使用Pyhive(Kerberos)

安装基本环境

conda create -n bigdata python=3.10

conda activate bigdata

conda install -y pandas numpy pyhive

yum install gcc-c++ python-devel.x86_64 cyrus-sasl-devel.x86_64

pip install sasl

Jupyter Notebook

安装jupyter notebook配置自动提示

复制代码
conda install nb_conda_kernels

配置jupyter,添加密码,允许root,设置启动目录

环境配置-为linux服务器配置可以远程访问的Jupyter - 知乎 (zhihu.com)

jupyter notebook启动,以下报错可忽略

AttributeError: 'NotebookApp' object has no attribute 'io_loop'

PyHive连接开启Kerberos的Hive

jupyter notebook部署的服务器为集群已配置Kerberos认证的服务器,否则需要配置客户端认证

python 复制代码
from pyhive import hive
import pandas as pd
import numpy as np
import time

def func_time(func):
    def wrapper(*args, **kwargs):
        start_time = time.time()
        result = func(*args, **kwargs)
        end_time = time.time()

        execution_time = end_time - start_time
        print(f"函数 {func.__name__} 的执行时间为:{execution_time} 秒")

        return result

    return wrapper


class HiveCursor:
    def __init__(self, host, port, db, auth, username, configuration={'hive.execution.engine': 'spark'}):
        self.host = host
        self.port = port
        self.db = db
        self.username = username
        self.auth = auth
        self.conn = None
        self.cursor = None
        self.configuration = configuration

    def __enter__(self):
        # 建立与 Hive 的连接
        if self.auth == "KERBEROS":
            self.conn = hive.Connection(host=self.host, port=self.port, database=self.db,
                                        kerberos_service_name=self.username,
                                        auth=self.auth, configuration=self.configuration)
        else:
            self.conn = hive.Connection(host=self.host, port=self.port, database=self.db, username=self.username,
                                        auth=self.auth, configuration=self.configuration)
        self.cursor = self.conn.cursor()
        return self

    def query(self, sql):
        self.cursor.execute(sql)
        # 将查询结果读取到 DataFrame
        df = pd.DataFrame(self.cursor.fetchall())
        # 设置 DataFrame 列名
        df.columns = [desc[0] for desc in self.cursor.description]
        return df

    def __exit__(self, exc_type, exc_val, exc_tb):
        # 关闭连接
        self.conn.close()


@func_time
def hive_kerberos():
    query = "show databases"

    with HiveCursor(host="master", port=10000, db="default", auth='KERBEROS',
                    username='hive') as _hive:
        result = _hive.query(sql)
        print(result)


@func_time
def hive_on_spark():
    query = "show databases"

    with HiveCursor(host="master", port=10000, db="default", auth='KERBEROS',
                    username='hive', configuration={'hive.execution.engine': 'spark'}) as _hive:
        df = _hive.query(sql)
        print(df)
相关推荐
zlkingdom3 小时前
Jetson Orin开发板,在conda环境中直接实现Pytorch的GPU加速
人工智能·pytorch·conda·随笔·jetson orin
Leo.yuan7 小时前
MySQL到Hive数据同步怎么选工具?FineDataLink全链路方案实测
数据库·hive·mysql
Nefu_lyh8 小时前
【Hive】02 Hive 分区与分桶:深入理解 Hive 分区与分桶的原理、执行过程、Bucket Map Join、SMB Join 以及最佳实践
数据仓库·hive·hadoop
战斗强9 小时前
RTX 5090 Grounded-SAM-2 实时 RTSP 跟踪部署指南
python·conda
Nefu_lyh9 小时前
【Hive】一、Hive数据类型:基本数据类型、复杂数据类型
数据仓库·hive·hadoop
zhangfeng11332 天前
htc 中minconda 明明安装了 Python 3.10显示 python 3.8 因为 `conda activate` 没有真正切换成功
开发语言·python·conda
卷毛迷你猪3 天前
快速实验篇(A4)Hive 数据仓库进阶:全站点干旱事件识别与多维统计分析
数据仓库·hive·hadoop·分布式
冰上浮云3 天前
Gravitino iceberg catalog backend 为hive 获取元数据过程
数据仓库·hive·hadoop·gravitino
H Journey3 天前
python包和项目管理工具uv、conda介绍
python·conda·uv
兔子宇航员03015 天前
HIVE SQL 中 NULL 值在 JOIN 和 GROUP BY 中的致命陷阱与解决方案
hive·hadoop·sql