Hadoop-SequenceFile code

本文提供了一个使用Hadoop SequenceFile的示例程序,演示了如何创建SequenceFile并将数据写入其中。通过具体代码展示了SequenceFile的基本用法,包括配置设置、文件系统的获取以及数据的写入过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

写的例子:

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值