`
san_yun
  • 浏览: 2595150 次
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

Hadoop-SequenceFile code

 
阅读更多

写的例子:

package mytest.mapreduce;

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.IOUtils;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.Text;

public class MySequenceTester {

	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 {
		Configuration conf = new Configuration();
		FileSystem fs = FileSystem.get(conf);
		Path path = new Path("/duitang/data/test/client-request.log.seq");

		IntWritable key = new IntWritable();
		Text value = new Text();

		SequenceFile.Writer writer = SequenceFile.createWriter(fs, conf, path, key.getClass(), value.getClass());
		try {

			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);
		}
	}
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics