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. Carrot2 in action 初步印象

    RawCluster:聚类中的类别单位 RawCluster.getDocuments():获得该类的文档列表 RawDocument:每个类的文档单位 STC:后缀树表示法 2008-11-13 C ...

  2. 洛谷1006==codevs1169

    P1006 传纸条 题目描述 小渊和小轩是好朋友也是同班同学,他们在一起总有谈不完的话题.一次素质拓展活动中,班上同学安排做成一个m行n列的矩阵,而小渊和小轩被安排在矩阵对角线的两端,因此,他们就无法 ...

  3. 【BZOJ1880】[Sdoi2009]Elaxia的路线 最短路+DP

    [BZOJ1880][Sdoi2009]Elaxia的路线 Description 最近,Elaxia和w**的关系特别好,他们很想整天在一起,但是大学的学习太紧张了,他们 必须合理地安排两个人在一起 ...

  4. IIS无法连接LocalDb,怎么办?

    最近安装了vs 2013,电脑配置不太好,所以没有安装数据库,直接使用vs2013自带的localdb工具,直接运行访问本地mdf数据库文件.但是部署到IIS就出问题了.问题就像下面的图片一样. 最后 ...

  5. iOS 多线程之 NSThread的基本使用

    一个NSThread对象就代表一条线程 下面是NSThread开启线程的方法 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEv ...

  6. js验证表单大全1

    附加:js验证radio是否选择 <script language="javascript"> function checkform(obj) { for(i=0;i& ...

  7. jQuery中删除节点方法remove()、detach()、empty()分析

    jQuery中提供了三种删除节点的方法:remove().detach().empty(),本文详细分析这三种方法. 最容易区分的是empty(),该方法严格上属于“清空节点”,即删除其子节点,自身并 ...

  8. 在一台服务器上搭建多个网站的方法(Apache版)

    Apache的配置文件一般放置在/etc/httpd/conf文件夹下,httpd.conf是它的主配置文件,在进行配置时可以将虚拟主机的配置文件单独配置,如取名为vhost.conf,然后再http ...

  9. 字符串之strchr

    功能:查找字符在字符串中第一次出现的位置. #include <iostream> #include <assert.h> using namespace std; char ...

  10. 3.1 使用STC89C52控制MC20拨打电话

    需要准备的硬件 MC20开发板 1个 https://item.taobao.com/item.htm?id=562661881042 GSM/GPRS天线 1根 https://item.taoba ...