利用DistributedCache实现map端连接 Join On Map Side

本文介绍了一种使用MapJoin技术在Hadoop环境下高效处理电影评分数据集的方法,通过读取并缓存电影数据文件,实现电影ID与名称的快速映射,进而进行数据集的JOIN操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

publicclassMapJoinByCache {

 // movies.dat id,name,type

//rating.dat uid,mid,rate,time

//join on map:movies.dat join rating.dat on mid

    publicstaticclassMapJoiner extendsMapper<LongWritable,Text,Text,Text>

    {  

        staticMap<String,String> movies=newHashMap<String,String>();

        publicvoidsetup(Context context) {           

            try {

                FileReader reader = new FileReader("movies.dat");

                BufferedReader br = new BufferedReader(reader);

                Strings1 = null;

                while ((s1= br.readLine()) != null)

                {

                    System.out.println(s1);

                    String[]splits= s1.split("::");                   

                    StringmovieId=splits[0];

                    StringmovieName =splits[1];

                    movies.put(movieId,movieName);                

                }

                br.close();

                reader.close();

            }catch (Exception e) {

                e.printStackTrace();

            }

        }

        private Text outKey=newText();

        private Text outVal=newText();    

        publicvoidmap(LongWritable key,Text value,Context context)throwsIOException, InterruptedException

        {

            if(value!=null||value.toString()!=null)

            {

                String[]splits = value.toString().split("::"); 

                StringmovieId =splits[1];

                StringmovieName= movies.get(movieId);

                outKey.set(movieId);

                outVal.set(movieName+"::"+value.toString());

                context.write(outKey, outVal);

            }

        }

    }

    publicstaticclassDirectReducer extendsReducer<Text,Text,NullWritable,Text>

    {

        NullWritableoutKey=NullWritable.get();

        publicvoidreduce(Text key,Iterable<Text> values,Context context)throwsIOException, InterruptedException

        {

            for(Textvalue :values)

            {

                context.write(outKey,value);

            }

        }

    }

    publicstaticvoidmain(String[] args) throwsURISyntaxException, IOException, InterruptedException, ClassNotFoundException {

        Configurationconf =new Configuration();

        String[]otherArgs = new GenericOptionsParser(conf,args).getRemainingArgs();

        DistributedCache.createSymlink(conf);

        DistributedCache.addCacheFile(new URI("hdfs://mylinux:9000/data/exam/movie/movies.dat#movies.dat"),conf);     

        Jobjob=newJob(conf);

        job.setJobName("Joinon Map Side");

        job.setJarByClass(MapJoinByCache.class);

        job.setMapperClass(MapJoiner.class);

        job.setReducerClass(DirectReducer.class);

        FileInputFormat.addInputPath(job,new Path(otherArgs[0]));

        job.setMapOutputKeyClass(Text.class);

        job.setMapOutputValueClass(Text.class);

        job.setOutputKeyClass(NullWritable.class);

        job.setOutputValueClass(Text.class);

        FileOutputFormat.setOutputPath(job,new Path(otherArgs[1]));       

        System.exit(job.waitForCompletion(true) ? 0: 1);

}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值