HDFS-查看文件属性+文件名称过滤
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | 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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示