zoukankan      html  css  js  c++  java
  • 熟悉常用的HDFS操作

    编程实现以下指定功能,并利用Hadoop提供的Shell命令完成相同任务:
    在本地Linux文件系统的“/home/hadoop/”目录下创建一个文件txt,里面可以随意输入一些单词.

    在本地查看文件位置(ls)

    在本地显示文件内容

       cd /usr/local/hadoop
        touch hello.txt
       
        cat hello.txt


    使用命令把本地文件系统中的“txt”上传到HDFS中的当前用户目录的input目录下。

      ./sbin/start-dfs.sh
        ./bin/hdfs dfs -mkdir -p /user/hadoop
        ./bin/hdfs dfs -mkdir input
        ./bin/hdfs dfs -put ./hello.txt input


    查看hdfs中的文件(-ls)

    ./bin/hdfs dfs -ls /input


    显示hdfs中该的文件内容

    ./bin/hdfs dfs -cat input/hello.txt


    删除本地的txt文件并查看目录

    ./bin/hdfs dfs -rm -ls input/hello.txt


    从hdfs中将txt下载地本地原来的位置。

    ./bin/hdfs dfs -get input/test.txt ~/hello.txt


    从hdfs中删除txt并查看目录

     ./bin/hdfs dfs -rm -ls input/hello.txt
    • 向HDFS中上传任意文本文件,如果指定的文件在HDFS中已经存在,由用户指定是追加到原有文件末尾还是覆盖原有的文件;
    • if $(hdfs dfs -test -e hello.txt);
      then $(hdfs dfs -appendToFile local.txt hello.txt);
      else $(hdfs dfs -copyFromLocal -f local.txt hello.txt);
      fi
    • 从HDFS中下载指定文件,如果本地文件与要下载的文件名称相同,则自动对下载的文件重命名;
    • if $(hdfs dfs -test -e file:
      then $(hdfs dfs -copyToLocal hello.txt ./hello2.txt); 
      else $(hdfs dfs -copyToLocal hello.txt ./hello.txt); 
      fi
    • 将HDFS中指定文件的内容输出到终端中;
    • hdfs dfs -cat hello.txt
    • 显示HDFS中指定的文件的读写权限、大小、创建时间、路径等信息;
    • hdfs dfs -ls -h hello.txt
    • 给定HDFS中某一个目录,输出该目录下的所有文件的读写权限、大小、创建时间、路径等信息,如果该文件是目录,则递归输出该目录下所有文件相关信息;
    • hdfs dfs -ls -R -h /user/hadoop
    • 提供一个HDFS内的文件的路径,对该文件进行创建和删除操作。如果文件所在目录不存在,则自动创建目录;
    • if $(hdfs dfs -hello -d dir1/dir2);
      then $(hdfs dfs -touchz dir1/dir2/filename); 
      else $(hdfs dfs -mkdir -p dir1/dir2 && hdfs dfs -touchz dir1/dir2/filename); 
      fi
    • 提供一个HDFS的目录的路径,对该目录进行创建和删除操作。创建目录时,如果目录文件所在目录不存在则自动创建相应目录;删除目录时,由用户指定当该目录不为空时是否还删除该目录;
    • if $(hdfs dfs -hello -d dir1/dir2);
      then $(hdfs dfs -touchz dir1/dir2/filename); 
      else $(hdfs dfs -mkdir -p dir1/dir2); 
      fi
      if$(hdfs dfs -rmdir dir1/dir2);
      then $(hdfs dfs -rmdir dir1/dir2)
      fi
    • 向HDFS中指定的文件追加内容,由用户指定内容追加到原有文件的开头或结尾;
    • 追加到文件末尾:hdfs dfs -appendToFile local.txt hello.txt
      追加到文件开头:
      (由于没有直接的命令可以操作,方法之一是先移动到本地进行操作,再进行上传覆盖):
      hdfs dfs -get hello.txt
      cat text.txt >> local.txt
      hdfs dfs -copyFromLocal -f hello.txt hello.txt
    • 删除HDFS中指定的文件;
    • hdfs dfs -rm hello.txt
    • 删除HDFS中指定的目录,由用户指定目录中如果存在文件时是否删除目录;
    • 删除目录(如果目录非空则会提示not empty,不执行删除):hdfs dfs -rmdir dir1/dir2
      强制删除目录:hdfs dfs -rm -R dir1/dir2
    • 在HDFS中,将文件从源路径移动到目的路径。
    • hdfs dfs -mv hello.txt hello2.txt
  • 相关阅读:
    VOC2012数据集
    flask 图像接口测试
    PyQt5 学习指南
    超分辨中的注意力机制:Attention in Super Resolution
    OpenCV:图像增亮与直方图均衡
    SOA 架构与微服务架构的区别
    Docker 部署 vue 项目
    element ui 中的 resetFields() 报错'resetFields' of undefined
    Mybatis分页方法
    Serializable的作用
  • 原文地址:https://www.cnblogs.com/wxf2/p/8940426.html
Copyright © 2011-2022 走看看