hdfscli 命令行

# hdfscli --help
HdfsCLI: a command line interface for HDFS. Usage:
hdfscli [interactive] [-a ALIAS] [-v...]
hdfscli download [-fsa ALIAS] [-v...] [-t THREADS] HDFS_PATH LOCAL_PATH
hdfscli upload [-sa ALIAS] [-v...] [-A | -f] [-t THREADS] LOCAL_PATH HDFS_PATH
hdfscli -L | -V | -h Commands:
download Download a file or folder from HDFS. If a
single file is downloaded, - can be
specified as LOCAL_PATH to stream it to
standard out.
interactive Start the client and expose it via the python
interpreter (using iPython if available).
upload Upload a file or folder to HDFS. - can be
specified as LOCAL_PATH to read from standard
in. Arguments:
HDFS_PATH Remote HDFS path.
LOCAL_PATH Path to local file or directory. Options:
-A --append Append data to an existing file. Only supported
if uploading a single file or from standard in.
-L --log Show path to current log file and exit.
-V --version Show version and exit.
-a ALIAS --alias=ALIAS Alias of namenode to connect to.
-f --force Allow overwriting any existing files.
-s --silent Don't display progress status.
-t THREADS --threads=THREADS Number of threads to use for parallelization.
0 allocates a thread per file. [default: 0]
-v --verbose Enable log output. Can be specified up to three
times (increasing verbosity each time). Examples:
hdfscli -a prod /user/foo
hdfscli download features.avro dat/
hdfscli download logs/1987-03-23 - >>logs
hdfscli upload -f - data/weights.tsv <weights.tsv HdfsCLI exits with return status 1 if an error occurred and 0 otherwise.

  

要使用hdfscli,首先需要设置hdfscli的默认配置文件

# cat ~/.hdfscli.cfg
[global]
default.alias = dev [dev.alias]
url = http://hadoop:50070
user = root

  python可用的客户端类:

    InsecureClient(default)

    TokenClient

上传或下载文件

使用hdfscli上传文件或文件夹(将hadoop文件夹上传到/hdfs)

  # hdfscli upload --alias=dev -f /hadoop-2.4.1/etc/hadoop/ /hdfs

使用hdfscli下载/logs目录到操作系统的/root/test目录下

  # hdfscli download /logs /root/test/

hdfscli 交互模式

[root@hadoop ~]# hdfscli --alias=dev

Welcome to the interactive HDFS python shell.
The HDFS client is available as `CLIENT`. >>> CLIENT.list("/")
[u'Demo', u'hdfs', u'logs', u'logss']
>>> CLIENT.status("/Demo")
{u'group': u'supergroup', u'permission': u'755', u'blockSize': 0,
u'accessTime': 0, u'pathSuffix': u'', u'modificationTime': 1495123035501L,
u'replication': 0, u'length': 0, u'childrenNum': 1, u'owner': u'root',
u'type': u'DIRECTORY', u'fileId': 16389}
>>> CLIENT.delete("logs/install.log")
False
>>> CLIENT.delete("/logs/install.log")
True

  

与python接口的绑定

  初始化客户端

  1、导入client类,然后调用它的构造函数

>>> from hdfs import InsecureClient
>>> client = InsecureClient("http://172.10.236.21:50070",user='ann')
>>> client.list("/")
[u'Demo', u'hdfs', u'logs', u'logss']

  2、导入config类,加载一个已存在的配置文件并且从已存在的alias创建一个client,配置文件默认的读取文件为~/.hdfs_config.cfg

>>> from hdfs import Config
>>> client=Config().get_client("dev")
>>> client.list("/")
[u'Demo', u'hdfs', u'logs', u'logss']

  

  读文件

  read()方法可从hdfs系统读取一个文件,但是它必须放在with块中,以确保每次都能正确关闭连接

>>> with client.read("/logs/yarn-env.sh",encoding="utf-8") as reader:
... features=reader.read()
...
>>> print features

  chunk_size参数将返回一个生成器,它使文件的内容变成流数据

>>> with client.read("/logs/yarn-env.sh",chunk_size=1024) as reader:
... for chunk in reader:
... print chunk
...

  delimiter参数同样返回一个生成器,文件内容是被指定符号分隔的

>>> with client.read("/logs/yarn-env.sh", encoding="utf-8", delimiter="\n") as reader:
... for line in reader:
... time.sleep(1)
... print line

  写文件

write方法用于写文件到hdfs(将本地文件kong.txt写入hdfs的/logs/kongtest.txt文件中)

>>> with open("/root/test/kong.txt") as reader, client.write("/logs/kongtest.txt") as writer:
... for line in reader:
... if line.startswith("-"):
... writer.write(line)

  

