熟悉經常使用的HDFS操做

1、Hadoop提供的Shell命令完成相同任務:python

  1. 在本地Linux文件系統的「/home/hadoop/」目錄下建立一個文件txt,裏面能夠隨意輸入一些單詞.
  2. 在本地查看文件位置(ls)
  3. 在本地顯示文件內容
    cd /home/hadoop/
    touch test.txt
    cat temp.txt

     

  4. 使用命令把本地文件系統中的「txt」上傳到HDFS中的當前用戶目錄的input目錄下。
  5. 查看hdfs中的文件(-ls)
    cd /usr/local/hadoop
    ./bin/hdfs dfs -put /home/hadoop/test.txt input
    ./bin/hdfs dfs -ls input

     

  6. 顯示hdfs中該的文件內容
  7. 刪除本地的txt文件並查看目錄
    ./bin/hdfs dfs -cat input/test.txt
    rm -rf test.txt
    ls

     

  8. 從hdfs中將txt下載地本地原來的位置。
  9. 從hdfs中刪除txt並查看目錄
    ./bin/hdfs dfs -get input/text.txt /home/hadoop
    ./bin/hdfs dfs -rm -r input/text.txt
    ./bin/hdfs dfs -ls input
    

      

 2、app

  1. 向HDFS中上傳任意文本文件,若是指定的文件在HDFS中已經存在,由用戶指定是追加到原有文件末尾仍是覆蓋原有的文件;
    if $(hdfs dfs -test -e test.txt);
    then $(hdfs dfs -appendToFile local.txt test.txt);
    else $(hdfs dfs -copyFromLocal -f local.txt test.txt);
    fi

     

  2. 從HDFS中下載指定文件,若是本地文件與要下載的文件名稱相同,則自動對下載的文件重命名;
    if $(hdfs dfs -test -e file:
    then $(hdfs dfs -copyToLocal hello.txt ./test2.txt);
    else $(hdfs dfs -copyToLocal hello.txt ./test.txt);
    fi

     

  3. 將HDFS中指定文件的內容輸出到終端中;
    hadoop fs -cat /usr/local/hadoop/test.txt

     

  4. 顯示HDFS中指定的文件的讀寫權限、大小、建立時間、路徑等信息;
    hadoop fs -ls -h /usr/local/hadoop/test.txt

     

  5. 給定HDFS中某一個目錄,輸出該目錄下的全部文件的讀寫權限、大小、建立時間、路徑等信息,若是該文件是目錄,則遞歸輸出該目錄下全部文件相關信息;
    hadoop fs -ls -R -h /usr/local/hadoop

     

  6. 提供一個HDFS內的文件的路徑,對該文件進行建立和刪除操做。若是文件所在目錄不存在,則自動建立目錄;
    if $(hadoop fs -test -d /usr/local/hadoop/test);
    then $(hadoop fs -touchz /usr/local/hadoop/test/test1.txt);
    else $(hadoop fs -mkdir -p /usr/local/hadoop/test && hadoop fs -touchz /usr/local/hadoop/test/test1.txt);
    fi

     

  7. 提供一個HDFS的目錄的路徑,對該目錄進行建立和刪除操做。建立目錄時,若是目錄文件所在目錄不存在則自動建立相應目錄;刪除目錄時,由用戶指定當該目錄不爲空時是否還刪除該目錄;
    hadoop fs -rmr /usr/local/hadoop/test

     

  8. 向HDFS中指定的文件追加內容,由用戶指定內容追加到原有文件的開頭或結尾;
    hadoop fs -appendToFile local.txt test.txt

     

  9. 刪除HDFS中指定的文件;
    hadoop fs -rm /usr/local/hadoop/test.txt

     

  10. 刪除HDFS中指定的目錄,由用戶指定目錄中若是存在文件時是否刪除目錄;
    hadoop fs -rmr /usr/local/hadoop

     

  11. 在HDFS中,將文件從源路徑移動到目的路徑。
    hadoop fs -mv /usr/local/hadoop/test.txt /usr/local/hadoop/hadoop_tmp/test.txt
相關文章
相關標籤/搜索