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<LongWritable, Text, Text,IntWritable>{
		@Override
		protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.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<Text, IntWritable, Text, IntWritable>{
		@Override
		protected void reduce(Text key, Iterable<IntWritable> 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是不是有结果,有结果代表成功了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值