HDFS-文件读取API
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 | package com.zhen.hdfs; import java.io.IOException; import java.io.InputStream; import java.net.URI; import java.net.URISyntaxException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FSDataInputStream; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IOUtils; /** * @author FengZhen * @date 2018年8月12日 * */ public class FileSystemReadAPI { /** * FileSystem实例有几个静态工厂方法 * public static FileSystem get(Configuration conf) throws IOException * return get(getDefaultUri(conf), conf); * 返回的是默认文件系统(在 conf/core-site.xml中指定的,如果没有指定,则使用默认的本地文件系统) * public static FileSystem get(URI uri, Configuration conf) throws IOException * 通过给定的URI方案和权限来确定要使用的文件系统,如果给定URI中没有指定方案,则返回默认文件系统 * public static FileSystem get(final URI uri, final Configuration conf, final String user) throws IOException, InterruptedException * 作为给定用户来访问文件系统,对安全来说是至关重要的 * * 在某些情况下,可能希望获取本地文件系统的运行实例,此时可以使用getLocal()方法 */ public static void main(String[] args) { String uri = "hdfs://fz/user/hdfs/MapReduce/data/test.txt" ; String uri1 = "hdfs://fz/user/hdfs/MapReduce/data/test1.txt" ; String user = "hadoop" ; //getByUri(uri); //getByUriAndUser(uri, user); seekDoubleCat(uri1); } public static void getByUri(String uri) { Configuration conf = new Configuration(); InputStream inputStream = null ; try { FileSystem fileSystem = FileSystem. get ( new URI(uri), conf); inputStream = fileSystem.open( new Path(uri)); IOUtils.copyBytes(inputStream, System. out , 4096, false ); } catch (IOException e) { e.printStackTrace(); } catch (URISyntaxException e) { e.printStackTrace(); } finally { IOUtils.closeStream(inputStream); } } public static void getByUriAndUser(String uri, String user) { Configuration conf = new Configuration(); InputStream inputStream = null ; try { FileSystem fileSystem = FileSystem. get ( new URI(uri), conf, user); inputStream = fileSystem.open( new Path(uri)); IOUtils.copyBytes(inputStream, System. out , 4096, false ); } catch (IOException e) { e.printStackTrace(); } catch (URISyntaxException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } finally { IOUtils.closeStream(inputStream); } } /** * 实际上,FileSystem对象中的open方法返回的是FSDataInputStream对象,而不是标准的java.io类对象。这个类是继承了java.io.DataInputStream接口的一个特殊类,并支持随机访问,由此可以从流的任意位置读取数据 * public class FSDataInputStream extends DataInputStream implements Seekable, PositionedReadable, ByteBufferReadable, HasFileDescriptor, CanSetDropBehind, CanSetReadahead, HasEnhancedByteBufferAccess {} */ /** * Seekable接口支持在文件中找到指定位置,并提供一个查询当前位置相对于文件其实位置偏移量(getPos())的查询方法 * public interface Seekable { void seek(long pos) throws IOException; long getPos() throws IOException; @InterfaceAudience.Private boolean seekToNewSource(long targetPos) throws IOException; } 调用seek方法来定位大于文件长度的位置会引发IOException异常。与java.io.InputStream的skip不同,seek可以移动到文件中任意一个绝对位置,skip则只能相对于当前位置定位到另一个新位置。、 注意,seek方法是一个相对高开销的操作,需要慎重使用 */ public static void seekDoubleCat(String uri) { Configuration conf = new Configuration(); FSDataInputStream inputStream = null ; try { FileSystem fileSystem = FileSystem. get ( new URI(uri), conf); inputStream = fileSystem.open( new Path(uri)); IOUtils.copyBytes(inputStream, System. out , 4096, false ); System. out .println( "-------------------end-----------------" ); inputStream.seek(1); //索引 IOUtils.copyBytes(inputStream, System. out , 4096, false ); } catch (IOException e) { e.printStackTrace(); } catch (URISyntaxException e) { e.printStackTrace(); } finally { IOUtils.closeStream(inputStream); } } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示