大家好,喜欢Bigdata的阿尼亚来了!希望大家会喜欢阿尼亚的文章!!哇酷哇酷!!!
本次为师傅们带来的是“红亚杯”数据分析进阶—使用Python操作Hive专题赛——满分解析系列的第③期,是“使用Python操作Hive”篇章哦!
第①期完整赛题和第②期配置Hive并开启相关服务的链接在下面,师傅们想看完整赛题的请安心享用:
【阿尼亚喜欢BigData】“红亚杯”数据分析进阶—使用Python操作Hive专题赛——满分解析①_爱波吉的阿尼亚的博客-CSDN博客
【阿尼亚喜欢BigData】“红亚杯”数据分析进阶—使用Python操作Hive专题赛——满分解析②_爱波吉的阿尼亚的博客-CSDN博客
目录
1. 创建文件/root/hs2/task1.py,编写程序连接hive,查看所有数据库,运行程序并打印结果。
2. 创建文件/root/hs2/task2.py,编写程序连接hive,创建数据库qingjiao(如不存在则创建)。
4. 创建文件/root/hs2/task3.py,编写程序连接hive,创建数据表student并添加数据(具体见步骤说明)
5. 创建文件/root/hs2/task4.py,编写程序连接hive,查询数据表student下所有数据,执行程序,查看打印结果
6. 创建文件/root/hs2/task5.py,编写程序连接hive,定时查询student数据,具体见步骤说明。
使用Python操作Hive(60.00 / 60分)
python3使用pyhive所需库及软件,命令如下:(需要手动下载,无需修改本地源)
# 所需软件
yum -y install cyrus-sasl cyrus-sasl-devel cyrus-sasl-lib
yum -y install cyrus-sasl-plain cyrus-sasl-devel cyrus-sasl-gssapi
# 所需依赖库
pip install sasl
pip install thrift
pip install thrift-sasl
pip install pyhive
考核条件如下:
1. 创建文件/root/hs2/task1.py,编写程序连接hive,查看所有数据库,运行程序并打印结果。
操作环境: python-hive
from pyhive import hive
HOST='hadoop000_IP'
PORT=10000
USERNAME='root'
PASSWORD='123456'
AUTH='CUSTOM'
conn = hive.Connection(host=HOST,port=PORT,username=USERNAME,password=PASSWORD,auth=AUTH)
cursor = conn.cursor()
cursor.execute('show databases')
print(cursor.fetchall())
cursor.close()
conn.commit()
conn.close()
2. 创建文件/root/hs2/task2.py,编写程序连接hive,创建数据库qingjiao(如不存在则创建)。
操作环境: python-hive
from pyhive import hive
HOST='hadoop000_IP'
PORT=10000
USERNAME='root'
PASSWORD='123456'
AUTH='CUSTOM'
conn = hive.Connection(host=HOST,port=PORT,username=USERNAME,password=PASSWORD,auth=AUTH)
cursor = conn.cursor()
cursor.execute('create database if not exists qingjiao')
cursor.close()
conn.commit()
conn.close()
3. 验证数据库青椒上是否创建成功
操作环境: python-hive
from pyhive import hive
HOST='hadoop000_IP'
PORT=10000
USERNAME='root'
PASSWORD='123456'
AUTH='CUSTOM'
conn = hive.Connection(host=HOST,port=PORT,username=USERNAME,password=PASSWORD,auth=AUTH)
cursor = conn.cursor()
cursor.execute('show databases')
print(cursor.fetchall())
cursor.close()
conn.commit()
conn.close()
4. 创建文件/root/hs2/task3.py,编写程序连接hive,创建数据表student并添加数据(具体见步骤说明)
操作环境: python-hive
from pyhive import hive
HOST='hadoop000_IP'
PORT=10000
USERNAME='root'
PASSWORD='123456'
AUTH='CUSTOM'
conn = hive.Connection(host=HOST,port=PORT,username=USERNAME,password=PASSWORD,auth=AUTH)
cursor = conn.cursor()
cursor.execute('use qingjiao')
cursor.execute('create table student(id string,name string)')
cursor.execute('insert into student values("001","python")')
cursor.execute('insert into student values("002","hive")')
cursor.close()
conn.commit()
conn.close()
5. 创建文件/root/hs2/task4.py,编写程序连接hive,查询数据表student下所有数据,执行程序,查看打印结果
操作环境: python-hive
from pyhive import hive
HOST='hadoop000_IP'
PORT=10000
USERNAME='root'
PASSWORD='123456'
AUTH='CUSTOM'
conn = hive.Connection(host=HOST,port=PORT,username=USERNAME,password=PASSWORD,auth=AUTH)
cursor = conn.cursor()
cursor.execute('use qingjiao')
cursor.execute('select * from student')
for result in cursor.fetchall():
print(result)
cursor.close()
conn.commit()
conn.close()
6. 创建文件/root/hs2/task5.py,编写程序连接hive,定时查询student数据,具体见步骤说明。
操作环境: python-hive
from pyhive import hive
import time
from datetime import datetime
def Get_Data_From_Hive():
HOST='hadoop000_IP'
PORT=10000
USERNAME='root'
PASSWORD='123456'
AUTH='CUSTOM'
conn = hive.connection(host=HOST,port=PORT,username=USERNAME,password=PASSWORD,auth=AUTH)
cursor = conn.cursor()
cursor.execute('use qingjiao')
count=cur.execute('select * from student')
for result in cursor.fetchall():
print(result)
cursor.close()
conn.commit()
conn.close()
while True:
if datetime.now() < datetime(2021,12,31,23,59,59):
Get_Data_From_Hive()
time.sleep(10)
else:
break