在Python中使用ibis,impyla,pyhive,pyspark连接Kerberos安全认证的Hive、Impala

在python中连接hive和impala有很多中方式,有pyhive,impyla,pyspark,ibis等等,本篇我们就逐一介绍如何使用这些包连接hive或impala,以及如何通过kerberos认证。

Kerberos

如果集群没开启kerberos认证则不需要这里的代码,或者在系统环境内通过kinit命令认证也不需要这部分的代码。

krbcontext.context_shell

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

__all__ = [ 'krbcontext', 'KRB5KinitError', ]

import os, sys
# import pwd
import subprocess


from contextlib import contextmanager

class KRB5KinitError(Exception):
    pass

# def get_login():
#     ''' Get current effective user name '''
#
#     return pwd.getpwuid(os.getuid()).pw_name

def init_ccache_as_regular_user(principal=None, ccache_file=None):
    '''Initialize credential cache as a regular user

    Return the filename of newly initialized credential cache
    '''

    if not sys.stdin.isatty():
        raise IOError('This is not running on console. So, you need to run kinit '
                      'with your principal manually before anything goes.')

    cmd = 'kinit %(ccache_file)s %(principal)s'
    args = {}

    args['principal'] = principal
    args['ccache_file'] = '-c %s' % ccache_file

    kinit_proc = subprocess.Popen(
        (cmd % args).split(),
        stderr=subprocess.PIPE)
    stdout_data, stderr_data = kinit_proc.communicate()

    if kinit_proc.returncode > 0:
        raise KRB5KinitError(stderr_data)

    return ccache_file



def init_ccache_with_keytab(principal, keytab_file, ccache_file):
    '''Initialize credential cache using keytab file

    Return the filename of newly initialized credential cache
    '''
    cmd = 'kinit -kt %(keytab_file)s -c %(ccache_file)s %(principal)s'
    args = {}

    args['principal'] = principal
    args['ccache_file'] = ccache_file
    args['keytab_file'] = keytab_file

    kinit_proc = subprocess.Popen(
        (cmd % args).split(),
        stderr=subprocess.PIPE)
    stdout_data, stderr_data = kinit_proc.communicate()

    if kinit_proc.returncode > 0:
        raise KRB5KinitError(stderr_data)

    return ccache_file


@contextmanager
def krbcontext(using_keytab=False, **kwargs):
    '''A context manager for Kerberos-related actions

    using_keytab: specify to use Keytab file in Kerberos context if True,
                  or be as a regular user.
    kwargs: contains the nec
下面是 Python3.6 的代码示例,可以连接 MySQL、PostgreSQL、Oracle、Kingbase、MariaDB 和 Hive 数据库: ```python import pymysql import psycopg2 import cx_Oracle import pykingbase import mariadb from pyhive import hive def get_db_connection(database_type, host, port, username, password, database_name): if database_type == 'mysql': connection = pymysql.connect( host=host, port=port, user=username, password=password, db=database_name ) elif database_type == 'pgsql': connection = psycopg2.connect( host=host, port=port, user=username, password=password, database=database_name ) elif database_type == 'oracle': connection = cx_Oracle.connect( username, password, f'{host}:{port}/{database_name}' ) elif database_type == 'kingbase': connection = pykingbase.connect( host=host, user=username, password=password, port=port, database=database_name ) elif database_type == 'mariadb': connection = mariadb.connect( user=username, password=password, host=host, port=port, database=database_name ) elif database_type == 'hive': connection = hive.connect( host=host, port=port, username=username, password=password, database=database_name, auth='NOSASL' ) else: raise ValueError(f"Unsupported database type '{database_type}'") return connection ``` 以上是一个 Python 方法,可以根据指定的数据库类型、IP 地址、用户名、密码等参数来连接 MySQL、PostgreSQL、Oracle、Kingbase、MariaDB 和 Hive 数据库。根据不同的数据库类型调用相应的库来进行连接
评论 19
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王义凯_Rick

遇见即是缘,路过就给个评论吧~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值