windows下运行mapreduce(本地运行)

windows下运行mapreduce并不需要配置什么文件,hadoop-2.7.3.tar.gz下载后解压,然后添加环境变量,主要是添加hadoop目录下的bin目录。然后在网上下载hadoop.dll(https://github.com/SweetInk/hadoop-common-2.7.1-bin)和winutils.exe(https://github.com/steveloughran/winutils ),把hadoop.dll放到c://windows/system32文件夹中,把winutils.exe放到hadoop/bin下,然后可以运行以下代码进行测试代码。不需要更改配置文件。

public class WordCount {
	public static class WordCountMapper extends Mapper{
		@Override
		protected void map(LongWritable key, Text value, Mapper.Context context)
				throws IOException, InterruptedException {
				String [] words = value.toString().split(" ");
			for (String word : words) {
				context.write(new Text(word), new IntWritable(1));
			}
		}
	}
	public static class WordCountReducer extends Reducer{
		@Override
		protected void reduce(Text key, Iterable value,Context context) throws IOException, InterruptedException {
			int sum = 0;
			for (IntWritable intWritable : value) {
				sum += intWritable.get();
			}
			context.write(key, new IntWritable(sum));
		}
	}

	public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
		Configuration conf = new Configuration();
		conf.set("mapreduce.framework.name", "local");
        Job job = Job.getInstance(conf);
        
        job.setJarByClass(WordCount.class);

        //设置Mapper类和Reducer类
        job.setMapperClass(WordCountMapper.class);
        job.setReducerClass(WordCountReducer.class);
        //设置Map端输出key类和输出value类
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        //设置Reduce端输出key类和输出value类
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        FileInputFormat.addInputPath(job,new Path("C:\\Users\\123\\Desktop\\wordcounttest.txt"));
        FileOutputFormat.setOutputPath(job,new Path("output"));
        //执行任务
        boolean status = job.waitForCompletion(true);
		System.out.println(status);
	}

}

注意更改一下更改一下input路径和output路径,然后看output是不是有结果,有结果代表成功了。

你可能感兴趣的:(mapreduce)