hadoop随笔二之MR-wordcount小试

基于随笔一搭建的环境,跑个wordcount任务测试。

代码如下:

public class WordCount {

	static final String INPUT_PATH = "hdfs://192.168.67.100:9000/input";
	static final String OUT_PATH = "hdfs://192.168.67.100:9000/output";

	public static void main(String[] args)
			throws IOException, URISyntaxException, ClassNotFoundException, InterruptedException {
		Configuration conf = new Configuration();

		// conf.set("fs.default.name", "hdfs://192.168.67.100:9000");
		// conf.set("hadoop.job.user", "root");
		// conf.set("mapred.job.tracker", "master:9001");
		final FileSystem fileSystem = FileSystem.get(new URI(INPUT_PATH), conf);
		final Path outPath = new Path(OUT_PATH);
		if (fileSystem.exists(outPath)) {
			fileSystem.delete(outPath, true);
		}
		// job实例
		Job job = Job.getInstance(conf, WordCount.class.getName());
		// 输入路径
		FileInputFormat.setInputPaths(job, INPUT_PATH);

		job.setMapperClass(Mapper1.class);
		job.setReducerClass(Reduce1.class);

		// 降序排列
		job.setSortComparatorClass(TextDescComparator.class);
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(IntWritable.class);

		FileOutputFormat.setOutputPath(job, outPath);
		job.setJar("/usr/example/Word1.jar");
		// 把job提交给JobTracker运行
		job.waitForCompletion(true);

	}

	static IntWritable mapInt = new IntWritable(1);

	public static class Mapper1 extends org.apache.hadoop.mapreduce.Mapper<Object, Text, Text, IntWritable> {

		@Override
		protected void map(Object key, Text value,
				org.apache.hadoop.mapreduce.Mapper<Object, Text, Text, IntWritable>.Context context)
						throws IOException, InterruptedException {
			final String[] splited = value.toString().split("\t");
			for (String word : splited) {
				context.write(new Text(word), mapInt);
			}

		}

	}

	public static class Reduce1 extends Reducer<Text, IntWritable, Text, IntWritable> {

		@Override
		protected void reduce(Text arg0, Iterable<IntWritable> arg1,
				Reducer<Text, IntWritable, Text, IntWritable>.Context context)
						throws IOException, InterruptedException {
			int sum = 0;
			for (IntWritable iw : arg1) {
				sum += iw.get();
			}

			context.write(arg0, new IntWritable(sum));
		}

	}

	public static class TextDescComparator extends Text.Comparator {
		public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {
			return -super.compare(b1, s1, l1, b2, s2, l2);
		}
	}
}
该功能是实现计算单词数,及逆序排序。

public class WordCountSort {

	static final String INPUT_PATH = "hdfs://192.168.67.100:9000/sort";
	static final String OUT_PATH = "hdfs://192.168.67.100:9000/output";

	public static void main(String[] args)
			throws IOException, URISyntaxException, ClassNotFoundException, InterruptedException {
		Configuration conf = new Configuration();

		final FileSystem fileSystem = FileSystem.get(new URI(INPUT_PATH), conf);
		final Path outPath = new Path(OUT_PATH);
		if (fileSystem.exists(outPath)) {
			fileSystem.delete(outPath, true);
		}
		// job实例
		Job job = Job.getInstance(conf, WordCountSort.class.getName());
		// 输入路径
		FileInputFormat.setInputPaths(job, INPUT_PATH);

		job.setMapperClass(Mapper1.class);
		job.setReducerClass(Reduce1.class);

		job.setMapOutputKeyClass(LongWritable.class);
		job.setMapOutputValueClass(Text.class);

		FileOutputFormat.setOutputPath(job, outPath);
		job.setJar("/usr/example/WordSort.jar");
		// 把job提交给JobTracker运行
		job.waitForCompletion(true);

	}

	public static class Mapper1 extends org.apache.hadoop.mapreduce.Mapper<LongWritable, Text, LongWritable, Text> {
		private static LongWritable wordCount = new LongWritable(1);
		private Text word = new Text();

		@Override
		protected void map(LongWritable key, Text value,
				org.apache.hadoop.mapreduce.Mapper<LongWritable, Text, LongWritable, Text>.Context context)
						throws IOException, InterruptedException {
			// final String[] splited = value.toString().split("\t");
			StringTokenizer tokenizer = new StringTokenizer(value.toString());
			while (tokenizer.hasMoreTokens()) {
				word = new Text(tokenizer.nextToken().trim());
				wordCount = new LongWritable(Integer.valueOf(tokenizer.nextToken().trim()));
				context.write(wordCount, word);// <k,v>互换
			}

		}
	}

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

		@Override
		protected void reduce(LongWritable key, Iterable<Text> values,
				Reducer<LongWritable, Text, Text, LongWritable>.Context context)
						throws IOException, InterruptedException {
			for (Text val : values) {
				result.set(val.toString());
				context.write(result, key);// <k,v>互换
			}
		}

	}

}
该代码基于第一次计数后按词频排序。

结果如下:


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值