定位读取HDFS文件

mac2022-06-30  30

 

1.文件从hdfs下载

 

下面来回顾回顾,出现的尴尬问题:

这个问题,出现的原因就是从hdfs下载到本地,不能后面直接跟一个目录,必须记得写一个名。

 

 

@Test public void getFileHdfs1() throws URISyntaxException, IOException, InterruptedException { Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(new URI("hdfs://bigdata111:9000"),conf,"root"); FSDataInputStream fis = fs.open(new Path("/123")); FileOutputStream fos = new FileOutputStream(new File("D:\\input\\test\\aa.txt")); IOUtils.copyBytes(fis,fos,1024*5,true); System.out.println("下载成功"); }  

2.文件从本地上传到HDFS

@Test public void putFileHdfs1() throws URISyntaxException, IOException, InterruptedException { Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(new URI("hdfs://bigdata111:9000"),conf,"root"); FileInputStream fis=new FileInputStream(new File("D:\\input\\test\\student.txt")); Path path=new Path("/student.txt"); FSDataOutputStream fos=fs.create(path); IOUtils.copyBytes(fis,fos,4*1024,false); System.out.println("上传成功"); fs.close(); }

 

3.文件的定位读取三次

 

/** * hdfs文件的定位读取第一个block块到windows中 * * */ @Test public void readFileHdfs1() throws URISyntaxException, IOException, InterruptedException { Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(new URI("hdfs://bigdata111:9000"),conf,"root"); //从hdfs创建输入流 FSDataInputStream fis=fs.open(new Path("/Python素材.rar")); //创建输出流 FileOutputStream fos=new FileOutputStream(new File("D:\\input\\test\\ss1.txt")); //对接流 byte[] buff = new byte[1024]; //1kb,然后是循环1024*128=128M for (int i=0;i<128*1024;i++){ fis.read(buff); fos.write(buff); } IOUtils.closeStream(fis); IOUtils.closeStream(fos); } /** * hdfs文件的定位读取第二个block块到windows中 */ @Test public void readFileHdfs2() throws URISyntaxException, IOException, InterruptedException { Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(new URI("hdfs://bigdata111:9000"),conf,"root"); //从hdfs创建输入流 FSDataInputStream fis=fs.open(new Path("/Python素材.rar")); //创建输出流 FileOutputStream fos=new FileOutputStream(new File("D:\\input\\test\\ss2.txt")); //对接流 byte[] buff = new byte[1024]; //定义偏移量 fis.seek(128*1024*1024); //1kb,然后是循环1024*128=128M for (int i=0;i<128*1024;i++){ fis.read(buff); fos.write(buff); } IOUtils.closeStream(fis); IOUtils.closeStream(fos); fis.close(); } /** * hdfs文件的定位读取第三个块到windows中 * * */ @Test public void readFileHdfs3() throws URISyntaxException, IOException, InterruptedException { Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(new URI("hdfs://bigdata111:9000"),conf,"root"); //从hdfs创建输入流 FSDataInputStream fis=fs.open(new Path("/Python素材.rar")); //创建输出流 FileOutputStream fos=new FileOutputStream(new File("D:\\input\\test\\ss3.txt")); //对接流 byte[] buff = new byte[1024]; //定义偏移量 fis.seek(128*1024*1024*2); IOUtils.copyBytes(fis, fos, 1024); IOUtils.closeStream(fis); IOUtils.closeStream(fos); fs.close(); }

 结果:

 在cmd中文件合并:

 

 

最终改成压缩包后,在解压,会变成原来的模样。

 

 

 

 

                                                                                                                                                                     保持学习,保持饥饿

                                                                                                                                                       ————Jackson_MVP 

 

最新回复(0)