基于文件的数据结构之SequenceFile实现

Hadoop的sequenceFile为二进制键/值对提供了一个持久数据结构。它可以作为小文件的容器。HDFS和MapReduce是针对大文件优化的,所以通过SequenceFile类型将小文件包装起来,可以获得更高效率的存储和处理。

SequenceFile的实现代码:

package com.jr.sequencefile;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.SequenceFile.Writer;
import org.apache.hadoop.io.Text;
import org.junit.Test;

public class TestSequenceFile {
	/**
	 * 写入
	 * @throws IOException 
	 */
	@Test
	public void write() throws IOException {
		Configuration conf=new Configuration();
		FileSystem fs=FileSystem.get(conf);
		Path name=new Path("/user/centos/hadoop/meseq2.seq");
		
		IntWritable iw=new IntWritable();
		Text txt=new Text();
		
		Writer w=SequenceFile.createWriter(fs, conf, name, IntWritable.class, Text.class);
		for(int i=0;i<1000;i++) {
			w.append(new IntWritable(i), new Text("tom"+i));
		}
		w.close();
	}
	
	/**
	 * 读数据
	 */
	@Test
	public void read() throws IOException {
		Configuration conf=new Configuration();
		FileSystem fs=FileSystem.get(conf);
		Path name=new Path("/user/centos/hadoop/meseq2.seq");
		
		IntWritable key=new IntWritable();
		Text txt=new Text();
		SequenceFile.Reader reader=new SequenceFile.Reader(fs, name,conf);
		
		while(reader.next(key)) {
			reader.getCurrentValue(txt);
			System.out.println(key.get()+":"+txt.toString());
		}
	}
	
	/**
	 * 写入无序的数据
	 * @throws IOException 
	 */
	@Test
	public void writeNoOrder() throws IOException {
		Configuration conf=new Configuration();
		FileSystem fs=FileSystem.get(conf);
		Path name=new Path("/user/centos/hadoop/meseqNoOrder.seq");
		
		IntWritable iw=new IntWritable();
		Text txt=new Text();
		
		Writer w=SequenceFile.createWriter(fs, conf, name, IntWritable.class, Text.class);
		w.append(new IntWritable(2), new Text("tom2"));
		w.append(new IntWritable(1), new Text("tom1"));
		w.append(new IntWritable(5), new Text("tom5"));
		w.append(new IntWritable(3), new Text("tom3"));
		w.close();
	}
	
	/**
	 * 对sequence进行排序
	 * @throws IOException 
	 */
	@Test
	public void sortSeqFile() throws IOException {
		Configuration conf=new Configuration();
		FileSystem fs=FileSystem.get(conf);
		Path src=new Path("/user/centos/hadoop/meseqNoOrder.seq");
		Path dest=new Path("/user/centos/hadoop/meseqOrder.seq");
		
		IntWritable iw=new IntWritable();
		Text txt=new Text();
		
		//创建排序对象
		SequenceFile.Sorter sorter=new SequenceFile.Sorter(fs, IntWritable.class, Text.class, conf);
		sorter.sort(src,dest);
	}
	
	/**
	 * 合并文件
	 * @throws IOException 
	 */
	@Test
	public void mergeFile() throws IOException {
		Configuration conf=new Configuration();
		FileSystem fs=FileSystem.get(conf);
		Path src=new Path("/user/centos/hadoop/meseqNoOrder.seq");
		Path dest=new Path("/user/centos/hadoop/meseqOrder.seq");
		Path merge=new Path("/user/centos/hadoop/mergeFile.seq");
		
		IntWritable iw=new IntWritable();
		Text txt=new Text();
		
		//创建排序对象
		SequenceFile.Sorter sorter=new SequenceFile.Sorter(fs, IntWritable.class, Text.class, conf);
		sorter.merge(new Path[] {src,dest}, merge);
	}
}

查看各个方法执行结果:

1.public void write():



2.public void read()读数据:


3.public void writeNoOrder()写入无序的数据


4.public void sortSeqFile() 对 sequence进行排序


5.public void mergeFile()合并(合并文件没有排序处理)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值