hdfscli 命令行

  1. # hdfscli --help
  2. HdfsCLI: a command line interface for HDFS.
  3.  
  4. Usage:
  5. hdfscli [interactive] [-a ALIAS] [-v...]
  6. hdfscli download [-fsa ALIAS] [-v...] [-t THREADS] HDFS_PATH LOCAL_PATH
  7. hdfscli upload [-sa ALIAS] [-v...] [-A | -f] [-t THREADS] LOCAL_PATH HDFS_PATH
  8. hdfscli -L | -V | -h
  9.  
  10. Commands:
  11. download Download a file or folder from HDFS. If a
  12. single file is downloaded, - can be
  13. specified as LOCAL_PATH to stream it to
  14. standard out.
  15. interactive Start the client and expose it via the python
  16. interpreter (using iPython if available).
  17. upload Upload a file or folder to HDFS. - can be
  18. specified as LOCAL_PATH to read from standard
  19. in.
  20.  
  21. Arguments:
  22. HDFS_PATH Remote HDFS path.
  23. LOCAL_PATH Path to local file or directory.
  24.  
  25. Options:
  26. -A --append Append data to an existing file. Only supported
  27. if uploading a single file or from standard in.
  28. -L --log Show path to current log file and exit.
  29. -V --version Show version and exit.
  30. -a ALIAS --alias=ALIAS Alias of namenode to connect to.
  31. -f --force Allow overwriting any existing files.
  32. -s --silent Don't display progress status.
  33. -t THREADS --threads=THREADS Number of threads to use for parallelization.
  34. 0 allocates a thread per file. [default: 0]
  35. -v --verbose Enable log output. Can be specified up to three
  36. times (increasing verbosity each time).
  37.  
  38. Examples:
  39. hdfscli -a prod /user/foo
  40. hdfscli download features.avro dat/
  41. hdfscli download logs/1987-03-23 - >>logs
  42. hdfscli upload -f - data/weights.tsv <weights.tsv
  43.  
  44. HdfsCLI exits with return status 1 if an error occurred and 0 otherwise.

  

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

  1. # cat ~/.hdfscli.cfg
  2. [global]
  3. default.alias = dev
  4.  
  5. [dev.alias]
  6. url = http://hadoop:50070
  7. 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 交互模式

  1. [root@hadoop ~]# hdfscli --alias=dev
  2.  
  3. Welcome to the interactive HDFS python shell.
  4. The HDFS client is available as `CLIENT`.
  5.  
  6. >>> CLIENT.list("/")
  7. [u'Demo', u'hdfs', u'logs', u'logss']
  8. >>> CLIENT.status("/Demo")
  9. {u'group': u'supergroup', u'permission': u'755', u'blockSize': 0,
  10. u'accessTime': 0, u'pathSuffix': u'', u'modificationTime': 1495123035501L,
  11. u'replication': 0, u'length': 0, u'childrenNum': 1, u'owner': u'root',
  12. u'type': u'DIRECTORY', u'fileId': 16389}
  13. >>> CLIENT.delete("logs/install.log")
  14. False
  15. >>> CLIENT.delete("/logs/install.log")
  16. True

  

与python接口的绑定

  初始化客户端

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

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

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

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

  

  读文件

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

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

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

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

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

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

  写文件

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

  1. >>> with open("/root/test/kong.txt") as reader, client.write("/logs/kongtest.txt") as writer:
  2. ... for line in reader:
  3. ... if line.startswith("-"):
  4. ... 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. javascript闭包获取table中tr的索引 分类: JavaScript 2015-05-04 15:10 793人阅读 评论(0) 收藏

    使用javascript闭包获取table标签中tr的索引 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN& ...

  2. postman—环境切换和设置变量

    postman提供了environment管理功能,想要在多个环境中测试,比如在测试环境.灰度环境.生产环境等,只需要用同样的接口,切换下环境即可,非常方便.具体步骤: 一.切换环境 1.点击界面右上 ...

  3. SPP(Spatial Pyramid Pooling)详解

    一直对Fast RCNN中ROI Pooling层不解,不同大小的窗口输入怎么样才能得到同样大小的窗口输出呢,今天看到一篇博文讲得挺好的,摘录一下,方便查找. Introduction 在一般的CNN ...

  4. 2-nginx 安装

    1, nginx简介: •Nginx("engine x") 是一个高性能的HTTP 和反向代理服务器,也是一个IMAP/POP3/SMTP 代理服务器.•第一个公开版本0.1.0 ...

  5. jQuery操纵cookie(原生javascript处理cookie)

    jQuery也是可以操作cookie的 1.首先下载jQuery.js 以及 jquery.cookie.js 这两个文件 2.安装(其实就是引用) <html>       <he ...

  6. Linux的MySQL不能远程访问

    1.首先,你要确认用户是否只允许localhost访问: 在linux下登录mysql mysql -uroot -p密码;     use mysql;     select `host`,`use ...

  7. Nodejs学习笔记(三)—模块

    简介及资料 通过Node.js的官方API可以看到Node.js本身提供了很多核心模块 http://nodejs.org/api/ ,这些核心模块被编译成二进制文件,可以require('模块名') ...

  8. 【Xmail】使用Xmail搭建局域网邮件服务器

    下载地址:  http://www.xmailserver.org/xmail-1.27.win32bin.zip,当前最新版本  1.27. 解压文件:xmail-1.27.win32bin.zip ...

  9. Walkway.js – 创建简约的 SVG 线条动画

    Walkway.js 是一个使用线条和路径元素组成 SVG 动画图像的简单方法.只需根据提供的配置对象创建一个新的 Walkway 实例就可以了.这种效果特别适合那些崇尚简约设计风格的网页.目前, W ...

  10. 转载:@Html.ValidationSummary(true)

    ASP.NET MVC3 Model验证总结 @Html.ValidationSummary(true)   http://www.wyjexplorer.cn/Post/2012/8/3/model ...