MapReduce Notes Demo

Source: Internet
Author: User
Tags constructor
gets the name of the file being read during the map process
Import Org.apache.hadoop.mapreduce.lib.input.FileSplit;
Gets the file name where input split is a
String curfilename = ((filesplit) Context.getinputsplit ()). GetPath (). GetName ();
Gets the file path
String Curfilepath = ((filesplit) Context.getinputsplit ()). GetPath (). toString ();

Individual has a small question whether the call is placed in the map method or in the Setup method.

If you put it in the Setup method, you can avoid retrieving the file name once per map method, but if you get the file name in the Setup method, you will not get the wrong filename when you use Combinefilelnputformat. Hope to have the great God answer
Setting the counter during Map/reduce

Context.getcounter ("GroupName", "FieldName"). Increment (1);

reduce multiple directories/custom file name Output

Package Com.demo;

Import java.io.IOException;
Import org.apache.hadoop.io.LongWritable;
Import Org.apache.hadoop.io.Text;
Import Org.apache.hadoop.mapreduce.Reducer;

Import org.apache.hadoop.mapreduce.lib.output.MultipleOutputs; /** * @author chichuduxing * @date March 31, 2017 pm 1:58:33 */public class Myreduce extends Reducer<text, text, text,

	text> {private multipleoutputs<text, text> _mos; @Override protected void Setup (context context) throws IOException, interruptedexception {_mos = new MULTIPLEOUTPUTS&L T

	Text, text> (context);

	} Long _count = 0l; @Override protected void reduce (Text key, iterable<text> values, context context) throws IOException, Interruptedex ception {//Multipleoutputs.addnamedoutput (Job, "result", Textoutputformat.class,text.class,text.class) when defining the job
		);
		Multipleoutputs.addnamedoutput (Job, "Count", Textoutputformat.class,text.class,longwritable.class);
		_count = 0;
			for (Text value:values) {_count++; COntext.write (key, value);
			Default output, file name: part-r-00000 _mos.write ("Result", key, values);
			Output File name example: result-r-00000 _mos.write ("Result", key, value, key + "/");
		Sample output file name:-r-00000//files will be sorted by key and placed in the corresponding folder with key name} _mos.write ("Count", Key, New Longwritable (_count)); 
		Output File name example: count-m-0000} @Override protected void Cleanup (context context) throws IOException, Interruptedexception {
	Be sure to remember to close _mos.close ();
 }
}



multiple MapReduce setting dependencies

Package Com.demo;

Import java.io.IOException;
Import org.apache.hadoop.conf.Configuration;
Import Org.apache.hadoop.mapreduce.Job;
Import Org.apache.hadoop.mapreduce.lib.jobcontrol.ControlledJob;

Import Org.apache.hadoop.mapreduce.lib.jobcontrol.JobControl; /** * @author chichuduxing * @date March 31, 2017 pm 1:39:18 * * public class Mymain {public static void main (string[] Ar
		GS) throws IOException {Configuration conf = new configuration ();
		Job job1 = job.getinstance (conf, "JOB1");

		Job job2 = job.getinstance (conf, "JOB2");
		The specific job setting is slightly controlledjob controlledJob1 = new Controlledjob (Job1.getconfiguration ());

		Controlledjob1.setjob (JOB1);
		Controlledjob controlledJob2 = new Controlledjob (Job2.getconfiguration ());

		Controlledjob2.setjob (JOB2);

		Set dependent Controlledjob2.adddependingjob (CONTROLLEDJOB1);
		Master Controller Jobcontrol JC = new Jobcontrol ("Jobcontrol");
		Jc.addjob (CONTROLLEDJOB1);

		Jc.addjob (CONTROLLEDJOB2);
		Thread jcthread = new Thread (JC); Jcthread.sTart ();
				while (true) {if (jc.allfinished ()) {System.out.println (Jc.getsuccessfuljoblist ());
				Jc.stop ();
			Return
				} if (Jc.getfailedjoblist (). Size () > 0) {System.out.println (Jc.getfailedjoblist ());
				Jc.stop ();
			Return
 }
		}
	}
}


MapReduce Custom type do key/valueIf you do value, just implement the writable interface, but if you do key, you need to implement Writablecomparable interface if you want to add a custom constructor for a custom writable class, be sure to keep the default empty constructor
If you use Textoutputformat to serialize instances of the custom writable type. To ensure that the writable data type used for customization has a meaningful ToString () implementation
The Hadoop lesson reuses an instance of the writable class when reading the input data. When you populate a field in the Readfileds () method, you should not rely on the existing state of the object

Package Com.bean;
Import Java.io.DataInput;
Import Java.io.DataOutput;

Import java.io.IOException;

Import org.apache.hadoop.io.WritableComparable; /** * @author chichuduxing * @date March 31, 2017 pm 3:20:39 * * Public class MyValue implements Writablecomparable<myval
	ue> {public String name;
	public int counter;

	public long timestamp;
		Public myvalue () {} public myvalue (String name, int counter, long timestamp) {this.name = name;
		This.counter = counter;
	This.timestamp = timestamp;
		} @Override public void ReadFields (Datainput in) throws IOException {name = In.readutf ();
		Counter = In.readint ();
	timestamp = In.readlong ();
		} @Override public void write (DataOutput out) throws IOException {Out.writeutf (name);
		Out.writeint (counter);
	Out.writelong (timestamp);
		} @Override public int compareTo (myvalue comparevalue) {if (this = = CompareValue) {return 0;
		} if (null = = THIS.name && null! = comparevalue.name) {return-1; } ElSe if (this.name = = Comparevalue.name | | this.name.equals (comparevalue.name)) {if (This.counter = = Comparevalue.counte
				R) {if (This.timestamp = = comparevalue.timestamp) {return 0;
				} else {return this.timestamp > Comparevalue.timestamp? 1:-1;
			}} else {return this.counter > comparevalue.counter? 1:-1;
		}} else if (null = = Comparevalue.name) {return 1;
		} else {return this.name.compareTo (comparevalue.name); }} @Override public String toString () {return new StringBuilder (). Append (name). append (' \ t '). Append (counter). Append
	(' \ t '). Append (timestamp). append (' \ t '). toString ();
 }

}







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.