Hadoop Programming (Khái niệm cơ bản về lập trình Hadoop MapReduce)

UN
Uni24h
0 次下载
33
0

资料内容

第 2 页

Overview • MapReduce Types • Input Formats • Output Formats • Serialization • Job • http://hadoop.apache.org/docs/r2.2.0/api/or g/apache/hadoop/mapreduce/package- summary.html

第 3 页

Mapper<KEYIN,VALUEIN,KEYOUT,VALUEOUT> • Maps input key/value pairs to a set of intermediate key/value pairs. • Maps are the individual tasks which transform input records into a intermediate records. The transformed intermediate records need not be of the same type as the input records. A given input pair may map to zero or many output pairs. • The Hadoop Map-Reduce framework spawns one map task for each InputSplit generated by the InputFormat for the job. • The framework first calls setup(org.apache.hadoop.mapreduce.Mapper.Context), followed by map(Object, Object, Context) for each key/value pair in the InputSplit. Finally cleanup(Context) is called. http://hadoop.apache.org/docs/r2.2.0/api/org/apache/hadoop/mapreduce/Mapper.ht ml

第 4 页

public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable>{ private final static IntWritable one = new IntWritable(1); private Text word = new Text(); public void map(Object key, Text value, Context context ) throws IOException, InterruptedException { StringTokenizer itr = new StringTokenizer(value.toString()); while (itr.hasMoreTokens()) { word.set(itr.nextToken()); context.write(word, one); } } }

第 5 页

What is Writable? • Hadoop defines its own “box” classes for strings (Text), integers (IntWritable), etc. • All values are instances of Writable • All keys are instances of WritableComparable

第 6 页

Writable • A serializable object which implements a simple, efficient, serialization protocol, based on DataInput and DataOutput. • Any key or value type in the Hadoop Map-Reduce framework implements this interface. • Implementations typically implement a static read(DataInput) method which constructs a new instance, calls readFields(DataInput) and returns the instance. • http://hadoop.apache.org/docs/r2.2.0/api/or g/apache/hadoop/io/Writable.html

第 8 页

public class MyWritable implements Writable { // Some data private int counter; private long timestamp; public void write(DataOutput out) throws IOException { out.writeInt(counter); out.writeLong(timestamp); } public void readFields(DataInput in) throws IOException { counter = in.readInt(); timestamp = in.readLong(); } public static MyWritable read(DataInput in) throws IOException { MyWritable w = new MyWritable(); w.readFields(in); return w; } }

第 9 页

public class MyWritableComparable implements WritableComparable { // Some data private int counter; private long timestamp; public void write(DataOutput out) throws IOException { out.writeInt(counter); out.writeLong(timestamp); } public void readFields(DataInput in) throws IOException { counter = in.readInt(); timestamp = in.readLong(); } public int compareTo(MyWritableComparable w) { int thisValue = this.value; int thatValue = ((IntWritable)o).value; return (thisValue < thatValue ? -1 : (thisValue==thatValue ? 0 : 1)); } }

第 10 页

Getting Data To The Mapper Input file Input file InputSplit InputSplit InputSplit InputSplit InputFormatRecordReader RecordReader RecordReader RecordReader Mapper Mapper Mapper Mapper (intermediates) (intermediates) (intermediates) (intermediates)

第 11 页

public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs(); if (otherArgs.length != 2) { System.err.println("Usage: wordcount <in> <out>"); System.exit(2); } Job job = new Job(conf, "word count"); job.setJarByClass(WordCount.class); job.setMapperClass(TokenizerMapper.class); job.setCombinerClass(IntSumReducer.class); job.setReducerClass(IntSumReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path(otherArgs[0])); FileOutputFormat.setOutputPath(job, new Path(otherArgs[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); }

第 12 页

Reading Data • Data sets are specified by InputFormats – Defines input data (e.g., a directory) – Identifies partitions of the data that form an InputSplit – Factory for RecordReader objects to extract (k, v) records from the input source

第 13 页

Input Format • InputFormat describes the input-specification for a Map- Reduce job • The Map-Reduce framework relies on the InputFormat of the job to: – Validate the input-specification of the job. – Split-up the input file(s) into logical InputSplits, each of which is then assigned to an individual Mapper. – Provide the RecordReader implementation to be used to glean input records from the logical InputSplit for processing by the Mapper. http://hadoop.apache.org/docs/r2.2.0/api/org/apache/hadoop/mapreduce/Inp utFormat.html

第 14 页

FileInputFormat and Friends • TextInputFormat – Treats each ‘\n’-terminated line of a file as a value • KeyValueTextInputFormat – Maps ‘\n’- terminated text lines of “k SEP v” • SequenceFileInputFormat – Binary file of (k, v) pairs (passing data between the output of one MapReduce job to the input of some other MapReduce job) • SequenceFileAsTextInputFormat – Same, but maps (k.toString(), v.toString())

第 15 页

Filtering File Inputs • FileInputFormat will read all files out of a specified directory and send them to the mapper • Delegates filtering this file list to a method subclasses may override – e.g., Create your own “xyzFileInputFormat” to read *.xyz from directory list

第 16 页

Record Readers • Each InputFormat provides its own RecordReader implementation – Provides (unused?) capability multiplexing • LineRecordReader – Reads a line from a text file • KeyValueRecordReader – Used by KeyValueTextInputFormat

第 17 页

Input Split Size • FileInputFormat will divide large files into chunks – Exact size controlled by mapred.min.split.size • RecordReaders receive file, offset, and length of chunk • Custom InputFormat implementations may override split size – e.g., “NeverChunkFile”

第 19 页

public class ObjectPositionInputFormat extends FileInputFormat<Text, Point3D> { public RecordReader<Text, Point3D> getRecordReader( InputSplit input, JobConf job, Reporter reporter) throws IOException { reporter.setStatus(input.toString()); return new ObjPosRecordReader(job, (FileSplit)input); } InputSplit[] getSplits(JobConf job, int numSplits) throuw IOException; }

第 20 页

class ObjPosRecordReader implements RecordReader<Text, Point3D> { public ObjPosRecordReader(JobConf job, FileSplit split) throws IOException {} public boolean next(Text key, Point3D value) throws IOException { // get the next line} public Text createKey() { } public Point3D createValue() { } public long getPos() throws IOException { } public void close() throws IOException { } public float getProgress() throws IOException {} }

显示前 20 页。下载资料查看完整内容。

评论 (0)

登录 以加入讨论

暂无评论。快来抢沙发吧!