hadoop笔记3

hadoop 文件系统牺牲了一些POSIX要求,即写入的内容不能立即可见。

import static org.junit.Assert.*;
import static org.hamcrest.Matchers.*;

import java.io.IOException;
import java.io.OutputStream;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

public class CoheModel {
	public static void main(String[] args) throws IOException {
		Configuration conf=new Configuration();
		FileSystem fs=FileSystem.get(conf);
		try {
			OutputStream out=fs.create(new Path(args[0]));
			out.write("contend".getBytes("UTF-8"));
			out.flush();
			assertThat(fs.getFileStatus(new Path(args[0])).getLen(), is(0l));
			
		} catch (Exception e) {
			e.printStackTrace();
		}	
	}
}

这里使用到了两个额外的包,junit禾hamcrest,运行hadoop命令之前要把hamcrest包放到hadoop根目录lib目录下,lib本身带有junit4.5的包,所以在编写这个程序之前的eclipse最好也是使用这个版本的junit包。因为assertThat和is都是静态的方法,所以在导入包的时使用了static,运行结果没有出现AssertException,说明这时的reader无法看见正在写入的块。

在out.flush 之后加上out.sync,或者out.close可以实现数据的同步。

做以下尝试:

import static org.junit.Assert.*;
import static org.hamcrest.Matchers.*;

import java.io.IOException;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

public class CoheModel {
	public static void main(String[] args) throws IOException {
		Configuration conf=new Configuration();
		FileSystem fs=FileSystem.get(conf);
		try {
			FSDataOutputStream out=fs.create(new Path(args[0]));
			out.write("contend".getBytes("UTF-8"));
			out.flush();
			out.sync();
			assertThat(fs.getFileStatus(new Path(args[0])).getLen(), is(7l));
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}	
	}
}

出现了断言异常:

 

Exception in thread "main" java.lang.AssertionError:
Expected: is <7L>
     got: <0L>

这是内存和数据节点没有同步的结果,把out.sync()换成out.close()能得到正确的结果,非常奇怪,有明白的朋友欢迎留言给我~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值