一、HDFS个人理解
HDFS是个文件系统,只不过加了个分布式作为文件系统的前缀而已,大概的结构就像LINUX系统。文件大的一个电脑存储不下,怎办?那就想办法存储到不同的机子上呗,于是HDFS就应用而生了。众所周知,文件在硬盘上是以文件块的形式存储。什么句柄啊,真心不懂,只能感知,没人能明白0和1,当然也没必要。
二、Map个人理解
数学函数大家都清楚吧,f(x)=2x,这是一个简单的线性函数。
打开脑洞,将x看成文件中的一行行的字符串,x=“hello hadoop hello mapreduce ” 而此时f(x)的功能是将此字符串按照空格隔开,输出结果为:(hello 1)、(hadoop 1)、(hello 1)、(mapreduce 1)格式。
继续打开脑洞,将x看成一个大文件的一个文件块,比如1万行作为一个文件块,作为x的数值。Hadoop会为每一个文件块构建一个Map任务。
假设,某个HDFS文件有5万行数据,Hadoop将5万行数据切分成5个不同的分片,每个分片有1万行数据,再假设有5台hadoop集群,那么Hadoop会构建5个Map任务,每个Map任务处理一个分片,完全是并行处理的。
三、 Map代码大放送
1public static class WordCountMapper extends Mapper<LongWritable,Text,Text,IntWritable> { 2 private static final IntWritable ONE = new IntWritable(1); 3 Text value = new Text(); 4 @Override 5 public void map(LongWritable key, Text value, Context context) 6 throws IOException, InterruptedException { 7 StringTokenizer tokenizer = new StringTokenizer(value.toString()); 8 while (tokenizer.hasMoreTokens()) { 9 word.set(StringUtils.trim(tokenizer.nextToken().replaceAll("\\W", ""))); 10 //写到一个临时目录下了 11 context.write(word, one); 12 } 13 } 14} 15 16public static class MyMapper implements Mapper<LongWritable, Text, Text, IntWritable> { 17 private static final IntWritable ONE = new IntWritable(1); 18 private Text word = new Text(); 19 @Override 20 public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException { 21 String record = value.toString(); 22 String[] split = record.split(" "); 23 for (String one : split) { 24 word.set(one); 25 output.collect(word, ONE); 26 } 27 } 28 }
二、Shuffle阶段
Shuffle即混洗,这个代码不需要我们去写,但是这个过程是存在的 。无数的类似与 (hello 1)、(hadoop 1)、(hello 1)、(mapreduce 1)这种格式的数据存储在磁盘下,真心不知道它是如何转换成(hello [1,1])、(hadoop [1])、(mapreduce [1])这种格式的,想想都可怕,想想都觉得是艰难,难道是每次在写的时候,都去查询了一把?......待我翻书,给你解释,最好不要有什么宽依赖,窄依赖的,太怕了。
三、Reduce代码大放送
没什么好解释的,Map的输出作为Reduce的输入。
1public static class WordCountReduce extends Reducer<Text, IntWritable, Text, IntWritable> { 2 @Override 3 public void reduce(Text key, Iterable<IntWritable> values,Context context) throws IOException,InterruptedException { 4 int sum = 0; 5 for (IntWritable val : values) { 6 sum += val.get(); 7 } 8 context.write(key, new IntWritable(sum)); 9 } 10} 11public static class MyReducer implements Reducer<Text, IntWritable, Text, IntWritable> { 12 private IntWritable result = new IntWritable(); 13 @Override 14 public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException { 15 int sum = 0; 16 while (values.hasNext()) { 17 sum += values.next().get(); 18 19 } 20 result.set(sum); 21 output.collect(key, result); 22 } 23 }
四、主方法代码大放送
1public class WordCount { 2 public static void main(String[] args) throws IOException, 3 ClassNotFoundException, InterruptedException { 4 Configuration conf = new Configuration(); 5 Job job = new Job(conf); 6 job.setJobName("wordcount"); 7 //在hadoop集群上运行这个作业的时候,需要将执行的类打包成一个jar文件,然后进行运行,为什么要打成jar文件呢?因为,因为,因为这是集群、要分发代码、要知道其他机子和driver做同样的事情。 8 job.setJarByClass(WordCountMapper.class); 9 //设置MapRededuce输出格式,key和value 10 job.setOutputKeyClass(Text.class); 11 job.setOutputValueClass(IntWritable.class); 12 //设置map和reduce的类型 13 job.setMapperClass(WordCountMapper.class); 14 job.setReducerClass(WordCountReduce.class); 15 //设置输入和输出的格式 16 job.setInputFormatClass(TextInputFormat.class); 17 job.setOutputFormatClass(TextOutputFormat.class); 18 //分别输入路径和输出路径,都是绝对路径 19 FileInputFormat.setInputPaths(job, new Path("/user/hadoop/wordcount.txt")); 20 FileOutputFormat.setOutputPath(job, new Path("/user/hadoop/" + new SimpleDateFormat("mmss").format(new Date()))); 21 //提交任务 22 job.waitForCompletion(true); 23 } 24}