python读取hdfs上的parquet文件
在使用python做大数据和机器学习处理过程中,首先需要读取hdfs数据,对于常用格式数据一般比较容易读取,parquet略微特殊。从hdfs上使用python获取parquet格式数据的方法(当然也可以先把文件拉到本地再读取也可以):
1、安装anaconda环境。
2、安装hdfs3。
conda install hdfs3
3、安装fastparquet。
conda install fastparquet
4、安装python-snappy。
conda install python-snappy
5、读取文件
##namenode mode:
from hdfs3 import HDFileSystem
from fastparquet import ParquetFile
hdfs = HDFileSystem(host=IP, port=8020)
sc =
pf = ParquetFile(filename, open_with=sc)
df = pf.to_pandas()
##返回pandas的DataFrame类型
##HA mode:
from hdfs3 import HDFileSystem
from fastparquet import ParquetFile
host = "nameservice1"
conf = {
"":"nameservice1",
......
}
hdfs = HDFileSystem(host = host, pars = conf)
......
python访问HDFS HA的三种方法
python访问hdfs常用的包有三个,如下:
1、hdfs3
其实从安装便捷性和使用上来说,并不推荐hdfs3,因为他的系统依赖和网络要求较高,但是某些情况下使用hdfs3会比较方便,官网资料点这里。如上面介绍,IP直接访问namenode:
from hdfs3 import HDFileSystem
hdfs = HDFileSystem(host=namenode, port=8020)
('/tmp')
HA访问:
host = "nameservice1"
conf = {"": "nameservice1",
".nameservice1": "namenode113,namenode188",
"-address.nameservice1.namenode113": "hostname_of_server1:8020",
"-address.nameservice1.namenode188": "hostname_of_server2:8020",
"-address.nameservice1.namenode188": "hostname_of_server1:50070",
"-address.nameservice1.namenode188": "hostname_of_server2:50070",
"": "kerberos"
}
fs = HDFileSystem(host=host, pars=conf)
##或者下面这种配置
host = "ns1"
conf = {
"":"ns1",
".ns1":"namenode122,namenode115",
"-address.ns1.namenode122":"nnlab01:8020",
"-address.ns1.namenode122":"nnlab01:8022",
"-address.ns1.namenode122":"nnlab01:50070",
"-address.ns1.namenode122":"nnlab01:50470",
"-address.ns1.namenode115":"nnlab02:8020",
"-address.ns1.namenode115":"nnlab02:8022",
"-address.ns1.namenode115":"nnlab02:50070",
"-address.ns1.namenode115":"nnlab02:50470",
}
hdfs = HDFileSystem(host = host, pars = conf)
2、hdfs
这种方法在使用的时候配置比较简单,官网资料也比较丰富,但是需要注意的是该API可以模拟用户访问,权限较大。IP直接访问:
import hdfs
client = (url="http://namenode:50070", user="hdfs")
HA访问:
import hdfs
client = (url="http://namenode1:50070;http://namenode2:50070", user="hdfs")
3、pyhdfs
安装命令:pip install PyHDFS
官网地址,直接访问:
import pyhdfs
client = (hosts="namenode:50070",user_name="hdfs")
HA访问
import pyhdfs
client = (hosts=["namenode1:50070","namenode2:50070"],user_name="hdfs")