原文地址:http://zh.hortonworks.com/hadoop-tutorial/using-commandline-manage-files-hdfs/

In this tutorial we will walk through some of the basic HDFS commands you will need to manage files on HDFS. To complete this tutorial you will need a working HDP cluster. The easiest way to have a Hadoop cluster is to download the Hortonworks Sandbox.

Let’s get started.

Step 1: Let’s create a directory in HDFS, upload a file and list.

Let’s look at the syntax first:

hadoop fs -mkdir:
  • It will take path uri’s as argument and creates directory or directories.
    Usage:
hadoop fs -mkdir <paths>
Example:
hadoop fs -mkdir /user/hadoop/dir1 /user/hadoop/dir2
hadoop fs -mkdir hdfs://nn1.example.com/user/hadoop/dir
hadoop fs -ls:
  • Lists the contents of a directory
  • For a file returns stats of a file
    Usage:
hadoop fs -ls <args>
Example:
hadoop fs -ls /user/hadoop/dir1 /user/hadoop/dir2
hadoop fs -ls /user/hadoop/dir1/filename.txt
hadoop fs -ls hdfs://<hostname>:9000/user/hadoop/dir1/

Let’s use the following commands as follows and execute. You can ssh to the sandbox using Tools like Putty. You could download putty.exe from the internet.

Let’s touch a file locally.

$ touch filename.txt

Step 2: Now, let’s check how to find out space utilization in a HDFS dir.

hadoop fs -du:
  • Displays sizes of files and directories contained in the given directory or the size of a file if its just a file.
    Usage:
hadoop fs -du URI
Example:
hadoop fs -du /user/hadoop/ /user/hadoop/dir1/Sample.txt

Step 4:

Now let’s see how to upload and download files from and to Hadoop Data File System(HDFS)
Upload: ( we have already tried this earlier)

hadoop fs -put:
  • Copy single src file, or multiple src files from local file system to the Hadoop data file system
    Usage:
hadoop fs -put <localsrc> ... <HDFS_dest_Path>
Example:
hadoop fs -put /home/ec2-user/Samplefile.txt ./ambari.repo /user/hadoop/dir3/

Download:
hadoop fs -get:

  • Copies/Downloads files to the local file system
    Usage:
hadoop fs -get <hdfs_src> <localdst>
Example:
hadoop fs -get /user/hadoop/dir3/Samplefile.txt /home/

Step 5: Let’s look at quickly two advanced features.

hadoop fs -getmerge
  • Takes a source directory files as input and concatenates files in src into the destination local file.
    Usage:
hadoop fs -getmerge <src> <localdst> [addnl]
Example:
hadoop fs -getmerge /user/hadoop/dir1/ ./Samplefile2.txt
Option:
addnl: can be set to enable adding a newline on end of each file
hadoop distcp:
  • Copy file or directories recursively
  • It is a tool used for large inter/intra-cluster copying
  • It uses MapReduce to effect its distribution copy, error handling and recovery, and reporting
    Usage:
hadoop distcp <srcurl> <desturl>
Example:
hadoop distcp hdfs://<NameNode1>:8020/user/hadoop/dir1/ \
hdfs://<NameNode2>:8020/user/hadoop/dir2/

You could use the following steps to perform getmerge and discp.
Let’s upload two files for this exercise first:

# touch txt1 txt2
# hadoop fs -put txt1 txt2 /user/hadoop/dir2/
# hadoop fs -ls /user/hadoop/dir2/

Step 6:Getting help

You can use Help command to get list of commands supported by Hadoop Data File System(HDFS)

    Example:
hadoop fs -help

Hope this short tutorial was useful to get the basics of file management.

