【阿尼亚喜欢BigData】“红亚杯”数据分析进阶—使用Python操作Hive专题赛——满分解析③

大家好,喜欢Bigdata的阿尼亚来了!希望大家会喜欢阿尼亚的文章!!哇酷哇酷!!!

本次为师傅们带来的是“红亚杯”数据分析进阶—使用Python操作Hive专题赛——满分解析系列的第③期,是“使用Python操作Hive”篇章哦!

第①期完整赛题和第②期配置Hive并开启相关服务的链接在下面,师傅们想看完整赛题的请安心享用:

【阿尼亚喜欢BigData】“红亚杯”数据分析进阶—使用Python操作Hive专题赛——满分解析①_爱波吉的阿尼亚的博客-CSDN博客

【阿尼亚喜欢BigData】“红亚杯”数据分析进阶—使用Python操作Hive专题赛——满分解析②_爱波吉的阿尼亚的博客-CSDN博客

目录

 使用Python操作Hive(60.00 / 60分)

1. 创建文件/root/hs2/task1.py,编写程序连接hive,查看所有数据库,运行程序并打印结果。

2. 创建文件/root/hs2/task2.py,编写程序连接hive,创建数据库qingjiao(如不存在则创建)。

3. 验证数据库青椒上是否创建成功

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

爱波吉的阿尼亚

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值