使用MapReduce计算文档中单词出现的次数

↘锁芯ラ 提交于 2019-11-28 09:40:37

编写MapReduce的Map函数类

/**
	 * MapReduce的Map函数类
	 */
	public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {
		// 在输出时不直接使用int或Integer是由于HDFS是一种分布式的文件系统
		private final static IntWritable one = new IntWritable(1);// 相当于int或者intger设置参数为1
		private Text word = new Text();

		@Override
		public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
			// 显示Map阶段Key的值
			System.out.println("Key:" + key.toString());
			// 显示Map阶段Value的值
			// 默认情况下,输入数据的值在此是按行读取的
			System.out.println("Value:" + value.toString());
			// 使用单词的自然分隔分隔单词
			StringTokenizer itr = new StringTokenizer(value.toString());
			while (itr.hasMoreTokens()) {
				// 确定Map阶段输出的key
				word.set(itr.nextToken());
				// 将单词统一生一个键值对
				context.write(word, one);
			}
		}
	}

编写MapReduce的reduce函数类

/**
	 * MapReduce的reduce函数类
	 */
	public static class IntSumReduce extends Reducer<Text, IntWritable, Text, IntWritable> {
		private IntWritable result = new IntWritable();

		@Override
		public void reduce(Text key, Iterable<IntWritable> values, Context context)
				throws IOException, InterruptedException {
			/*
			 * 在map阶段有个单词: word:1;word:1;word:1 在reduce阶段会将所有相同的键归并为一个,其值就是一个列表
			 */
			// 显示Reduce阶段输入的key
			System.out.println("Reduce阶段的Key:" + key);
			// 显示Reduce阶段输入的值
			System.out.println("Reduce阶段的Value:" + values);
			// 显示每一个结果
			/*
			 * for (IntWritable v :values){ System.out.print(v.get() + ","); }
			 * System.out.println();
			 */
			int sum = 0;
			for (IntWritable val : values) {
				sum += val.get();
			}
			result.set(sum);
			context.write(key, result);
		}
	}

MapReduce程序的入口

/**
	 * MapReduce程序的入口
	 * 
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		// hadoop yarn的配置
		Configuration conf = new Configuration();
		conf.set("dfs.permissions", "false");
		// conf.set("fs.defaultFS","hdfs://192.168.211.211:9000");
		// 实例化一个任务
		Job job = Job.getInstance(conf, "word count");
		// 设置MapReduce中的入口函数类
		job.setJarByClass(WordCount.class);
		// 设置MapReduce中的Map函数所在的类
		job.setMapperClass(TokenizerMapper.class);
		// 设置MapReduce中的Reduce函数所在的类
		job.setReducerClass(IntSumReduce.class);
		// 设置Combiner
		job.setCombinerClass(IntSumReduce.class);
		// 设置输出键的类型
		job.setOutputKeyClass(Text.class);
		// 设置输出值的类型
		job.setOutputValueClass(IntWritable.class);
		// 设置输入数据的位置
		FileInputFormat.addInputPath(job, new Path(args[0]));
		// 删除已存在的输出目录
		Path path = new Path("output");
		// 在判断之前要获取HDFS文件系统
		FileSystem fs = FileSystem.get(conf);
		// 判断输出目录是否存在
		if (fs.exists(path)) {
			// 删除目录
			boolean flag = fs.delete(path, true);
			if (flag) {
				System.out.println("删除成功.......");
			} else {
				System.out.println("MapReduce运行时的输出目录必须为空.");
				return;
			}
		}
		// 设置输出目录
		FileOutputFormat.setOutputPath(job, new Path(args[1]));
		// 提交运行
		job.submit();
		// 系统退出
		System.exit(job.waitForCompletion(true) ? 1 : 0);
	}

添加静态代码块

	static {
		try {
			System.load("D:\\hadoop\\bin\\hadoop.dll"); 这里的路径记得改
		} catch (UnsatisfiedLinkError e) {
			System.err.println("Native code library failed to load.\n" + e);
			System.exit(1);
		}
	}

添加WordCount
在这里插入图片描述
在这里插入图片描述
最后会自动生成一个output文件夹,打开里面的
在这里插入图片描述
效果如下在这里插入图片描述

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!