HDFS-客户端操作(三)通过IO流操作HDFS

Posted on 2020-04-14 14:32  MissRong  阅读(350)  评论(0)    收藏  举报

HDFS-客户端操作(三)通过IO流操作HDFS

一、通过IO流上传文件到HDFS

 1 /**
 2      * 一、IO流方式上传到HDFS
 3      */
 4     @Test
 5     public void putFileToHDFSIO(){
 6         //1.创建配置信息对象 Configuration:配置
 7         Configuration conf = new Configuration();
 8 
 9         //2.找到文件系统
10         FileSystem fs = null;
11         try {
12             fs = FileSystem.get(new URI("hdfs://bigdata111:9000/"), conf, "root");
13         } catch (IOException e) {
14             e.printStackTrace();
15         } catch (InterruptedException e) {
16             e.printStackTrace();
17         } catch (URISyntaxException e) {
18             e.printStackTrace();
19         }
20 
21         //3.创建输入流
22         FileInputStream fis = null;
23         try {
24             fis = new FileInputStream(new File("D:/f1.txt"));
25         } catch (FileNotFoundException e) {
26             e.printStackTrace();
27         }
28 
29         //4.输出路径
30                                                  // 注意: 需要指定一个名字
31         Path writePath = new Path("hdfs://bigdata111:9000/A/123");
32         FSDataOutputStream fos = null;
33         try {
34             fos = fs.create(writePath);
35         } catch (IOException e) {
36             e.printStackTrace();
37         }
38 
39         //5.流对接
40         //InputStream in:输入流
41         //OutputStream out:输出流
42         //int buffSize:缓冲区大小
43         //boolean close:是否关闭
44         try {
45             IOUtils.copyBytes(fis,fos,4*1024,false);
46         } catch (IOException e) {
47             e.printStackTrace();
48         }finally{
49             IOUtils.closeStream(fis);
50             IOUtils.closeStream(fos);
51         }
52     }

二、IO方式读取HDFS文件到控制台

 1     /**
 2      * 二、IO方式读取HDFS到控制台
 3      * @throws Exception
 4      */
 5     @Test
 6     public void getFileFromHDFSIO() throws Exception {
 7         //1.创建配置信息对象
 8         Configuration conf = new Configuration();
 9 
10         //2.连接文件系统
11         FileSystem fs = FileSystem.get(new URI("hdfs://bigdata111:9000/"), conf, "root");
12 
13         //3.读取路径
14         Path readPath = new Path("hdfs://bigdata111:9000/A/123");
15 
16         //4.输入
17         FSDataInputStream fis = fs.open(readPath);
18 
19         //5.输出到控制台
20         //InputStream in:输入流
21         //OutputStream out:输出流
22         //int buffSize:缓冲区大小
23         //boolean close:是否关闭
24         IOUtils.copyBytes(fis,System.out,4*1024,false);
25     }

三、定位文件读取

 1     /**
 2      * 三、定位文件读取(一)
 3      * @throws Exception
 4      */
 5     @Test
 6     public void readFileSeek1() throws Exception {
 7         //1.创建配置文件信息
 8         Configuration conf = new Configuration();
 9 
10         //2.获取文件系统
11         FileSystem fs = FileSystem.get(new URI("hdfs://bigdata111:9000"), conf, "root");
12 
13         //3.输入路径
14         Path path = new Path("hdfs://bigdata111:9000/A/hadoop-2.7.2.rar");
15         FSDataInputStream fis = fs.open(path);
16 
17         //4.输出
18         FileOutputStream fos = new FileOutputStream("D:/HDFS/A1");
19 
20         //5.流对接
21         byte[] buf= new byte[1024];
22         for(int i=0;i<128*1024;i++){
23             fis.read(buf);
24             fos.write(buf);
25         }
26         //6.关闭流
27         IOUtils.closeStream(fos);
28         IOUtils.closeStream(fis);
29     }
30 /** 31 * 三、定位文件读取(二) 32 * @throws Exception 33 */ 34 @Test 35 public void readFileSeek2() throws Exception { 36 //1.创建配置文件信息 37 Configuration conf = new Configuration(); 38 39 //2.获取文件系统 40 FileSystem fs = FileSystem.get(new URI("hdfs://bigdata111:9000"), conf, "root"); 41 42 //3.输入路径、输出流 43 Path path = new Path("hdfs://bigdata111:9000/A/hadoop-2.7.2.rar"); 44 FSDataInputStream fis = fs.open(path); 45 46 //4.输出 47 FileOutputStream fos = new FileOutputStream("D:/HDFS/A2"); 48 49 //5.定位偏移量/offse/游标/读取进展(目的:找到第一块的尾巴,第二块的开头) 50 fis.seek(128*1024*1024); 51 52 //6.流对接 53 IOUtils.copyBytes(fis,fos,1024); 54 55 //7.关闭流 56 IOUtils.closeStream(fos); 57 IOUtils.closeStream(fis); 58 }

合并文件

在window命令窗口-cmd中执行

type A2 >> A1  然后更改后缀为rar即可

查看A1的大小变为204MB-源文件的总大小

博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3