一、背景

hdfs在写入的过程中,只有完全写入整个文件的内容之后,对其他client才会可见,否则在写入的过程中是不可见。如果想要立即可见,就需要一致性flush 的操作。

二、一致性flush

代码例子:

@Test
	public void writeFile() throws Exception{
		// 1 创建配置信息对象
		Configuration configuration = new Configuration();
		fs = FileSystem.get(configuration);
		
		// 2 创建文件输出流
		Path path = new Path("F:\\date\\H.txt");
		FSDataOutputStream fos = fs.create(path);
		
		// 3 写数据
		fos.write("hello Andy".getBytes());
        // 4 一致性刷新
		fos.hflush();
		
		fos.close();
	}

方法很简单,就是调用输出流的 hflush() 方法,刷写client的缓冲区,让其他client立即可以看到写入的内容。一般在并发读写的情况下,要求多个client的数据都是一致,就会要求这点。