HDFS-LocalFileSystem

LocalFileSystem

      Hadoop LocalFileSystem是客户端校验的类。在使用LocalFileSystem写文件时,会透明的创建一个.filename.crc的文件。校验文件大小的字节数由io.bytes.per.checksum属性设置,默认是512bytes,即每512字节就生成一个CRC-32校验和。

      .filename.crc文件会存 io.bytes.per.checksum的信息。在读取的时候,会根据此文件进行校验。

      事实上LocalFileSystem是通过继承ChecksumFileSystem实现校验的工作。

写例子:

package com.bigdata.io;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.LocalFileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RawLocalFileSystem;


public class WriteToLocal {
	
	public static void main(String[] args) throws IOException, URISyntaxException {
		Configuration conf = new Configuration();
		LocalFileSystem fs =  new LocalFileSystem(new RawLocalFileSystem());
		fs.initialize(new URI("file:///home/peter/abc1"), conf); // put the conf object to filesystem instance
		OutputStream out = fs.create(new Path("file:///home/peter/abc1"));
		for(int i = 0; i < 512*10;i++){
			out.write(97);
		}
		out.close();
		Path file = fs.getChecksumFile(new Path("file:///home/peter/abc1"));
		System.out.println(file.getName());
		fs.close();
	}

}

相关推荐