Hadoop多目錄輸入,join,進入reduce,資料流分析

來源:互聯網
上載者:User

標籤:hadoop   多目錄輸入   資料流   

前言

在做需求時,經常遇到多個目錄,也就是多個維度進行join,這裡分析一下,資料是怎麼流動的。

1、多目錄輸入

使用MultipleInputs.addInputPath()  對多目錄製定格式和map

2、資料流分析

map按行讀入資料,需要對不同的輸入目錄,打上不同的標記(這個方法又叫reduce端串連),map在輸出後會進行partition和sort,按照key進行排序,然後輸出到reduce進行處理。


例子三個輸入檔案:a.txt:
500501

b.txt:
500501600 505

c.txt:
501500700 800




代碼
import java.io.IOException;import java.util.Iterator;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.Partitioner;import org.apache.hadoop.mapreduce.Reducer;import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;import org.apache.hadoop.util.Tool;import org.apache.hadoop.util.ToolRunner;import util.TextPair;import com.sina.hadoop.MultipleInputs;public class Main extends Configured implements Tool{    public static void main(String[] args) throws Exception    {        int exitcode = ToolRunner.run(new Main(), args);        System.exit(exitcode);    }    /**     * 分區     */    static class TextPairKeyPartitioner extends Partitioner<TextPair, Text>    {        public int getPartition(TextPair key, Text value, int numPartitions)        {            return (key.getFirst().hashCode() & Integer.MAX_VALUE) % numPartitions;        }    }    public int run(String[] arg0) throws Exception    {        int exitcode = 0;        if (exitcode == 0)        {            Job job1 = new Job();            job1.setJobName("testMultipleInputs");            job1.setJarByClass(Main.class);            MultipleInputs.addInputPath(job1, new Path("xx/testMultipleInputs/input/a/"),                    TextInputFormat.class, AMapper.class);            MultipleInputs.addInputPath(job1, new Path("xx/testMultipleInputs/input/b/"),                    TextInputFormat.class, BMapper.class);            MultipleInputs.addInputPath(job1, new Path("xx/testMultipleInputs/input/c/"),                    TextInputFormat.class, CMapper.class);            job1.setReducerClass(TestReducer.class);            FileOutputFormat.setOutputPath(job1, new Path("xx/testMultipleInputs/output/"));            job1.setOutputKeyClass(Text.class);            job1.setOutputValueClass(Text.class);            job1.setPartitionerClass(TextPairKeyPartitioner.class);            job1.setGroupingComparatorClass(TextPair.FirstComparator.class);            job1.setMapOutputKeyClass(TextPair.class);            job1.setMapOutputValueClass(Text.class);            job1.setNumReduceTasks(1);            exitcode = job1.waitForCompletion(true) ? 0 : 1;        }        return exitcode;    }    public class AMapper extends Mapper<LongWritable, Text, TextPair, Text>    {        public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException        {            String[] data = value.toString().split("\t", -1);            String id = "";            if (data.length >= 1)            {                id = data[0];                if (!"".equals(id))                {                    context.write(new TextPair(id, "1"), new Text("0"));                }            }        }    }    public class BMapper extends Mapper<LongWritable, Text, TextPair, Text>    {        public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException        {            String[] data = value.toString().split("\t", -1);            String id1 = "";            String id2 = "";            if (data.length >= 2)            {                id1 = data[0];                id2 = data[1];                if (!"".equals(id1))                {                    context.write(new TextPair(id1, "2"), new Text(id2));                }            }        }    }    public class CMapper extends Mapper<LongWritable, Text, TextPair, Text>    {        public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException        {            String[] data = value.toString().split("\t", -1);            String id1 = "";            String id2 = "";            if (data.length >= 2)            {                id1 = data[0];                id2 = data[1];                if (!"".equals(id1))                {                    context.write(new TextPair(id1, "3"), new Text(id2));                }            }        }    }    public class TestReducer extends Reducer<TextPair, Text, Text, Text>    {        public void reduce(TextPair key, Iterable<Text> values, Context context) throws IOException, InterruptedException        {            String data = "";            Iterator<Text> i = values.iterator();            while (i.hasNext())            {                data = i.next().toString();                context.write(key.getFirst(), new Text(data));            }        }    }}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.