认证连接_在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 pwdimport 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  @contextmanagerdef krbcontext(using_keytab=False, **kwargs):    '''A context manager for Kerberos-related actions    using_keytab: specify to use Keytab file in Kerberos context if True,
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值