Using the command line to manage files on HDFS--转载的更多相关文章

  1. How to Use Android ADB Command Line Tool

    Android Debug Bridge (adb) is a tool that lets you manage the state of an emulator instance or Andro ...

  2. 18 Command Line Tools to Monitor Linux Performance

    By Ravi Saive Under: Linux Commands, Monitoring Tools On: December 26, 2013 http://www.tecmint.com/c ...

  3. How to build .apk file from command line(转)

    How to build .apk file from command line Created on Wednesday, 29 June 2011 14:32 If you don’t want ...

  4. Can't use Subversion command line client: svn Probably the path to Subversion executable is wrong. Fix it.

    1.最近使用SVN工具时,Checkout出项目到本地后后,然后将其导入到Intellij idea中开发,在提交svn代码的时候,出现这样的错误:Can't use Subversion comma ...

  5. 使用intellij的svn时提示出错: Can't use Subversion command line client: svn.Errors found while svn working copies detection.

    使用Intellij的svn时提示出错:Can't use Subversion command line client: svn. Errors found while svn working co ...

  6. MySQL 5.7 Command Line Client输入密码后闪退和windows下mysql忘记root密码的解决办法

    MySQL 5.7 Command Line Client输入密码后闪退的问题: 问题分析: 1.查看mysql command line client默认执行的一些参数.方法:开始->所有程序 ...

  7. 《The Linux Command Line》 读书笔记01 基本命令介绍

    <The Linux Command Line> 读书笔记01 基本命令介绍 1. What is the Shell? The Shell is a program that takes ...

  8. Linux Command Line Basics

    Most of this note comes from the Beginning the Linux Command Line, Second Edition by Sander van Vugt ...

  9. python click module for command line interface

    Click Module(一)                                                  ----xiaojikuaipao The following mat ...

随机推荐

  1. 20155316 2016-2017-2 《Java程序设计》第10周学习总结

    教材学习内容总结 Java和Android开发学习指南(第二版)第22章 Java 密码技术 教材学习中的问题和解决过程 1.什么叫柯克霍夫原则? 数据的安全基于密钥而不是算法的保密. 也就是说系统的 ...

  2. 20155336虎光元实验四 Android开发基础

    20155336虎光元实验四 Android开发基础 实验内容 1:完成Hello World, 要求修改res目录中的内容,Hello World后要显示自己的学号 2:创建 ThirdActivi ...

  3. 海思NB-IOT HI2115芯片电压域的问题

    1. 先看模块引脚,利尔达NB86-G模块 2. VDD_IO_R1, VDD_IO_R2, VDD_IO_L1 and VDD_IO_L2 control the IO voltages on PI ...

  4. 向大学说拜拜——大学 > 兴趣 + 时间 + 思考 + 实践

    [人物素描] 大学期间,担任过班委,加入过学生会,参加过社团,拿过奖学金......而印象最深刻的莫过于参加并组织过ACM集训,以及参加过导师的国家自然科学基金项目了.毕业时顺利拿到一波offer,并 ...

  5. runtime如何实现weak属性

    首先了解weak是一种非拥有关系,属性所值对象销毁时,属性值会清空(nil). Runtime对注册的类会进行布局,对于weak对象会放入hash表中,用weak指向的内存地址作为key,当对象引用计 ...

  6. 「日常训练」 Fire!(UVA-11624)

    与其说是训练不如说是重温.重新写了Java版本的代码. import java.util.*; import java.math.*; import java.io.BufferedInputStre ...

  7. [转]Git 撤销操作

    二. Git撤消操作 12.1 修改最后一次提交 git commit --amend 1.新建一个文件 2.提交一个之前的更改 3.跟踪这个文件 4.跟前一次一起提交 提示你是否重新编辑提交说明,如 ...

  8. Vue2 v-bind:href 中如何使用过滤器

    <a class="topic_title" v-bind:href="info.id|getTitleHref" v-bind:title=" ...

  9. mysql查看锁表与解锁

    查看锁表语句 show OPEN TABLES where In_use > 0; 找到锁表的进程 show processlist;  删除锁表进程 kill 51045123;

  10. Java GUI 点击按钮退出

    import java.awt.*; import java.awt.event.*; public class TestFrameTwo implements ActionListener { Fr ...