使用hadoop命令:hadoop fs -ls /hdfsapi/test  我们能够查看HDFS文件系统/hdfsapi/test目录下的所有文件信息

那么使用代码怎么写呢?直接先上代码:(这之后贴上去的代码怎么就全灰色了?....)

public class HDFSApp {

    public static final String HDFS_PATH = "hdfs://hadoop000:8020";
    FileSystem fileSystem = null;
    Configuration configuration = null;     @Before
    public void setUp() throws Exception{
        System.out.println("setUp-----------");
        configuration = new Configuration();
        configuration.set("dfs.replication","1");         /**
         * 构造一个访问制定HDFS系统的客户端对象
         * 第一个参数:HDFS的URI
         * 第二个参数:客户端制定的配置参数
         * 第三个参数:客户端的身份,说白了就是用户名
         */
        fileSystem = FileSystem.get(new URI(HDFS_PATH),configuration,"hadoop");
    }
  /**
* 查看目标文件夹下的所有文件
* @throws Exception
*/
@Test
public void listFiles() throws Exception{
FileStatus[] statuses = fileSystem.listStatus(new Path("/hdfsapi/test"));
for(FileStatus file : statuses){
String isDir = file.isDirectory() ? "文件夹" : "文件";
String permission = file.getPermission().toString();
short replication = file.getReplication();
long length = file.getLen();
String path = file.getPath().toString();
System.out.println(isDir + "\t" + permission + "\t" + replication + "\t" + length + "\t" + path);
}
}
   @After
    public void tearDown(){
        configuration = null;
        fileSystem = null;
        System.out.println("----------tearDown------");
    }
} 运行测试类:
setUp-----------
log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
文件    rw-r--r--    3    14    hdfs://hadoop000:8020/hdfsapi/test/a.txt
文件    rw-r--r--    1    28    hdfs://hadoop000:8020/hdfsapi/test/c.txt
文件    rw-r--r--    1    181367942    hdfs://hadoop000:8020/hdfsapi/test/jdk.zip
文件    rw-r--r--    1    2732    hdfs://hadoop000:8020/hdfsapi/test/t.txt
文件夹    rwxr-xr-x    0    0    hdfs://hadoop000:8020/hdfsapi/test/testdir
----------tearDown------

首先我们找到fileSystem的listStatus方法,这个方法怎么用?还是那句话:哪里不会Ctrl点哪里。我们点进去能看到方法的源码信息,能够知道该方法的返回值是一个FileStatus[]数组类型,所要传入的参数是目标目录Path:

 /**
* List the statuses of the files/directories in the given path if the path is
* a directory.
* <p>
* Does not guarantee to return the List of files/directories status in a
* sorted order.
*列出给定路径中文件/目录的状态(如果路径为目录。不保证返回排序顺序。
* @param f given path
* @return the statuses of the files/directories in the given patch
* @throws FileNotFoundException when the path does not exist;
* IOException see specific implementation
*/
public abstract FileStatus[] listStatus(Path f) throws FileNotFoundException,
IOException;

既然返回的是一个数组类型,我们自然会想到用循环来遍历,但是FileStatus 这个又是什么呢?Ctrl点进去:

/** Interface that represents the client side information for a file.
*表示文件的客户端信息的接口。
*/
@InterfaceAudience.Public
@InterfaceStability.Stable
public class FileStatus implements Writable, Comparable { private Path path;
private long length;
private boolean isdir;
private short block_replication;
private long blocksize;
private long modification_time;
private long access_time;
private FsPermission permission;
private String owner;
private String group;
private Path symlink;

FileStatus这是一个表示文件的客户端信息的接口,贴上了一些类的成员变量,我们能从中知道这个里面包含了文件的这么多信息接口。自然就能够使用类里的方法进行访问取得文件的相关信息了。

测试成功,但是我们发现一个问题,就是这个方法就如hadoop fs -ls /hdfsapi/test 一样用户只能查看到当前目录下的文件信息,倘若文件夹test下还有文件夹testdir,testdir文件夹里还有文件就无法显示了,所以我们来看看怎么进行递归查看目标文件夹下的所有文件。

首先通过hadoop命令递归查看: hadoop fs -ls -R /hdfsapi/test    (赶紧试试去 recursive 递归)

那么通过代码怎么实现呢?

我们之前使用的是fileSystem下的listStatus方法,那么我们继续查看API有没有能够使用的,我们看到有一个方法是listFiles:

