httpfs是hadoop中HDFS over HTTP的实现,为HDFS的读写操作提供了统一的REST HTTP接口。在一些特定场景下非常有用,例如不同hadoop版本集群间数据拷贝, 使用httpfs作为对外提供数据访问的网关等。
httpfs组件与namenode内置的http接口都能提供webhdfs http接口,但二者不同的是httpfs可以作为独立服务部署到其他节点上,从而与namenode互相独立。
在emr V2版本中已经默认在master节点上启动了httpfs组件,无需单独部署和启动,emr V1版本或者在非emr节点的客户机中启动方式如下:
su - hadoop
/usr/local/service/hadoop/sbin/httpfs.sh start
emr中默认配置的httpfs端口为4032,启动之后即可通过下面方式进行测试:
$ curl "localhost:4032/webhdfs/v1?user.name=hadoop&op=gethomedirectory"
{"Path":"\/user\/hadoop"}
通过hdfs客户端进行测试:
$ hdfs dfs -ls webhdfs://localhost:4032/
Found 7 items
drwxr-xr-x - hadoop supergroup 0 2018-05-25 15:28 webhdfs://localhost:4032/apps
drwxrwx--- - hadoop supergroup 0 2018-05-31 19:06 webhdfs://localhost:4032/emr
drwxr-xr-x - hadoop supergroup 0 2018-05-29 15:36 webhdfs://localhost:4032/hbase
drwxr-xr-x - hadoop supergroup 0 2018-06-06 16:49 webhdfs://localhost:4032/spark-history
drwx-wx-wx - hadoop supergroup 0 2018-05-28 15:09 webhdfs://localhost:4032/tmp
drwxr-xr-x - hadoop supergroup 0 2018-05-28 15:06 webhdfs://localhost:4032/user
drwxr-xr-x - hadoop supergroup 0 2018-05-31 19:04 webhdfs://localhost:4032/usr
通过python hdfs模块访问测试:
将测试代码保存至hdfs_t.py, 测试代码如下:
#!/usr/bin/env python
from hdfs import InsecureClient
client = InsecureClient('http://localhost:4032', user='hadoop')
# Read a file
with client.read('/tmp/1.txt') as reader:
txt = reader.read()
print txt
# Write a file
data_str = "test hdfs write"
client.write('/tmp/2.txt', data=data_str, encoding='utf-8')
# Upload a file
client.upload('/tmp/', './testhttpfs', n_threads=5)
执行以下命令安装模块并执行测试脚本:
# 安装模块
yum -y install python-pip
pip install hdfs
# 准备测试文件
date >1.txt
hdfs dfs -put 1.txt /tmp/
# 执行测试脚本
python hdfs_t.py
webhdfs api文档参考链接:
https://hadoop.apache.org/docs/stable/hadoop-project-dist/hadoop-hdfs/WebHDFS.html
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。