zoukankan      html  css  js  c++  java
  • HDFS命令行操作 和 api操作

    HDFS,是Hadoop Distributed File System的简称,是Hadoop抽象文件系统的一种实现。Hadoop抽象文件系统可以与本地系统、Amazon S3等集成,甚至可以通过Web协议(webhsfs)来操作。HDFS的文件分布在集群机器上,同时提供副本进行容错及可靠性保证。例如客户端写入读取文件的直接操作都是分布在集群各个机器上的,没有单点性能压力。

    HDFS相关的搭建可以看我前面的一篇博文,我们今天主要来讲下怎么操作hdfs的api和 hdfs命令行,

    java内操作HDFS需要先配置仓库

    <repositories>
      <repository>
    	<id>cloudera</id>
    	<url>https://repository.cloudera.com/artifactory/cloudera-repos</url>
      </repository>
    </repositories>
    //导包
    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-client</artifactId>
      <version>${hadoop.version}</version>
    </dependency>
    

      

    例子:通过api创建目录

    this.configuration = new Configuration();
    this.fileSystem = FileSystem.get(new URI(this.HDFS_PATH),configuration,"hadoop");
    Path path = new Path("/hdfsapi/test");
    boolean result  = fileSystem.mkdirs(path);
    

      

    通过API读取文件,回写到本地

    Path path = new Path("/gwyy.txt");
    FSDataInputStream fsDataInputStream = fileSystem.open(path);
    FileOutputStream fileOutputStream = new FileOutputStream(new File("a.txt"));
    byte[] buffer = new byte[1024];
    int length = 0;
    StringBuffer sb = new StringBuffer();
    while( (  length = fsDataInputStream.read(buffer)) != -1) {
    	sb.append(new String(buffer,0,buffer.length));
    fileOutputStream.write(buffer,0,buffer.length);
    }
    System.out.println(sb.toString());
    

      

    HDFS 创建文件并且写入内容

    FSDataOutputStream out = fileSystem.create(new Path("/fuck.txt"));
    out.writeUTF("aaabbb");
    out.flush();
    out.close();
    

      

    HDFS 重名

    boolean a = fileSystem.rename(new Path("/fuck.txt"),new Path("/fuck.aaa"));
    System.out.println(a);
    

      

    HDFS拷贝文件

    fileSystem.copyFromLocalFile(new Path("a.txt"),new Path("/copy_a.txt"));
    

      

    HDFS上传大文件

    InputStream in = new BufferedInputStream(new FileInputStream(new File("hive-1.1.0-cdh5.15.1.tar.gz")));
    Path dst = new Path("/hive.tar.gz");
    //显示进度条
    FSDataOutputStream out = fileSystem.create(dst, new Progressable() {
    	@Override
    	public void progress() {
    		System.out.flush();
    		System.out.print('.');
    	}
    });
    byte[] buffer = new byte[4096];
    int length = 0;
    //写入到 hdfs
    while((length = in.read(buffer,0,buffer.length)) != -1) {
    	out.write(buffer,0,buffer.length);
    }
    

      

    HDFS下载文件

    fileSystem.copyToLocalFile(new Path("/fuck.aaa"),new Path("./"));
    

      

    HDFS 列出所有文件

    FileStatus[] fileStatuses = fileSystem.listStatus(new Path("/"));
    for (FileStatus f:fileStatuses) {
    	System.out.println(f.getPath());
    }
    

      

    HDFS 递归列出文件

    RemoteIterator<LocatedFileStatus>  remoteIterator = fileSystem.listFiles(new Path("/"),true);
    while(remoteIterator.hasNext()) {
    	LocatedFileStatus file =  remoteIterator.next();
    	System.out.println(file.getPath());
    }
    

      

    HDFS查看文件区块

    FileStatus fileStatus = fileSystem.getFileStatus(new Path("/jdk-8u221-linux-x64.tar.gz"));
    BlockLocation[] blockLocations = fileSystem.getFileBlockLocations(fileStatus,0,fileStatus.getLen());
    //查看区块
    for (BlockLocation b:blockLocations) {
    	for (String name:b.getNames()) {
    		System.out.println(name + b.getOffset() + b.getLength());
    	}
    }
    

      

    HDFS删除文件

    如果路径是目录并设置为*如果为true,则删除目录,否则引发异常。在*对于文件,递归可以设置为true或false。
    boolean a = fileSystem.delete(new Path("/gwyy.txt"),true);
    System.out.println(a);
    

      

    下面我们介绍下HDFS的命令行操作

    查看 hdfs 文件根目录

    hadoop fs -ls /
    

      

    上传文件到 hdfs的根目录

    hadoop fs -put  gwyy.txt  /
    

      

    从本地拷贝文件到hdfs

    hf -copyFromLocal xhc.txt  /
    

      

    ####从本地移动文件到hdfs 本地文件删除 hf -moveFromLocal a.txt /

    查看文件内容

    hadoop fs -cat /gwyy.txt
    hadoop fs -text  /gwyy.txt
    

      

    从 hdfs里拿文件到本地

    hadoop fs -get /a.txt  ./
    

      

    HDFS创建文件夹

    hadoop fs -mkdir  /hdfs-test
    

      

    从A文件夹移动到B文件夹

    hadoop fs -mv /a.txt  /hdfs-test/a.txt
    

      

    文件复制操作

    hadoop fs -cp /hdfs-test/a.txt /hdfs-test/a.txt.back
    

      

    把多个文件合并到一起 导出来

    hadoop fs -getmerge /hdfs-test ./t.txt
    

      

    删除一个文件

     hf -rm /hdfs-test/a.txt.back
    

      

    删除一个目录

    hadoop fs -rmdir /hdfs-test   只能删除空目录
    hadoop fs -rm -r /hdfs-test  删除目录不管有没有东西都删
    

      

  • 相关阅读:
    【异常记录(11)】 Web应用程序项目 已配置为使用 IIS。无法访问 元数据库。您没有足够的特权访问计算机上的 IIS 网站。Unable to access the IIS metabase. You do not have sufficient privilege to access IIS web sites on your machine.
    用 C# 计算 与 java 一致的unix时间戳 (长时间整形 如:1476956079961) Unix timestamps consistent with Java format,Implemented in C #
    【异常记录(十)】 接口调用,提示跨域 Cross-domain calling interface, Access-Control-Allow-Origin
    【异常记录(九)】 System.Threading.ThreadAbortException: 正在中止线程
    数据结构-kmp算法
    数据结构-线性表 (栈,队列,串)
    dfs序
    树上倍增
    网络流
    树状数组题目合集
  • 原文地址:https://www.cnblogs.com/gwyy/p/12205199.html
Copyright © 2011-2022 走看看