第二十八讲.seek操作+测试一致模型(描述文件读和写的数据可见性)

 视频:【美妙人生】Hadoop课程系列之HDFS--手把手教你精通HDFS

【美妙人生】Hadoop课程系列之HDFS--手把手教你精通HDFS

【视频笔记】


seek操作【P59】
------------------------------------------------------------------------------------

    /**
     * 通过FileSystem API做read操作,设置seek()
     * 总结:FSDataInputStream输入流可执行Seekable,而FSDataOutputStream没有
     */
    @Test
    public void seekByAPI() throws IOException{
        Configuration conf  =new Configuration();
        FileSystem fs = FileSystem.get(conf);
        Path file = new Path("/spaceQuota/hello3333.txt");
        FSDataInputStream in = fs.open(file);
        IOUtils.copyBytes(in, System.out, 4096,false);//注意:stream不能关闭
        in.seek(0);
        IOUtils.copyBytes(in, System.out, 4096,true);
    }


测试一致模型【P73-3.6.3小节】:描述文件读/写的数据可见性
---------------------------------------------------------------------------------------
  

  /**
     * 通过读写,测试文件系统的一致模型
     */
    @Test
    public void  writeByAPIText() throws IOException{
        Configuration conf = new Configuration();
        conf.set("dfs.bytes-per-checksum", "10");
        FileSystem fs  =FileSystem.get(conf);
        Path file = new Path("/spaceQuota/hello.txt");
        //创建文件时,文件立即可见
        FSDataOutputStream out = fs.create(file,true,4096,(short)2,10);
        out.write("hello world1a".getBytes());
        out.flush();
        out.write("hello world2a".getBytes());
        out.hflush();
        out.write("hello world3a".getBytes());
        out.hsync();
        out.close();
    }
    
    
    @Test
    public void  readByAPIText() throws IOException{
        Configuration conf = new Configuration();
        FileSystem fs  =FileSystem.get(conf);
        Path file = new Path("/spaceQuota/hello.txt");
        FSDataInputStream in = fs.open(file);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] b = new byte[1024];
        int bytesRead;
        while ((bytesRead = in.read(b))!=-1) {
             out.write(b, 0, bytesRead);
        }
        System.out.println(new String(out.toByteArray()));
//        IOUtils.copyBytes(in, System.out, 4096,true);
    }


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值