hadoop經常使用命令實例

一、查看指定目錄下內容: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

二、將本地文件夾存儲至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 /]$ ls
bin  boot  dev  dfs  dfs_bak  etc  home  lib  lib64  lost+found  media  misc  mnt  net  opt  proc  root  sbin  selinux  srv  sys  test_put_dir  tmp  usr  var  wawa.txt  wbwb.txt  wyp.txt
[hdfs@cdh01 /]$ 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

三、在hadoop指定目錄內建立新目錄:hadoop fs –mkdir [目錄地址]
[root@cdh01 /]# su hdfs
[hdfs@cdh01 /]$ hadoop fs -mkdir /hff

四、在hadoop指定目錄下新建一個空文件,使用touchz命令:
[hdfs@cdh01 /]$ hadoop fs -touchz /test_put_dir/test_new_file.txt
[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

五、將本地文件存儲至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

六、打開某個已存在文件:hadoop fs –cat [file_path]
[hdfs@cdh01 /]$ hadoop fs -cat /hff/wawa.txt
1       張三    男      135
2       劉麗    女      235
3       王五    男      335

七、將hadoop上某個文件重命名hadoop fs –mv [舊文件名] [新文件名]
[hdfs@cdh01 /]$ hadoop fs -mv /tmp /tmp_bak #修改文件夾名

八、將hadoop上某個文件down至本地已有目錄下:hadoop fs -get [文件目錄] [本地目錄]
[hdfs@cdh01 /]$ hadoop fs -get /hff/wawa.txt /test_put_dir
[hdfs@cdh01 /]$ ls -l /test_put_dir/
total 4
-rw-r--r-- 1 hdfs hdfs 42 Jan 21 15:39 wawa.txt

九、刪除hadoop上指定文件:hadoop fs -rm [文件地址]
[hdfs@cdh01 /]$ hadoop fs -ls /test_put_dir/
Found 2 items
-rw-r--r--   3 hdfs supergroup          0 2016-01-21 15:41 /test_put_dir/new2.txt
-rw-r--r--   3 hdfs supergroup          0 2016-01-21 15:29 /test_put_dir/test_new_file.txt
[hdfs@cdh01 /]$ 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

十、刪除hadoop上指定文件夾(包含子目錄等):hadoop fs –rm -r [目錄地址]
[hdfs@cdh01 /]$ hadoop fs -rmr /test_put_dir
16/01/21 15:50:59 INFO fs.TrashPolicyDefault: Namenode trash configuration: Deletion interval = 1440 minutes, Emptier interval = 0 minutes.
Moved: 'hdfs://cdh01.cap.com:8020/test_put_dir' to trash at: hdfs://cdh01.cap.com:8020/user/hdfs/.Trash/Current
[hdfs@cdh01 /]$ 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

十一、將hadoop指定目錄下全部內容保存爲一個文件,同時down至本地
hadoop dfs –getmerge /user /home/t

十二、將正在運行的hadoop做業kill掉
hadoop job –kill  [job-id]node

相關文章
相關標籤/搜索