 /**
* List the statuses and block locations of the files in the given path.
* Does not guarantee to return the iterator that traverses statuses
* of the files in a sorted order.
*
* If the path is a directory,
* if recursive is false, returns files in the directory;
* if recursive is true, return files in the subtree rooted at the path.
* If the path is a file, return the file's status and block locations.
*
* @param f is the path
* @param recursive if the subdirectories need to be traversed recursively
*
* @return an iterator that traverses statuses of the files
*
* @throws FileNotFoundException when the path does not exist;
* IOException see specific implementation
*/
public RemoteIterator<LocatedFileStatus> listFiles(
final Path f, final boolean recursive)
throws FileNotFoundException, IOException {}

所以需要我们不仅善于查看API还要善于查找API。

于是递归查看目标文件夹下的所有文件代码这么写:

/**
* 递归查看目标文件夹下的所有文件
* @throws Exception
*/
@Test
public void listFilesRecursive() throws Exception{ RemoteIterator<LocatedFileStatus> files = fileSystem.listFiles(new Path("/hdfsapi/test"),true); while (files.hasNext()){
LocatedFileStatus file = files.next();
String isDir = file.isDirectory() ? "文件夹" : "文件";
String permission = file.getPermission().toString();
short replication = file.getReplication();
long length = file.getLen();
String path = file.getPath().toString();
System.out.println(isDir + "\t" + permission + "\t" + replication + "\t" + length + "\t" + path);
}
} 运行测试类:
setUp-----------
log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
文件 rw-r--r-- 3 14 hdfs://hadoop000:8020/hdfsapi/test/a.txt
文件 rw-r--r-- 1 28 hdfs://hadoop000:8020/hdfsapi/test/c.txt
文件 rw-r--r-- 1 181367942 hdfs://hadoop000:8020/hdfsapi/test/jdk.zip
文件 rw-r--r-- 1 2732 hdfs://hadoop000:8020/hdfsapi/test/t.txt
文件 rw-r--r-- 1 11 hdfs://hadoop000:8020/hdfsapi/test/testdir/h.txt
----------tearDown------

【HDFS API编程】查看目标文件夹下的所有文件、递归查看目标文件夹下的所有文件的更多相关文章

  1. 【HDFS API编程】jUnit封装-改写创建文件夹

    首先:什么是jUnit  回顾: https://www.cnblogs.com/Liuyt-61/p/10374732.html 上一节我们知道: /** * 使用Java API操作HDFS文件系 ...

  2. 【HDFS API编程】查看HDFS文件内容、创建文件并写入内容、更改文件名

    首先,重点重复重复再重复: /** * 使用Java API操作HDFS文件系统 * 关键点: * 1)创建 Configuration * 2)获取 FileSystem * 3)...剩下的就是 ...

  3. 【HDFS API编程】第一个应用程序的开发-创建文件夹

    /** * 使用Java API操作HDFS文件系统 * 关键点: * 1)创建 Configuration * 2)获取 FileSystem * 3)...剩下的就是 HDFS API的操作了*/ ...

  4. 【HDFS API编程】从本地拷贝文件,从本地拷贝大文件,拷贝HDFS文件到本地

