Sample Join Analysis

Source: Internet
Author: User

Sample Data:student.txt

1,yaoshuya,25
2,yaoxiaohua,29
3,yaoyuanyie,15
4,yaoshupei,26

Sample Data:score.txt

1,yuwen,100
1,shuxue,99
2,yuwen,99
2,shuxue,88
3,yuwen,99
3,shuxue,56
4,yuwen,33
4,shuxue,99

Output file Contents:

1 [yaoshuya,25,yuwen,100]
1 [yaoshuya,25,shuxue,99]
2 [yaoxiaohua,29,yuwen,99]
2 [yaoxiaohua,29,shuxue,88]
3 [yaoyuanyie,15,yuwen,99]
3 [yaoyuanyie,15,shuxue,56]
4 [yaoshupei,26,yuwen,33]
4 [yaoshupei,26,shuxue,99]

Parameters:

Args= "-dio.sort.mb=10

-R 1

-informat Org.apache.hadoop.mapred.KeyValueTextInputFormat

-outformat Org.apache.hadoop.mapred.TextOutputFormat

-outkey Org.apache.hadoop.io.Text

-outvalue org.apache.hadoop.mapred.join.TupleWritable

Hdfs://namenode:9000/user/hadoop/student/student.txt

Hdfs://namenode:9000/user/hadoop/student/score2.txt

Hdfs://namenode:9000/user/hadoop/joinout ". Split (" ");

It is important to note that the output format I use is Textoutputformat (it is purely for easy observation of the output data)

The output of the valuetype is org.apache.hadoop.mapred.join.TupleWritable, which is very convenient, similar to the array type, and can accept multiple values.

The code that is added to the source is the keyvalue delimiter that is used to configure my data source file, (comma).

Jobconf.set ("Key.value.separator.in.input.line", ",");

Key Code Brief analysis:

Job.setinputformatclass (Compositeinputformat.class);
Job.getconfiguration (). Set (compositeinputformat.join_expr,
Compositeinputformat.compose (OP, Inputformatclass,
Plist.toarray (new path[0]));

Use Compositeinputformat to perform join operations. Description of this class:

/**
* An inputformat capable of performing joins over a set of data sources sorted
* and partitioned the same.
*
* A user may define new join types by setting
* <tt>mapreduce.join.define.&lt;ident&gt;</tt> to a classname.
* in the expression <tt>mapreduce.join.expr</tt>, the identifier'll be
* Assumed to be a composablerecordreader.
* <tt>mapreduce.join.keycomparator</tt> can is a classname used to compare
* Keys in the join.
* @see #setFormat
* @see Joinrecordreader
* @see Multifilterrecordreader
*/

Use OP to specify the connection type: INNER,OUTER,TBL, etc., there are other needs can also be achieved.

How exactly is it connected? A merge connection is made according to the key of two source entering mapper. Therefore, the data source is required to be ordered according to the key value. This connection is implemented on the map side.

I used Keyvaluetextinputformat to handle the test and the default format was Key\tvalue, so I used the code above to reset the format. But if your file is not a key in the first place, you need to write your own fileinputformat.

But it's obvious that the data source you're dealing with is going to be read using the same fileinputformat.

Also, there is a support for multi-file connections, in the example I only use two sample files, you can add more files, the path is added before the OutputDir.

Sample Join Analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.