博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hadoop SequenceFile Writer And Reader
阅读量:2395 次
发布时间:2019-05-10

本文共 3242 字,大约阅读时间需要 10 分钟。

 

package cn.edu.xmu.dm.mpdemo.ioformat;import java.io.IOException;import java.net.URI;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.SequenceFile.CompressionType;import org.apache.hadoop.io.Text;/** * desc: SequenceFileWriter * SequenceFileWriteDemo *  * @author chenwq (irwenqiang@gmail.com) * @version 1.0 2012/05/19 */public class SequenceFileWriteDemo {	private static final String[] DATA = { "One, two, buckle my shoe",			"Three, four, shut the door", "Five, six, pick up sticks",			"Seven, eight, lay them straight", "Nine, ten, a big fat hen" };	public static void main(String[] args) throws IOException {		String uri = args[0];		Configuration conf = new Configuration();		FileSystem fs = FileSystem.get(URI.create(uri), conf);		Path path = new Path(uri);		IntWritable key = new IntWritable();		Text value = new Text();		SequenceFile.Writer writer = null;		try {			/**			 * fs: outputstream			 * conf: configuration object			 * key: the key' type			 * value: the value's type			 */			writer = SequenceFile.createWriter(fs, conf, path, key.getClass(),					value.getClass());//			writer = SequenceFile.createWriter(fs, conf, path, key.getClass(),//					value.getClass(), CompressionType.BLOCK);			for (int i = 0; i < 100; i++) {				key.set(100 - i);				value.set(DATA[i % DATA.length]);				System.out.printf("[%s]\t%s\t%s\n", writer.getLength(), key,						value);				writer.append(key, value);			}		} finally {			IOUtils.closeStream(writer);		}	}}

 

 

 

package cn.edu.xmu.dm.mpdemo.ioformat;import java.io.IOException;import java.net.URI;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.SequenceFile;import org.apache.hadoop.io.Writable;import org.apache.hadoop.util.ReflectionUtils;/** * desc: SequenceFileReader * SequenceFileReadDemo *  * @author chenwq (irwenqiang@gmail.com) * @version 1.0 2012/05/19 */public class SequenceFileReadDemo {	public static void main(String[] args) throws IOException {		String uri = args[0];		Configuration conf = new Configuration();		FileSystem fs = FileSystem.get(URI.create(uri), conf);		Path path = new Path(uri);		SequenceFile.Reader reader = null;		try {			reader = new SequenceFile.Reader(fs, path, conf);			Writable key = (Writable) ReflectionUtils.newInstance(					reader.getKeyClass(), conf);			Writable value = (Writable) ReflectionUtils.newInstance(					reader.getValueClass(), conf);			long position = reader.getPosition();			while (reader.next(key, value)) {				String syncSeen = reader.syncSeen() ? "*" : "";				System.out.printf("[%s%s]\t%s\t%s\n", position, syncSeen, key,						value);				position = reader.getPosition(); // beginning of next record			}		} finally {			IOUtils.closeStream(reader);		}	}}

 

 

 

使用Block压缩后的大小对比:

 

root@ubuntu:~# hadoop fs -ls mpdemo/Found 2 items-rw-r--r--   3 root supergroup       4788 2012-05-19 00:11 /user/root/mpdemo/seqinput-rw-r--r--   3 root supergroup        484 2012-05-19 00:17 /user/root/mpdemo/seqinputblock
 

 

转载地址:http://wtwob.baihongyu.com/

你可能感兴趣的文章
Servlet--关于RequestDispatcher(forward、include)的原理
查看>>
Servlet--Cookie原理及API使用详解
查看>>
Servlet--Session原理及API的使用
查看>>
Servlet--三个作用域(Request、Session、ServletContext)总结
查看>>
Listener--监听器的分类、功能及API详解
查看>>
Listener--ServletContextListener接口的使用详解
查看>>
Listener--HttpSessionListener、ServletRequestListener接口的使用详解
查看>>
Listener--域对象属性变化监听器([ServletRequest | HttpSession | ServletContext] AttributeListener)API详解
查看>>
Listener--HttpSessionBindingListenerAPI及使用(在线人数统计)详解
查看>>
Listener--HttpSessionActivationListener(钝化、活化)API、配置和使用详解
查看>>
Filter--过滤器Filter概述、API、配置与使用详解
查看>>
Web--使用Filter和Cookie实现自动登录
查看>>
Web--Filter使用装饰器模式解决全站中文乱码问题
查看>>
JS--JavaScript入门(script标签使用与外部JavaScript文件引入)
查看>>
JS--JavaScript语句(表达式语句、语句块、条件语句if、switch)详解
查看>>
JS--JavaScript语句(循环语句、跳转语句、异常处理语句、function语句)详解
查看>>
JS--JavaScript数据类型(数值、字符串、布尔值)详解
查看>>
JS--JavaScript函数(匿名函数、闭包函数等)详解
查看>>
JS--JavaScript对象类型
查看>>
JS--JavaScript数组Array(join、split、reverse、concat、slice)详解
查看>>