执行本地文件加载在hive中的py脚本及依赖安装记录

脚本demo

脚本功能:把本地文件加载在hive中

#!/usr/bin/env python 
# encoding: utf-8 

"""
@version: v1.0
@author: chey
@contact: ~
@software: PyCharm
@file: test.py.py
@time: 2019-07-03 15:56
"""

"""
创建分区表:
CREATE TABLE test_part
(
name string,
sex string
)
partitioned by (month string)
row format delimited fields terminated by '\t';
"""
"""
t1.txt
陈二	男生	201907
张三	男生	201907
里斯	女生	201907
王五	女生	201907
"""
from pyhive import hive

# 创建连接
conn = hive.Connection(host='xx.xx.xx.xxx', port=10000, username='bigdata', database='default')
cursor = conn.cursor()

# 创建目录
cursor.execute("dfs -mkdir -p /user/hive/warehouse/test_part/month=201907")
# 上传目录
cursor.execute("load data local inpath '/opt/datas/t1.txt' into table test_part partition(month='201907')")

# 查询
cursor.execute("select * from test_part where month='201907'")
for result in cursor.fetchall():
    print(result)


依赖安装记录

$ sudo pip3 install pyhive
$ sudo pip3 install thrift
$ sudo pip3 install sasl
$ sudo yum install gcc-c++ python-devel.x86_64 cyrus-sasl-devel.x86_64
$ sudo pip3 install sasl
$ sudo pip3 install thrift_sasl

总结一下:

  • 要load的数据文件必须在hiveserver2所在节点,且文件所在的这个文件夹要有x权限,不然执行不然load操作,报“Invalid path ‘/path’:No files matching path file”异常。
  • 可以使用LOAD DATA方式加载HDFS上的数据,就不会有这样的限制。
  • hiveserver2所在节点不必要装python环境,哪个节点跑python脚本就在那个节点装下python3环境以及依赖库即可。
  • 跑python脚本的节点可以是集群上的节点,也可以是集群外的节点,更没必要一定放在有hive的节点上执行。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值