优酷hadoop mapred 面试题[find friends]

package com.sanmao.hadoop_02.mianshi;

import com.sanmao.hadoop_02.mr.WordCountTest;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
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.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**
 *   mapred 找共同朋友,数据格式如下
 1. A B C D E F
 2. B A C D E
 3. C A B E
 4. D A B E
 5. E A B C D
 6. F A
 */
public class findFriends {
    private static Logger wcLogger = LoggerFactory.getLogger(WordCountTest.class);

    /**
     *  编写map类,负责从文件中逐行解析
     * */
    static class FindMapper extends Mapper<Object,Text,Text,Text>{
        @Override
        protected void setup(Context context) throws IOException, InterruptedException {
            super.setup(context);
        }

        @Override
        protected void map(Object k1, Text v1, Context context) throws IOException, InterruptedException {
            //StringTokenizer  的功能跟split一样
            //返回值 类似一个迭代器
            StringTokenizer itr = new StringTokenizer(v1.toString());
            Text owner= new Text();
            Set<String> set = new TreeSet<String>();
            //首元素是本人,之后的是主人的朋友,分开存储,主人放到Text中,她的朋友放到Set集合里面
            owner.set(itr.nextToken());
            while(itr.hasMoreTokens()){
                set.add(itr.nextToken());
            }
            String[] friends = new String[set.size()];
            friends = set.toArray(friends);

            for (int i = 0; i < friends.length; i++) {
                for (int j = i+1; j < friends.length; j++) {
                    //k2 是一对对的关系 AB AC AD  AF  ,v2 是主人 A
                    String outputKey = friends[i]+friends[j];
                    context.write(new Text(outputKey),owner);
                }
            }
        }

        @Override
        protected void cleanup(Context context) throws IOException, InterruptedException {
            super.cleanup(context);
        }
    }
    static class FindReducer extends Reducer<Text,Text,Text,Text>{
        @Override
        protected void setup(Context context) throws IOException, InterruptedException {
            super.setup(context);
        }

        @Override
        protected void reduce(Text k2, Iterable<Text> v2, Context context) throws IOException, InterruptedException {
            String commonfrends = "";
            for (Text val: v2) {
                if (commonfrends == ""){
                    commonfrends = val.toString();
                }
                else{
                    commonfrends = commonfrends +":"+val.toString();
                }
            }
            context.write(k2,new Text(commonfrends));
        }

        @Override
        protected void cleanup(Context context) throws IOException, InterruptedException {
            super.cleanup(context);
        }
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        if (args == null || args.length<2){
            wcLogger.error("parameter errors! Useage: <input_path> <output_path>");
            System.exit(-1);
        }
        String inputPath = args[0];
        String outputPath = args[1];
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf,findFriends.class.getSimpleName());
        //在集群中执行jar 必须  要添加这一句话
        job.setJarByClass(findFriends.class);
        //如果输出目录已经存在,干掉
        FileSystem fs = FileSystem.newInstance(conf);
        fs.delete(new Path(outputPath),true);
        //给Map设置输入数据源
        FileInputFormat.setInputPaths(job,inputPath);
        //重点 : 设置MR
        //设置Map
        job.setMapperClass(findFriends.FindMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);
        job.setInputFormatClass(TextInputFormat.class);

        //设置reducer
        FileOutputFormat.setOutputPath(job, new Path(outputPath));
        job.setReducerClass(findFriends.FindReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);

        job.setOutputFormatClass(TextOutputFormat.class);

        //设置reducer的数量
        job.setNumReduceTasks(1);
        job.waitForCompletion(true);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值