package com.zhen.hdfs;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path; /**
* @author FengZhen
* @date 2018年8月12日
*
*/
public class FileSystemStatusAPI { /**
* 文件元数据:FileStatus
* 任何文件系统的一个重要特征都是提供其目录结构浏览和检索它所在文件和目录相关信息的功能。
* FileStatus类封装了文件系统中文件和目录的元数据,包括文件长度、块大小、复本、修改时间、所有者以及权限信息
*/ private static FileSystem fileSystem; public static void main(String[] args) {
//setUp();
//fileStatusForFile();
//tearDown(); //globbing();
pathFilter();
} public static void setUp() {
String uri = "/user/hdfs/MapReduce/data/test_status";
Configuration configuration = new Configuration();
try {
fileSystem = FileSystem.get(new URI(uri), configuration);
OutputStream outputStream = fileSystem.create(new Path(uri));
outputStream.write("content".getBytes("UTF-8"));
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
} public static void tearDown() {
if (fileSystem != null) {
try {
fileSystem.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* path=/user/hdfs/MapReduce/data/test_status
isDir=false
length=7
modificationTime=1534080334126
replication=3
blockSize=134217728
owner=FengZhen
group=hdfs
permissions=rw-r--r--
*/
public static void fileStatusForFile() {
Path file = new Path("/user/hdfs/MapReduce/data/test_status");
try {
FileStatus fileStatus = fileSystem.getFileStatus(file);
String path = fileStatus.getPath().toUri().getPath();
System.out.println("path="+path);
Boolean isDir = fileStatus.isDirectory();
System.out.println("isDir="+isDir);
long length = fileStatus.getLen();
System.out.println("length="+length);
long modificationTime = fileStatus.getModificationTime();
System.out.println("modificationTime="+modificationTime);
int replication = fileStatus.getReplication();
System.out.println("replication="+replication);
long blockSize = fileStatus.getBlockSize();
System.out.println("blockSize="+blockSize);
String owner = fileStatus.getOwner();
System.out.println("owner="+owner);
String group = fileStatus.getGroup();
System.out.println("group="+group);
String permissions = fileStatus.getPermission().toString();
System.out.println("permissions="+permissions);
} catch (IOException e) {
e.printStackTrace();
} } /**
* 文件模式
* 在单个操作中处理一批文件是一个很常见的需求。
* 在一个表达式中使用通配符来匹配多个文件是比较方便的,无需列举每个文件和目录来指定输入,该操作称为"通配"(globbing)。
* Hadoop为执行通配提供了两个FileSystem方法
* public FileStatus[] globStatus(Path pathPattern) throws IOException {
return new Globber(this, pathPattern, DEFAULT_FILTER).glob();
}
public FileStatus[] globStatus(Path pathPattern, PathFilter filter) throws IOException {
return new Globber(this, pathPattern, filter).glob();
}
globStatus方法返回与其路径匹配于指定模式的所有文件的FileStatus对象数组,并按路径排序。
PathFilter命令作为可选项可以进一步对匹配结果进行限制
*/
public static void globbing() {
String uri = "/user/hdfs/MapReduce/data";
Configuration configuration = new Configuration();
try {
fileSystem = FileSystem.get(new URI(uri), configuration);
// /2018/08/12 /2017/08/11
FileStatus[] fileStatus = fileSystem.globStatus(new Path("/user/hdfs/MapReduce/data/*/*/{11,12}"));
// 1./user/hdfs/MapReduce/data/201[78](201[7-8] 、 201[^01234569]) hdfs://fz/user/hdfs/MapReduce/data/2017 hdfs://fz/user/hdfs/MapReduce/data/2018
// 2./user/hdfs/MapReduce/data/*/*/11 hdfs://fz/user/hdfs/MapReduce/data/2017/08/11
// 3./user/hdfs/MapReduce/data/*/*/{11,12} hdfs://fz/user/hdfs/MapReduce/data/2017/08/11 hdfs://fz/user/hdfs/MapReduce/data/2018/08/12
for (FileStatus fileStatus2 : fileStatus) {
System.out.println(fileStatus2.getPath().toString());
}
fileSystem.close();
} catch (Exception e) {
e.printStackTrace();
}
} /**
* PathFilter
* 通配符模式并不总能够精确地描述我们想要访问的字符集。比如,使用通配格式排除一个特定文件就不太可能。
* FileSystem中的listStatus和globStatus方法提供了可选的pathFilter对象,以编程方式控制通配符
*/
public static void pathFilter() {
String uri = "/user/hdfs/MapReduce/data";
Configuration configuration = new Configuration();
try {
fileSystem = FileSystem.get(new URI(uri), configuration);
// /2018/08/12 /2017/08/11 新增一个/2017/08/12
FileStatus[] fileStatus = fileSystem.globStatus(new Path("/user/hdfs/MapReduce/data/201[78]/*/*"), new RegexExcludePathFilter("^.*/2017/08/11$"));
//FileStatus[] fileStatus = fileSystem.globStatus(new Path("/user/hdfs/MapReduce/data/2017/*/*"), new RegexExcludePathFilter("/user/hdfs/MapReduce/data/2017/08/11"));
for (FileStatus fileStatus2 : fileStatus) {
System.out.println(fileStatus2.getPath().toString());
}
fileSystem.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

  

HDFS-查看文件属性+文件名称过滤的更多相关文章

  1. HDFS的Java客户端操作代码(查看HDFS下的文件是否存在)

    1.查看HDFS目录下得文件是否存在 package Hdfs; import java.io.IOException; import java.net.URI; import org.apache. ...

  2. hadoop学习;大数据集在HDFS中存为单个文件;安装linux下eclipse出错解决;查看.class文件插件

    sudo apt-get install eclipse 安装后打开eclipse,提示出错 An error has occurred. See the log file /home/pengeor ...

  3. 【HDFS API编程】查看目标文件夹下的所有文件、递归查看目标文件夹下的所有文件

    使用hadoop命令:hadoop fs -ls /hdfsapi/test  我们能够查看HDFS文件系统/hdfsapi/test目录下的所有文件信息 那么使用代码怎么写呢?直接先上代码:(这之后 ...

  4. HDFS设计思路,HDFS使用,查看集群状态,HDFS,HDFS上传文件,HDFS下载文件,yarn web管理界面信息查看,运行一个mapreduce程序,mapreduce的demo

    26 集群使用初步 HDFS的设计思路 l 设计思想 分而治之:将大文件.大批量文件,分布式存放在大量服务器上,以便于采取分而治之的方式对海量数据进行运算分析: l 在大数据系统中作用: 为各类分布式 ...

  5. linux stat 命令 显示文件和文件系统状态(查看文件属性) 查看文件inode

    stat 显示文件和文件系统状态(查看文件属性)  查看文件inode详细信息 [root@MongoDB ~]# stat /etc/hosts File: ‘/etc/hosts’ Size: B ...

  6. hadoop学习笔记(十):hdfs在命令行的基本操作命令(包括文件的上传和下载和hdfs中的文件的查看等)

    hdfs命令行 ()查看帮助 hdfs dfs -help ()查看当前目录信息 hdfs dfs -ls / ()上传文件 hdfs dfs -put /本地路径 /hdfs路径 ()剪切文件 hd ...

  7. 牛客网Java刷题知识点之File对象常用功能:获取文件名称、获取文件路径、获取文件大小、获取文件修改时间、创建与删除、判断、重命名、查看系统根目录、容量获取、获取某个目录下内容、过滤器

    不多说,直接上干货! 获取文件名称.获取文件路径.获取文件大小.获取文件修改时间 FileMethodDemo.java package zhouls.bigdata.DataFeatureSelec ...

  8. Hadoop HDFS (3) JAVA訪问HDFS之二 文件分布式读写策略

    先把上节未完毕的部分补全,再剖析一下HDFS读写文件的内部原理 列举文件 FileSystem(org.apache.hadoop.fs.FileSystem)的listStatus()方法能够列出一 ...

  9. Hadoop之HDFS原理及文件上传下载源码分析(上)

    HDFS原理 首先说明下,hadoop的各种搭建方式不再介绍,相信各位玩hadoop的同学随便都能搭出来. 楼主的环境: 操作系统:Ubuntu 15.10 hadoop版本:2.7.3 HA:否(随 ...

随机推荐

  1. 标准模板库--STL

    标准模板库STL 1.泛型程序设计 C++ 语言的核心优势之一就是便于软件的重用 C++中有两个方面体现重用: 1.面向对象的思想:继承和多态,标准类库 2.泛型程序设计(generic progra ...

  2. mysql导出csv文件

    SELECT * FROM (select 'id','url','大分类','分类','贴吧名称','关注用户数','帖子数量','简介','帖子名称','楼主ID','发表时间','采集时间',' ...

  3. shell脚本中格式化日期

    date [-u] [-d datestr] [-s datestr] [--utc] [--universal] [--date=datestr] [--set=datestr] [--help] ...

  4. Creating a Message Queue in PHP Without External Libraries

    w http://www.ebrueggeman.com/blog/creating-a-message-queue-in-php

  5. ASP-DateAdd时间增加

    ASP内置函数DateAdd(interval, number, date) 1.interval标识你要加的日期部分,具体参数如下:yyyy → 年m → 月d → 日h → 小时n → 分钟s → ...

  6. Java基础知识大全

    本文引用于: http://uule.iteye.com/blog/762949 /; DecimalFormat df = new DecimalFormat("0.00");/ ...

  7. Java的OO与多态

    this的用法 class Banana { void f(int i) {} }Banana a = new Banana(), b = new Banana();a.f(1);b.f(2);若只有 ...

  8. Python 中星号作用:解包&打散

    python中’*’和’**’的使用分两个方面,一个是计算,另一个是参数传递过程中元素的打包和解包.  计算方面 ‘*’和’**’在python中最常见的作用分别是‘相乘’和‘乘幂’,如下: > ...

  9. 斯坦福大学Andrew Ng - 机器学习笔记(6) -- 聚类 & 降维

    大概用了一个月,Andrew Ng老师的机器学习视频断断续续看完了,以下是个人学习笔记,入门级别,权当总结.笔记难免有遗漏和误解,欢迎讨论. 鸣谢:中国海洋大学黄海广博士提供课程视频和个人笔记,在此深 ...

  10. 蒙特卡罗树搜索(MCTS)【转】

    简介 最近AlphaGo Zero又火了一把,paper和各种分析文章都有了,有人看到了说不就是普通的Reinforcement learning吗,有人还没理解估值网络.快速下子网络的作用就放弃了. ...