MapReduce Single-Table Association learning ~

Source: Internet
Author: User

The first consideration is the self-connection of the table, followed by the setting of the column, and finally the result collation.

File contents:

Importorg.apache.hadoop.conf.Configuration;Importorg.apache.hadoop.conf.Configured;ImportOrg.apache.hadoop.fs.Path;Importorg.apache.hadoop.io.IntWritable;ImportOrg.apache.hadoop.io.Text;ImportOrg.apache.hadoop.mapreduce.Job;ImportOrg.apache.hadoop.mapreduce.Mapper;ImportOrg.apache.hadoop.mapreduce.Reducer;ImportOrg.apache.hadoop.mapreduce.lib.input.FileInputFormat;ImportOrg.apache.hadoop.mapreduce.lib.input.TextInputFormat;ImportOrg.apache.hadoop.mapreduce.lib.output.FileOutputFormat;ImportOrg.apache.hadoop.util.Tool;ImportOrg.apache.hadoop.util.ToolRunner;Importjava.io.IOException;ImportJava.util.Iterator;Importjava.util.Objects; Public classStjoinextendsConfiguredImplementsTool { Public Static intTime = 0; Map splits the input into child and parent, and then the positive sequence output is once as the right table, and the reverse output is once as the left table
It is important to note that the left and right table difference flags must be added to the output value
Public Static classMapextendsMapper<object,text,text,text>{ Public voidMap (Object key,text value,context Context)throwsIOException, interruptedexception{String childname=NewString (); String ParentName=NewString (); String Relationtype=NewString (); String Line=value.tostring (); inti = 0;
Files are separated by spaces while(Line.charat (i)! = ") {i++; }
Split child and parent string[] values= {line.substring (0,i), line.substring (i+1)}; if(Values[0].compareto ("child")! = 0) {ChildName= Values[0]; ParentName= Values[1];
Left and right table distinguishing mark Relationtype= "1"; Context.write (NewText (Values[1]),NewText (Relationtype + "+" + childname + "+" +parentname)); Relationtype= "2"; Context.write (NewText (Values[0]),NewText (Relationtype + "+" + childname + "+" +parentname)); } } } Public Static classReduceextendsReducer<text,text,text,text>{ Public voidReduce (Text key,iterable<text> values,context Context)throwsioexception,interruptedexception{
Output table Headerif(Time = = 0) {Context.write (NewText ("grandchild"),NewText ("Grandparent")); time++; } intGrandchildnum = 0; String grandchild[]=NewString[10]; intGrandparentnum = 0; String grandparent[]=NewString[10]; Iterator ite=Values.iterator (); while(Ite.hasnext ()) {String record=Ite.next (). toString (); intLen =record.length (); inti = 2; if(len = = 0){ Continue; } CharRelationtype = Record.charat (0); String ChildName=NewString (); String ParentName=NewString (); while(Record.charat (i)! = ' + ') {ChildName= ChildName +Record.charat (i); I++; } I= I+1; while(i<Len) {ParentName= ParentName +Record.charat (i); I++; } if(Relationtype = = ' 1 ') {Grandchild[grandchildnum]=ChildName; ; Grandchildnum++; }Else{Grandparent[grandparentnum]=ParentName; Grandparentnum++; } } if(Grandparentnum! = 0 && Grandchildnum! = 0){ for(intm = 0;m<grandchildnum;m++){ for(intn = 0;n<grandparentnum;n++) {System.out.println (Grandchild[m]+ " " +Grandparent[n]); Context.write (NewText (Grandchild[m]),NewText (Grandparent[n])); } } } } } Public intRun (string[] args)throwsexception{Configuration AAA=NewConfiguration (); Job Job=job.getinstance (AAA); String inputpaths= "/usr/local/idea-ic-139.1117.1/hadoop/out/datainput/child-parent.txt"; String OutputPath= "/usr/local/idea-ic-139.1117.1/hadoop/out/dataout/"; Job.setjarbyclass (Sort.class); Job.setjobname ("Sort"); Job.setmapperclass (Map.class); Job.setreducerclass (Reduce.class); Fileinputformat.setinputpaths (Job,NewPath (inputpaths)); Fileoutputformat.setoutputpath (Job,NewPath (OutputPath)); Job.setoutputkeyclass (Text.class); Job.setoutputvalueclass (Text.class); Job.setinputformatclass (Textinputformat.class); Job.setoutputformatclass (Org.apache.hadoop.mapreduce.lib.output.TextOutputFormat.class); BooleanSuccess = Job.waitforcompletion (true); returnSuccess? 0:1; } Public Static voidMain (string[] args)throwsexception{intret = Toolrunner.run (Newstjoin (), args); System.exit (ret); }}

Output Result:

Reference: "Hadoop combat"

MapReduce Single-Table Association learning ~

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.