首页 > 代码库 > MR之SequenceFile详解

MR之SequenceFile详解

package com.leaf.hadoop.second;

import java.util.Random;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.Text;
//$ hadoop jar SequenceWriteSample.jar SequenceWriteSample
public class SequenceWriteSample {

	private static String meg = "hello world";
	public static void main(String[] args) throws Exception{
		Configuration conf = new Configuration();//获取环境变量
		FileSystem fs = FileSystem.get(conf);//获取文件系统
		Path path = new Path("sequenceFile");//定义路径
		Random rand = new Random();
		SequenceFile.Writer write = new SequenceFile.Writer(fs, conf, path, IntWritable.class, Text.class);
		for(int i=0;i<100;i++){
			write.append(new IntWritable(rand.nextInt(100)), new Text(meg));//写操作,隐藏的为每一行添加一个偏移量
			//System.out.println(write.getLength());
		}
		IOUtils.closeStream(write);//将写出流关闭
	}
	
	/*
	 * createWriter()方法的源码;
	 * public static Writer createWriter(FileSystem fs,Configuration conf,Path name,Class keyClass,Class valClass)throws IOException{
		return createWriter(fs,conf,name,keyClass,valClass,getCompressionType(conf));
	}
	*CompressionType类用以对SequenceFile写入的文件设定是否进行压缩处理
	*
	*CompressionType.NONE:表示不对任何数据进行压缩从而直接进行存储
	*CompressionType.RECORD:表示仅仅压缩key而对value不进行压缩
	*CompressionType.BLOCK:表示对所有的key与value都进行压缩
	*/
	
	/**
	 * write实例还隐藏的为每一行添加一个偏移量,用于获取当前文件的移动位置,
	 * 并按一定顺序生成相应的"同步位置"(Sync position),同步位置是
	 * 生成在SequenceFile文件的内部划分位置处,同步位置具体用法见SequenceFile.Reader
	 */
}
class SequenceReaderSample{//运行命令:$ hadoop jar SequenceReaderSample.jar SequenceReaderSample
	public static void main(String[] args)throws Exception{
		Configuration conf = new Configuration();
		FileSystem fs = FileSystem.get(conf);//获取文件系统
		Path path = new Path("cool.txt");//定义输出路径
		SequenceFile.Reader reader = new SequenceFile.Reader(fs, path, conf);//创建实例
		
		IntWritable key = new IntWritable();//创建待读入Key实例
		Text value = http://www.mamicode.com/new Text();//创建待读入value实例>