你好,游客 登录 注册 搜索
背景:
阅读新闻

MapReduce的一对多连接操作

[日期:2013-09-04] 来源:Linux社区  作者:computerdragon [字体:大 中 小]

MapReduce

package whut.onetomany;
import java.io.IOException;
import java.util.Iterator;
import org.apache.Hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
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.Mapper.Context;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.input.MultipleInputs;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
public class JoinMain extends Configured implements Tool {
    public static class JoinMapper extends Mapper<LongWritable, Text, TextIntPair, Text>
    {
        private TextIntPair tp=new TextIntPair();
        private Text val=new Text();
        @Override
        protected void map(LongWritable key, Text value, Context context)
                throws IOException, InterruptedException {
            // TODO Auto-generated method stub
            //获取要处理的文件的名称
            FileSplit file=(FileSplit)context.getInputSplit();
            String fileName=file.getPath().toString();
            //获取输入行分隔
            String line=value.toString();
            String[] lineKeyValue=line.split("\"");
            String lineKey=lineKeyValue[0];
            String lineValue=lineKeyValue[1];
            tp.setFirstKey(lineKey);
            //判断是否是trade文件
            if(fileName.indexOf("trade")>=0)
            {
                tp.setSecondKey(0);
                val.set(lineValue);
            }
            //判断是否是pay文件
            else if(fileName.indexOf("pay")>=0)
            {
                tp.setSecondKey(1);
                val.set(lineValue);
            }
            context.write(tp, val);
        }
    }
                       
    public static class JoinReducer extends Reducer<TextIntPair, Text, Text, Text>
    {
        @Override
        protected void reduce(TextIntPair key, Iterable<Text> values,
                Context context)throws IOException, InterruptedException {
            Iterator<Text> valList=values.iterator();
            //注意这里一定要写成string不可变,写成Text有问题
            //Text trade=valList.next();
            String tradeName=valList.next().toString();
            while(valList.hasNext())
            {
                Text pay=valList.next();
                context.write(new Text(tradeName), pay);
            }
        }
    }
    @Override
    public int run(String[] args) throws Exception
    {
        Configuration conf=getConf();
        Job job=new Job(conf,"JoinJob");
        job.setJarByClass(JoinMain.class);
        //ToolRunner已经利用GenericOptionsParser解析了命令行中的参数
        //并且将其存放在数组中,传递给该run()方法了
        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileInputFormat.addInputPath(job, new Path(args[1]));
        //输入文件必须以,隔开
        //FileInputFormat.addInputPaths(job, args[0]);
        FileOutputFormat.setOutputPath(job, new Path(args[2]));
                           
        job.setMapperClass(JoinMapper.class);
        job.setReducerClass(JoinReducer.class);
        //设置分区方法
        job.setPartitionerClass(PartitionByText.class);
        //设置分组排序
        job.setGroupingComparatorClass(TextComparator.class);
        job.setSortComparatorClass(TextIntComparator.class);
                           
        job.setMapOutputKeyClass(TextIntPair.class);
        job.setMapOutputValueClass(Text.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);
        job.waitForCompletion(true);
        int exitCode=job.isSuccessful()?0:1;
        return exitCode;
    }
    public static void main(String[] args)throws Exception
    {
        // TODO Auto-generated method stub
        int code=ToolRunner.run(new JoinMain(), args);
        System.exit(code);
    }
}

注意:
    一般有些地方没有定义组内排序策略,但是经过多次测试,发现无法保证第一个表在第二个表之前到达,则这里就自定义了组内排序策略。版本号为Hadoop1.1.2

更多Hadoop相关信息见Hadoop 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=13

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款