基于Chukwa Sequence File的MapReduce

关于Chukwa配置及运行实例,请参见:

http://my.oschina.net/xiangchen/blog/100424

Chukwa将收集到的数据以Sink Files的形式写入到HDFS中,如果不做Archive和Demux操作的话,默认存储在hdfs:///chukwa/logs目录下。

Sink File是Hadoop Sequence File,包含key-value集合。

其中key的类型为org.apache.hadoop.chukwa.ChukwaArchiveKey;

value的类型为org.apache.hadoop.chukwa.ChunkImpl.

一个完整的MapReduce过程可参考代码:

http://javasourcecode.org/html/open-source/chukwa/chukwa-0.4.0/org/apache/hadoop/chukwa/extraction/demux/Demux.java.html

假定已经有Sequence File文件写入到hdfs:///chukwa/logs目录下,我们要对该文件做一个最基本的Word Count MapReduce操作。代码如下:

Tips: 读取Sequece File需要注明job.setInputFormatClass(SequenceFileInputFormat.class)

public class WordCount_SequenceFile {

	public static class TokenizerMapper extends
		Mapper<ChukwaArchiveKey, ChunkImpl, Text, IntWritable> {

		private final static IntWritable one = new IntWritable(1);
		private Text word = new Text();

		public void map(ChukwaArchiveKey key, ChunkImpl value, Context context)
				throws IOException, InterruptedException {
			
			StringTokenizer itr = new StringTokenizer(new String(value.getData()));
			
			while (itr.hasMoreTokens()) {
				word.set(itr.nextToken());
				context.write(word, one);
			}
		}
	}

	public static class IntSumReducer extends
		Reducer<Text, IntWritable, Text, IntWritable> {
		private IntWritable result = new IntWritable();

		public void reduce(Text key, Iterable<IntWritable> values,
				Context context) throws IOException, InterruptedException {
			int sum = 0;
			for (IntWritable val : values) {
				sum += val.get();
			}
			result.set(sum);
			context.write(key, result);
		}
	}

	public static void main(String[] args) throws Exception {
		Configuration conf = new Configuration();

		String[] otherArgs = new GenericOptionsParser(conf, args)
				.getRemainingArgs();
		if (otherArgs.length != 2) {
			System.err.println("Usage: wordcount <in> <out>");
			System.exit(2);
		}

		@SuppressWarnings("deprecation")
		Job job = new Job(conf, "word count");

		job.setJarByClass(WordCount_SequenceFile.class);

		job.setInputFormatClass(SequenceFileInputFormat.class);
		 
		job.setMapperClass(TokenizerMapper.class);
		job.setCombinerClass(IntSumReducer.class);
		job.setReducerClass(IntSumReducer.class);

		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(IntWritable.class);

		FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
		FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));

		System.exit(job.waitForCompletion(true) ? 0 : 1);
	}
}
关于MapReduce程序的编译,请参见:

http://my.oschina.net/xiangchen/blog/102091

在运行时需要添加chukwa-0.5.0.jar库:

hadoop jar MapReduce.jar demo.mapreduce.WordCount_SequenceFile
-libjars MapReduce.jar,chukwa-0.5.0.jar /chukwa/logs/xx.done /output_file

转载于:https://my.oschina.net/xiangchen/blog/102325

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值