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

知识共享许可协议 版权声明:署名,允许他人基于本文进行创作,且必须基于与原先许可协议相同的许可协议分发本文 (Creative Commons

脚本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的节点上执行。

猜你喜欢

转载自blog.csdn.net/qq_30552441/article/details/94576094