前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >「EMR 开发指南」之通过 Python 连接 Hive

「EMR 开发指南」之通过 Python 连接 Hive

原创
作者头像
岳涛
发布2023-11-22 11:23:52
4701
发布2023-11-22 11:23:52
举报
文章被收录于专栏:大数据生态大数据生态

说明

本文描述问题及解决方法同样适用于 弹性 MapReduce(EMR)

背景

Hive 中集成了 Thrift 服务。Thrift 是 Facebook 开发的一个软件框架,它用来进行可扩展且跨语言的服务的开发。Hive 的 HiveServer2 就是基于 Thrift 的,所以能让不同的语言如 Java、Python 来调用 Hive 的接口。 本节将演示如何使用 Python 代码来连接 HiveServer2。

开发准备

  • 确认您已经开通了腾讯云,并且创建了一个 EMR 集群。在创建 EMR 集群的时候需要在软件配置界面选择 Hive 组件。
  • Hive 等相关软件安装在路径 EMR 云服务器的/usr/local/service/路径下。

查看参数

首先需要登录 EMR 集群中的任意机器,最好是登录到 Master 节点。 在 EMR 命令行先使用以下指令切换到 Hadoop 用户,并进入 Hive 安装文件夹:

代码语言:javascript
复制
[root@172 ~]# su Hadoop
[hadoop@172 root]$ cd /usr/local/service/hive/
[hadoop@172 hive]$

查看在程序中需要使用的参数:

代码语言:javascript
复制
[hadoop@172 hive]$ vim conf/hive-site.xml

<property>
        <name>hive.server2.thrift.bind.host</name>
        <value>$hs2host</value>
</property>
<property>
        <name>hive.server2.thrift.port</name>
        <value>$hs2port</value>
</property>

其中 hs2host 为您的Hiveserver2的hostID,hs2port 为您的 HiveServer2 的端口号。

使用 Python 进行 Hive 操作

使用 Python 程序操作 Hive 需要安装 pip:

代码语言:javascript
复制
[hadoop@172 hive]$ su
[root@172 hive]# pip install pyhs2

安装完成后切换回 Hadoop 用户。 在/usr/local/service/hive/目录下新建一个 Python 文件 hivetest.py,并且添加以下代码:

代码语言:javascript
复制
#!/usr/bin/env python

import pyhs2
import sys

default_encoding = 'utf-8'

conn = pyhs2.connect(host='$hs2host',
                                  port=$hs2port,
                                  authMechanism='PLAIN',
                                  user='hadoop',
                                  password='',
                                  database='default',)


tablename = 'HiveByPython'
cur = conn.cursor()
print 'show the databases: '
print cur.getDatabases()

print "\n"
print 'show the tables in default: '
cur.execute('show tables')
for i in cur.fetch():
        print i

cur.execute('drop table if exists ' + tablename)
cur.execute('create table ' + tablename + ' (key int,value string)')

print "\n"
print 'show the new table: '
cur.execute('show tables ' +"'" +tablename+"'")
for i in cur.fetch():
        print i

print "\n"
print "contents from " + tablename + ":";
cur.execute('insert into ' + tablename + ' values (42,"hello"),(48,"world")')
cur.execute('select * from ' + tablename)
for i in cur.fetch():
        print i

注意:替换其中 hs2host 为您的 Hiveserver2 的 hostID,hs2port 为您的 HiveServer2 的端口号。

该程序连接 HiveServer2 之后,首先输出所有的数据库,然后显示“default”数据库中的表。创建一个名叫“hivebypython”的表,在表中插入两个数据并输出。运行该程序:

代码语言:javascript
复制
[hadoop@172 hive]$ ./hivetest.py
show the databases: 
[['default', ''], ['hue_test', ''], ['test', '']]


show the tables in default: 
['dd']
['ext_table']
['hive_test']
['hivebypython']


show the new table: 
['hivebypython']


contents from HiveByPython:
[42, 'hello']
[48, 'world']
[root@172 /]# ./hivetest3.py 
show the databases: 
[['default', ''], ['hue_test', ''], ['test', '']]


show the tables in default: 
['dd']
['ext_table']
['hive_test']
['hivebypython']


show the new table: 
['hivebypython']


contents from HiveByPython:
[42, 'hello']
[48, 'world']

我正在参与2023腾讯技术创作特训营第三期有奖征文,组队打卡瓜分大奖!

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 说明
  • 背景
  • 开发准备
  • 查看参数
  • 使用 Python 进行 Hive 操作
相关产品与服务
大数据
全栈大数据产品,面向海量数据场景,帮助您 “智理无数,心中有数”!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com