hadoop常用命令实例

1、查看指定目录下内容:hadoop fs –ls [文件目录]
[root@cdh01 tmp]# hadoop fs -ls -h /tmp
Found 2 items
drwxrwxrwx - hdfs supergroup 0 2016-01-21 10:24 /tmp/.cloudera_health_monitoring_canary_files
drwx-wx-wx - hive supergroup 0 2016-01-21 10:02 /tmp/hive
[root@cdh01 tmp]# hadoop fs -ls -h /
Found 2 items
drwxrwxrwx - hdfs supergroup 0 2016-01-21 10:02 /tmp
drwxrwxr-x - hdfs supergroup 0 2016-01-21 10:01 /user

2、将本地文件夹存储至hadoop上:hadoop fs –put [本地目录] [hadoop目录]
[root@cdh01 /]# mkdir test_put_dir #创建目录
[root@cdh01 /]# chown hdfs:hadoop test_put_dir #赋目录权限给hadoop用户
[root@cdh01 /]# su hdfs #切换到hadoop用户
[hdfs@cdh01 /] l s b i n b o o t d e v d f s d f s b a k e t c h o m e l i b l i b 64 l o s t + f o u n d m e d i a m i s c m n t n e t o p t p r o c r o o t s b i n s e l i n u x s r v s y s t e s t p u t d i r t m p u s r v a r w a w a . t x t w b w b . t x t w y p . t x t [ h d f s @ c d h 01 / ] hadoop fs -put test_put_dir /
[hdfs@cdh01 /]$ hadoop fs -ls /
Found 4 items
drwxr-xr-x - hdfs supergroup 0 2016-01-21 11:07 /hff
drwxr-xr-x - hdfs supergroup 0 2016-01-21 15:25 /test_put_dir
drwxrwxrwt - hdfs supergroup 0 2016-01-21 10:39 /tmp
drwxr-xr-x - hdfs supergroup 0 2016-01-21 10:39 /user

3、在hadoop指定目录内创建新目录:hadoop fs –mkdir [目录地址]
[root@cdh01 /]# su hdfs
[hdfs@cdh01 /]$ hadoop fs -mkdir /hff

4、在hadoop指定目录下新建一个空文件,使用touchz命令:
[hdfs@cdh01 /] h a d o o p f s t o u c h z / t e s t p u t d i r / t e s t n e w f i l e . t x t [ h d f s @ c d h 01 / ] hadoop fs -ls /test_put_dir
Found 1 items
-rw-r–r– 3 hdfs supergroup 0 2016-01-21 15:29 /test_put_dir/test_new_file.txt

5、将本地文件存储至hadoop上:hadoop fs –put [本地地址] [hadoop目录]
[hdfs@cdh01 /] hadoop fs -put wyp.txt /hff #直接目录  [hdfs@cdh01 /] hadoop fs -put wyp.txt hdfs://cdh01.cap.com:8020/hff #服务器目录
注:文件wyp.txt放在/根目录下,结构如:
bin dfs_bak lib64 mnt root sys var
boot etc lost+found net sbin test_put_dir wawa2.txt
dev home media opt selinux tmp wbwb.txt
dfs lib misc proc srv usr wyp.txt

6、打开某个已存在文件:hadoop fs –cat [file_path]
[hdfs@cdh01 /]$ hadoop fs -cat /hff/wawa.txt
1 张三 男 135
2 刘丽 女 235
3 王五 男 335

7、将hadoop上某个文件重命名hadoop fs –mv [旧文件名] [新文件名]
[hdfs@cdh01 /]$ hadoop fs -mv /tmp /tmp_bak #修改文件夹名

8、将hadoop上某个文件down至本地已有目录下:hadoop fs -get [文件目录] [本地目录]
[hdfs@cdh01 /] h a d o o p f s g e t / h f f / w a w a . t x t / t e s t p u t d i r [ h d f s @ c d h 01 / ] ls -l /test_put_dir/
total 4
-rw-r–r– 1 hdfs hdfs 42 Jan 21 15:39 wawa.txt

9、删除hadoop上指定文件:hadoop fs -rm [文件地址]
[hdfs@cdh01 /] h a d o o p f s l s / t e s t p u t d i r / F o u n d 2 i t e m s r w r r 3 h d f s s u p e r g r o u p 0 2016 01 21 15 : 41 / t e s t p u t d i r / n e w 2. t x t r w r r 3 h d f s s u p e r g r o u p 0 2016 01 21 15 : 29 / t e s t p u t d i r / t e s t n e w f i l e . t x t [ h d f s @ c d h 01 / ] hadoop fs -rm /test_put_dir/new2.txt
16/01/21 15:42:24 INFO fs.TrashPolicyDefault: Namenode trash configuration: Deletion interval = 1440 minutes, Emptier interval = 0 minutes.
Moved: ‘hdfs://cdh01.cap.com:8020/test_put_dir/new2.txt’ to trash at: hdfs://cdh01.cap.com:8020/user/hdfs/.Trash/Current
[hdfs@cdh01 /]$ hadoop fs -ls /test_put_dir/
Found 1 items
-rw-r–r– 3 hdfs supergroup 0 2016-01-21 15:29 /test_put_dir/test_new_file.txt

10、删除hadoop上指定文件夹(包含子目录等):hadoop fs –rm -r [目录地址]
[hdfs@cdh01 /] h a d o o p f s r m r / t e s t p u t d i r 16 / 01 / 21 15 : 50 : 59 I N F O f s . T r a s h P o l i c y D e f a u l t : N a m e n o d e t r a s h c o n f i g u r a t i o n : D e l e t i o n i n t e r v a l = 1440 m i n u t e s , E m p t i e r i n t e r v a l = 0 m i n u t e s . M o v e d : h d f s : / / c d h 01. c a p . c o m : 8020 / t e s t p u t d i r t o t r a s h a t : h d f s : / / c d h 01. c a p . c o m : 8020 / u s e r / h d f s / . T r a s h / C u r r e n t [ h d f s @ c d h 01 / ] hadoop fs -ls /
Found 3 items
drwxr-xr-x - hdfs supergroup 0 2016-01-21 11:07 /hff
drwxrwxrwt - hdfs supergroup 0 2016-01-21 10:39 /tmp
drwxr-xr-x - hdfs supergroup 0 2016-01-21 15:42 /user

11、将hadoop指定目录下所有内容保存为一个文件,同时down至本地
hadoop dfs –getmerge /user /home/t

扫描二维码关注公众号,回复: 69500 查看本文章

12、将正在运行的hadoop作业kill掉
hadoop job –kill [job-id]

猜你喜欢

转载自blog.csdn.net/u010515155/article/details/80091369
今日推荐