DataJoin多資料來源的Reduce端連結實現方法

來源:互聯網
上載者:User

為了完成不同資料來源的連結,首先,需要為不同資料來源下的每個記錄定義一個資料來源標籤(Tag),接著,為了表示每個資料來源下的不同記錄並且完成串連處理,需要為每個資料記錄設定一個主鍵(GroupKey),然後,DataJoin類庫分別在Map階段和Reduce階段提供一個處理架構,僅僅留下一些任務有程式員完成。下面是處理過程:

從上述過程可以看到,多資料來源的資料會首先被處理成多個資料記錄,這些記錄是帶有標籤Tag和主鍵Group Key的記錄,因此使用DataJoin時,我們需要實現generateInputTag(String inputFile)方法和generateTaggedMapOutput(Object value)和generateGroupKey(TaggedMapOutput aRecord)方法,在這個過程中,出現了一個新的類(即帶有標籤的記錄類),因此我們也要實現自訂的記錄類。在combine過程中,我們會對笛卡爾積的結果進行整合(這也是為何我們把DataJoin叫做Reduce端串連),因此我們需要實現一個combine(Object[] tags,Object[] values)方法,注意這個combine和MapReduce架構中的combine是兩個完全不同的東西,忌混淆。

 

 

 代碼如下 複製代碼
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.contrib.utils.join.DataJoinMapperBase;
import org.apache.hadoop.contrib.utils.join.DataJoinReducerBase;
import org.apache.hadoop.contrib.utils.join.TaggedMapOutput;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.mapred.FileInputFormat;
import org.apache.hadoop.mapred.FileOutputFormat;
import org.apache.hadoop.mapred.JobClient;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.TextInputFormat;
import org.apache.hadoop.mapred.TextOutputFormat;


public class DataJoin {
    public static class DataJoinMapper extends DataJoinMapperBase {
        public Text generateInputTag(String inputFiles) {
            return new Text(inputFiles); 
        }
        public Text generateGroupKey(TaggedMapOutput aRecord) {
            return new Text(((Text)aRecord.getData()).toString().split(",")[0]);
        }
        public TaggedMapOutput generateTaggedMapOutput(Object value) {
            TaggedWritable ret = new TaggedWritable((Text)value); 
            ret.setTag(this.inputTag);
            return ret;
        }
    }
    public static class TaggedWritable extends TaggedMapOutput {
        private Writable data;
        public TaggedWritable() {
            this.tag = new Text("");
            this.data = new Text("");
        }
       
        public TaggedWritable(Writable data) {
            this.tag = new Text("");
            this.data = data;
        }
       
        public void write(DataOutput out) throws IOException {
            this.tag.write(out);
            this.data.write(out);
        }

        public void readFields(DataInput in) throws IOException {
            this.data.readFields(in);
            this.tag.readFields(in);
        }

        public Writable getData() {
            return data;
        }
        public void setData(Writable data){
            this.data=data;
        }
    }
   
    public static class DataJoinReducer extends DataJoinReducerBase {

        @Override
        public TaggedMapOutput combine(Object[] tags, Object[] values) {
            if (tags.length < 2) {
                return null;
            }
            StringBuffer joinedStr = new StringBuffer("");
            for (int i = 0; i < values.length; i++) {
                TaggedWritable tw=(TaggedWritable)values[i];
                String str=((Text)tw.getData()).toString();   
                if(i==0)
                    joinedStr.append(str);
                else
                    joinedStr.append(str.split(",",2)[1]);
                if(i<values.length-1)
                    joinedStr.append(",");
            }
            TaggedWritable ret = new TaggedWritable(new Text(joinedStr.toString()));
            ret.setTag((Text)tags[0]);
            return ret;
        }
    }
   
    public static void main(String[] args) throws Exception {
       
            Configuration conf = new Configuration();
            JobConf job = new JobConf(conf);
            job.setJarByClass(DataJoin.class);
       
            Path in = new Path(args[0]);
            FileInputFormat.addInputPath(job, in);
           
            Path out = new Path(args[1]);
            FileOutputFormat.setOutputPath(job, out);
           
            job.setMapperClass(DataJoinMapper.class);
            job.setReducerClass(DataJoinReducer.class);
           
            job.setInputFormat(TextInputFormat.class);
            job.setOutputFormat(TextOutputFormat.class);
           
            job.setOutputKeyClass(Text.class);
            job.setOutputValueClass(TaggedWritable.class);
            //設定輸出文本中key與value之間的符號,預設為定位字元Tab
            job.set("mapred.textoutputformat.separator","=");
           
            JobClient.runJob(job);
        }   
}
 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.