    接着之前继续API操作的学习 CopyFromLocalFile: 顾名思义,从本地文件拷贝 /** * 使用Java API操作HDFS文件系统 * 关键点: * 1)create Configur ...

  5. HDFS API编程

    3.1常用类        3.1.1Configuration Hadoop配置文件的管理类,该类的对象封装了客户端或者服务器的配置(配置集群时,所有的xml文件根节点都是configuration ...

  6. 【HDFS API编程】开发环境搭建

    使用HDFS API的方式来操作HDFS文件系统 IDEA Java 使用Maven来管理项目 先打开IDEA,New Project 创建GAV然后next 默认使用的有idea内置的Maven,可 ...

  7. 【HDFS API编程】查看文件块信息

    现在我们把文件都存在HDFS文件系统之上,现在有一个jdk.zip文件存储在上面,我们想知道这个文件在哪些节点之上?切成了几个块?每个块的大小是怎么样?先上测试类代码: /** * 查看文件块信息 * ...

  8. 【HDFS API编程】删除文件

    所有操作都是以fileSystem为入口进行,我们使用fileSystem下的delete方法进行删除文件操作,删除的时候必须慎重. 直接上代码: /** * 删除文件 * @throws Excep ...

  9. 【HDFS API编程】图解客户端写文件到HDFS的流程

随机推荐

  1. 配置https and http2 local本地开发环境

    今天,几乎所有你访问的网站都是受HTTPS保护的.如果你还没有这样做,是时候这样做了.使用HTTPS保护您的服务器也就意味着您无法从非HTTPS的服务器发送请求到此服务器.这对使用本地开发环境的开发人 ...

  2. 闲记 单元格与单元格之间的边 ///字体属性都是font开头,除了颜色属性 ///文本属性都是text开的,除了设置行高。

    这两天一直在做网页没有什么太大的问题,期间也考了一场试,对答案的时候老师讲了一些小知识,因此来记录一下. 单元格与单元格之间的边距(cellspaling) list-type-image可以使用图像 ...

  3. 学习笔记TF064:TensorFlow Kubernetes

    AlphaGo,每个实验1000个节点,每个节点4个GPU,4000 GPU.Siri,每个实验2个节点,8个GPU.AI研究,依赖海量数据计算,离性能计算资源.更大集群运行模型,把周级训练时间缩短到 ...

  4. mongoDB创建windows服务启动解决

    最近想了解一下关于MongoDB的知识,记得之前电脑上安装的MongoDB也能正常启用,可是这次在使用mongodb,却遇到一下小麻烦啊.mongodb无法启动,小编很苦恼,尝试了各种方法,甚至卸载重 ...

  5. 爬取网络图片到C盘存储的PermissionError: [Errno 13] Permission denied

    C盘根目录下不能拷进去文件,但可以新建文件夹的,“发生错误,操作被阻止,客户端没有所需的特权. 因为是系统目录,不要在里面拷贝文件,最好建立一个目录再放在里面. 硬要拷贝的话,可以使用管理员权限打开e ...

  6. Mybatis逆向工程的配置

    源码github下载地址:https://github.com/wcyong/mybatisGeneratorCustom.git 参考文章:https://www.cnblogs.com/whgk/ ...

  7. Int和String互转的方法

    Java 中int.String的类型转换   int -> String int i=12345;String s="";第一种方法:s=i+"";第二 ...

  8. Java工程师可以从事的几大职业

    在重庆,程序员一直被认为是高薪职业,Java作为最受欢迎的语言,是很多初入行的人都会选择的方向.那么学习之后可以从事哪些工作呢?下面小编就给大家介绍一下. 一.大数据技术 Hadoop以及其他大数据处 ...

  9. 关于PCA

    PCA是常见的降维技术. 对于使用PCA来进行降维的数据,需要进行预处理,是指能够实现均值为0,以及方差接近.如何来确定到底哪个维度是"主成分"?就要某个axis的方差. 为什么要 ...

  10. 自己DIY出来一个JSON结构化展示器

    说来也巧,这个玩意,一直都想亲手写一个,因为一直用着各种网上提供的工具,觉得这个还是有些用途,毕竟,后面的实现思路和原理不是太复杂,就是对json的遍历,然后给予不同节点类型以不同的展现风格. 我这次 ...