java编写mapreduce并在hadoop中运行

原文:https://www.cnblogs.com/jetdw/p/7238105.html?utm_source=itdadao&utm_medium=referral

1、mapreduce代码

package test.mapreduce;

import java.io.IOException;
import java.util.StringTokenizer;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;

/**
 * 该例子为网上经典例子统计单词出现次数
 *
 */
public class WordCount {
    public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {

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

        /**
         * key 偏移量包括了回车所占的字符数(Windows和Linux环境会不同)
         * value 一行数据
         * context存储新Map的对象
         */
        public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
            StringTokenizer itr = new StringTokenizer(value.toString());
            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();

        /**
         * key 为Map中的key,hadoop会把相同key的内容合并为一个list,该list就为values。
         * context为存放结果的对象
         */
        public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException,
                InterruptedException {
            int sum = 0;
            for (IntWritable val : values) {
            	//累加每一个value
                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);
        }
        Job job = Job.getInstance();
        
        //设置本次job作业使用的mapper类是那个
        job.setJarByClass(WordCount.class);
        //本次job作业使用的mapper类是那个?
        job.setMapperClass(TokenizerMapper.class);
        //本次job作业使用的reducer类是那个
        job.setCombinerClass(IntSumReducer.class);
        //本次job作业使用的reducer类是那个
        job.setReducerClass(IntSumReducer.class);
        //本次job作业使用的reducer类的输出数据key类型
        job.setOutputKeyClass(Text.class);
        //本次job作业使用的reducer类的输出数据value类型
        job.setOutputValueClass(IntWritable.class);
        
        job.setNumReduceTasks(1);//设置reduce的个数
        
        
        //判断output文件夹是否存在,如果存在则删除
        Path path = new Path(otherArgs[1]);
        FileSystem fileSystem =path.getFileSystem(conf);//根据path找到这个文件夹
        if(fileSystem.exists(path)) {
        	fileSystem.delete(path,true);//true的意思是,就算output里面有东西,也一带删除
        }
        
        //本次job作业要处理的原始数据所在的路径
        FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
        //本次job作业产生的结果输出路径
        FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
        
        //提交本次作业
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}

2、在hadoop中运行

hadoop jar /usr/local/hadoop_demo.jar test.mapreduce.WordCount /bbb.txt /output
hadoop jar 【jar包文件】 【jar包中main函数所在的类(包括包名)】 【需统计的文件】 【输出路径】 


Python是支持MapReduce编程模式的,但是Hadoop本身是用Java编写的,因此在Hadoop运行MapReduce任务时,Java是官方推荐的开发语言。不过,为了方便其他语言的开发者,Hadoop提供了一个名为Hadoop Streaming的工具,它允许用户使用任何可以读取标准输入和输出的程序作为MapReduce作业的Mapper和Reducer。 以下是使用Python编写MapReduce程序并在Hadoop运行的一般步骤: 1. 准备数据:首先需要准备输入数据,这些数据将被上传到HDFS(Hadoop分布式文件系统)。 2. 编写Python脚本: - 通常需要编写两个脚本,一个用于Map任务,另一个用于Reduce任务。 - Map脚本负责读取输入数据,将输入数据转换成一系列的键值对(key-value pairs)。 - Reduce脚本则对Map阶段输出的数据进行排序和汇总,生成最终结果。 3. 使用Hadoop Streaming运行Python脚本: - 在命令行使用Hadoop Streaming命令提交MapReduce作业。 - 命令格式大致如下: ``` hadoop jar /path/to/hadoop-streaming.jar \ -file /path/to/map.py -mapper /path/to/map.py \ -file /path/to/reduce.py -reducer /path/to/reduce.py \ -input /path/to/input -output /path/to/output ``` - 其,`-file`参数用于指定Map和Reduce脚本的位置,`-mapper`和`-reducer`用于指定Map和Reduce的脚本,`-input`和`-output`用于指定输入输出的HDFS路径。 4. 监控作业运行:通过Hadoop的Web界面或者命令行工具来监控MapReduce作业的运行状态,确保作业顺利完成。 5. 检查结果:作业完成后,可以在指定的输出路径检查结果文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值