hdfs操作手册的更多相关文章

  1. python基础操作以及hdfs操作

    目录 前言 基础操作 hdfs操作 总结 一.前言        作为一个全栈工程师,必须要熟练掌握各种语言...HelloWorld.最近就被"逼着"走向了python开发之路, ...

  2. HDFS操作

    HDFS操作 1.shell 1.1 创建目录 hadoop fs -mkdir 目录名(其中/为根目录) 1.2 遍历目录 hadoop fs -ls 目录名 1.3 删除目录 hadoop fs ...

  3. (47) odoo详细操作手册

    odoo 8 详细操作手册, ERP(Odoo8.0)操作手册-v1.10(陈伟明).pdf 链接: http://pan.baidu.com/s/1hsp0bVQ 密码: r9tt 花了将近9个月时 ...

  4. SharePoint2010升级到SharePoint2013操作手册

    SharePoint2010升级到SharePoint2013操作手册 目 录 第一章 前言    3 第二章 升级前准备    3 第三章 升级流程图    5 第四章 升级过程    5 4.1 ...

  5. Mysql 操作手册

    mysql操作手册 版本:5.6.16mysql linux安装基本步骤:#rpm -e --nodeps mysql-lib-5.1.*#rpm -ivh mysql-server#rpm -ivh ...

  6. [转]SVN操作手册

    [转]SVN操作手册 2012-04-28 11:26 by NewSea, 2495 阅读, 0 评论, 收藏, 编辑 原文: http://hi.baidu.com/caiqiupeng/blog ...

  7. SVN操作手册(part1&part2)——SVN安装

    SVN操作手册 1.关于SVN 有一个简单但不十分精确比喻: SVN = 版本控制 + 备份服务器 简单的说,您可以把SVN当成您的备份服务器,更好的是,他可以帮您记住每次上传到这个服务器的档案内容. ...

  8. svn 迁移至git操作手册

    svn 迁移至git操作手册 项目交付.版本管理工具变更等情况下,迁移svn旧历史记录有很大必要,方便后续追踪文件的提交历史,文件修改记录比对等.git自带了从svn迁移至git的工具命令,可很好的对 ...

  9. jmeter接口入门操作手册

    基础操作手册:Windows Mr丶菜鸟 1.下载jmeter  ,jmeter是一款基于java的开源工具,可以测试接口和性能,需要jdk环境,下载jmeter地址:https://jmeter.a ...

随机推荐

  1. 全网最详细使用Scrapy时遇到0: UserWarning: You do not have a working installation of the service_identity module: 'cannot import name 'opentype''. Please install it from ..的问题解决(图文详解)

    不多说,直接上干货! 但是在运行爬虫程序的时候报错了,如下: D:\Code\PycharmProfessionalCode\study\python_spider\30HoursGetWebCraw ...

  2. __name__ == "__main__"的作用是什么?

    问题: __name__ == "__main__" 的作用是什么? # Threading example import time, thread def myfunction( ...

  3. 转:Java8内存模型—永久代(PermGen)和元空间(Metaspace)

    一.JVM 内存模型 根据 JVM 规范,JVM 内存共分为虚拟机栈.堆.方法区.程序计数器.本地方法栈五个部分. 1.虚拟机栈:每个线程有一个私有的栈,随着线程的创建而创建.栈里面存着的是一种叫“栈 ...

  4. Mysql备份工具xtraback全量和增量测试

    Mysql备份工具xtraback全量和增量测试   xtrabackup 是 percona 的一个开源项目,可以热备份innodb ,XtraDB,和MyISAM(会锁表) 官方网址http:// ...

  5. bind9配置转发服务

    修改bind主配置文件 $ vi /etc/named.conf//// named.conf//// Provided by Red Hat bind package to configure th ...

  6. SpringMVC 工作原理详解

    本文Github开源项目https://github.com/Snailclimb/JavaGuide,只供自己学习总结无商业用途,如有侵权,联系删除 先来看一下什么是 MVC 模式 MVC 是一种设 ...

  7. 湘潭校赛 Bob's Problem

    Bob's Problem Accepted : 18   Submit : 115 Time Limit : 1000 MS   Memory Limit : 65536 KB  题目描述 Bob今 ...

  8. 解决文字和text-decoration:underline下划线重叠问题

    一.text-decoration:underline下划线的问题 CSS text-decoration:underline可以给内联文本增加下划线,但是,如果对细节要求较高,就会发现,下划线经常会 ...

  9. package.json参数简单介绍

    概述: 每个项目的根目录下都会有一个package.json文件,定义了项目所需的模块,以及项目信息.执行npm install 命令会自动下载package.json中配置的模块,也就是配置项目的运 ...

  10. UVAlive6807 Túnel de Rata (最小生成树)

    题意 题目链接 Sol 神仙题Orz 我们考虑选的边的补集,可以很惊奇的发现,这个补集中的边恰好是原图中的一颗生成树: 并且答案就是所有边权的和减去这个边集中的边的权值: 于是我们只需要求最大生成树就 ...