優酷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);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章