HDFS-查看文件属性+文件名称过滤
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-查看文件属性+文件名称过滤的更多相关文章
- HDFS的Java客户端操作代码(查看HDFS下的文件是否存在)
1.查看HDFS目录下得文件是否存在 package Hdfs; import java.io.IOException; import java.net.URI; import org.apache. ...
- hadoop学习;大数据集在HDFS中存为单个文件;安装linux下eclipse出错解决;查看.class文件插件
sudo apt-get install eclipse 安装后打开eclipse,提示出错 An error has occurred. See the log file /home/pengeor ...
- 【HDFS API编程】查看目标文件夹下的所有文件、递归查看目标文件夹下的所有文件
使用hadoop命令:hadoop fs -ls /hdfsapi/test 我们能够查看HDFS文件系统/hdfsapi/test目录下的所有文件信息 那么使用代码怎么写呢?直接先上代码:(这之后 ...
- HDFS设计思路,HDFS使用,查看集群状态,HDFS,HDFS上传文件,HDFS下载文件,yarn web管理界面信息查看,运行一个mapreduce程序,mapreduce的demo
26 集群使用初步 HDFS的设计思路 l 设计思想 分而治之:将大文件.大批量文件,分布式存放在大量服务器上,以便于采取分而治之的方式对海量数据进行运算分析: l 在大数据系统中作用: 为各类分布式 ...
- linux stat 命令 显示文件和文件系统状态(查看文件属性) 查看文件inode
stat 显示文件和文件系统状态(查看文件属性) 查看文件inode详细信息 [root@MongoDB ~]# stat /etc/hosts File: ‘/etc/hosts’ Size: B ...
- hadoop学习笔记(十):hdfs在命令行的基本操作命令(包括文件的上传和下载和hdfs中的文件的查看等)
hdfs命令行 ()查看帮助 hdfs dfs -help ()查看当前目录信息 hdfs dfs -ls / ()上传文件 hdfs dfs -put /本地路径 /hdfs路径 ()剪切文件 hd ...
- 牛客网Java刷题知识点之File对象常用功能:获取文件名称、获取文件路径、获取文件大小、获取文件修改时间、创建与删除、判断、重命名、查看系统根目录、容量获取、获取某个目录下内容、过滤器
不多说,直接上干货! 获取文件名称.获取文件路径.获取文件大小.获取文件修改时间 FileMethodDemo.java package zhouls.bigdata.DataFeatureSelec ...
- Hadoop HDFS (3) JAVA訪问HDFS之二 文件分布式读写策略
先把上节未完毕的部分补全,再剖析一下HDFS读写文件的内部原理 列举文件 FileSystem(org.apache.hadoop.fs.FileSystem)的listStatus()方法能够列出一 ...
- Hadoop之HDFS原理及文件上传下载源码分析(上)
HDFS原理 首先说明下,hadoop的各种搭建方式不再介绍,相信各位玩hadoop的同学随便都能搭出来. 楼主的环境: 操作系统:Ubuntu 15.10 hadoop版本:2.7.3 HA:否(随 ...
随机推荐
- 【BZOJ2282】[Sdoi2011]消防 树形DP+双指针法+单调队列
[BZOJ2282][Sdoi2011]消防 Description 某个国家有n个城市,这n个城市中任意两个都连通且有唯一一条路径,每条连通两个城市的道路的长度为zi(zi<=1000). 这 ...
- 使用 mock 测试
参考文章:https://semaphoreci.com/community/tutorials/getting-started-with-mocking-in-python What are the ...
- 高并发服务器建议调小 TCP 协议的 time_wait 超时时间
说明:操作系统默认 240 秒后,才会关闭处于 time_wait 状态的连接,在高并发访问下,服 务器端会因为处于 time_wait 的连接数太多,可能无法建立新的连接,所以需要在服务器上 调小此 ...
- ThreadLocal(四) : FastThreadLocal原理
一.ThreadLocal的原理以及存在的问题 a. 每个线程内部维护了一个ThreadLocal.ThreadLocalMap类型的变量 b. ThreadLocalMap 的 key 为 Thre ...
- KindEditor 4.1.11最新版 网站实例 包含使用方法 下载地址
KindEditor是非常好的富媒体文本编辑系统,短小精悍,可惜网上找到的都是有各种问题!一怒之下,我自己进行了修改和配置,搞成了一个网站示例.下载解压后,可以直接运行!!完全无问题.! Kinded ...
- Oracle11g用户频繁锁定并且解锁后不允许登录
原因有可能是oracle的密码过期机制导致的:一.由于Oracle中默认在default概要文件中设置了“PASSWORD_LIFE_TIME=180天”所导致.解决办法:1.查看用户用的哪种prof ...
- java.lang.IllegalStateException: availableProcessors is already set to [4], rejecting [4]
Links: 1.Getting availableProcessors is already set to [1], rejecting [1] IllegalStateException exce ...
- 小数据池、is 和 ==的区别
小数据池,在一定情况下出现内存共享(只有int 和 str 才有的) is 和 ==的区别 id() 打印数据的地址 a = 'hello' b = 'hello' print(a = ...
- 面向服务体系架构(SOA)和数据仓库(DW)的思考
摘要: 当前业界对面向服务体系架构(SOA)和数据仓库(Data Warehouse,DW)都介绍的很多,提出了很多优秀的解决方案,但是一般是把 SOA 和 DW 单独考虑,SOA 和 DW 有着共同 ...
- SQL Server 存储过程 分页查询
Transact-SQL中的存储过程,非常类似于Java语言中的方法,它可以重复调用.当存储过程执行一次后,可以将语句缓存中,这样下次执行的时候直接使用缓存中的语句.这样就可以提高存储过程的性能. Ø ...