酱油HADOOP 1
来源:互联网 发布:开一家淘宝店的成本 编辑:程序博客网 时间:2024/04/28 20:27
有很多介绍Hadoop的资源,Hadoop自动的Doc文件夹中也包含很多学习文档,有中文版的。
http://www.cnblogs.com/wayne1017/archive/2007/03/18/668768.html
关于MapReduce的内容,建议看看孟岩的这篇MapReduce:The Free Lunch Is Not Over!
import java.io.IOException;import java.util.StringTokenizer;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.Path;import org.apache.hadoop.io.IntWritable;import org.apache.hadoop.io.Text;import org.apache.hadoop.mapreduce.Job;import org.apache.hadoop.mapreduce.Mapper;import org.apache.hadoop.mapreduce.Reducer;import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;import org.apache.hadoop.util.GenericOptionsParser;/** * * 描述:WordCount explains by York * @author Hadoop Dev Group */public class WordCount { /** * Map的四个参数分别是Map输入的<key, value>类型和Map的输出 <key, value>类型* 建立Mapper类TokenizerMapper继承自泛型类Mapper * Mapper类:实现了Map功能基类 * Mapper接口: * WritableComparable接口:实现WritableComparable的类可以相互比较。所有被用作key的类应该实现此接口。 * Reporter 则可用于报告整个应用的运行进度,本例中未使用。 * */ public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable>{ /** * IntWritable, Text 均是 Hadoop 中实现的用于封装 Java 数据类型的类,这些类实现了WritableComparable接口, * 都能够被串行化从而便于在分布式环境中进行数据交换,你可以将它们分别视为int,String 的替代品。 * 声明one常量和word用于存放单词的变量 */ private final static IntWritable one = new IntWritable(1); private Text word = new Text(); /** * Mapper中的map方法: * void map(K1 key, V1 value, Context context) * 映射一个单个的输入k/v对到一个中间的k/v对 * 输出对不需要和输入对是相同的类型,输入对可以映射到0个或多个输出对。 * Context:收集Mapper输出的<k,v>对。 * Context的write(k, v)方法:增加一个(k,v)对到context * 程序员主要编写Map和Reduce函数.这个Map函数使用StringTokenizer函数对字符串进行分隔,通过write方法把单词存入word中 * write方法存入(单词,1)这样的二元组到context中 */ 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);}}} public static class IntSumReducer extends Reducer<Text,IntWritable,Text,IntWritable> {private IntWritable result = new IntWritable(); /** * Reducer类中的reduce方法: * void reduce(Text key, Iterable<IntWritable> values, Context context) * 中k/v来自于map函数中的context,可能经过了进一步处理(combiner),同样通过context输出 * */ public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {int sum = 0;for (IntWritable val : values) {sum += val.get();}result.set(sum);context.write(key, result);}}public static void main(String[] args) throws Exception {/** * * Configuration:map/reduce的j配置类,向hadoop框架描述map-reduce执行的工作 * */ 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名称 job.setJarByClass(WordCount.class); job.setMapperClass(TokenizerMapper.class); //为job设置Mapper类 job.setCombinerClass(IntSumReducer.class); //为job设置Combiner类job.setReducerClass(IntSumReducer.class); //为job设置Reducer类 job.setOutputKeyClass(Text.class); //为job的输出数据设置Key类 job.setOutputValueClass(IntWritable.class); //为job输出设置value类 FileInputFormat.addInputPath(job, new Path(otherArgs[0])); //为job设置输入路径 FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));//为job设置输出路径 System.exit(job.waitForCompletion(true) ? 0 : 1); //运行job }}}
Map的结果,会通过partition分发到Reducer上,Reducer做完Reduce操作后,通过OutputFormat,进行输出,下面我们就来分析参与这个过程的类。
Mapper的结果,可能送到Combiner做合并,Combiner在系统中并没有自己的基类,而是用Reducer作为Combiner的基类,他们对外的功能是一样的,只是使用的位置和使用时的上下文不太一样而已。Mapper最终处理的键值对<key, value>,是需要送到Reducer去合并的,合并的时候,有相同key的键/值对会送到同一个Reducer那。哪个key到哪个Reducer的分配过程,是由Partitioner规定的。它只有一个方法,
getPartition(Text key, Text value, int numPartitions)
如果需要控制Map输出到那个Reduce上去执行,就需要重载getPartition,实现自己的partition。
输入是Map的结果对<key, value>和Reducer的数目,输出则是分配的Reducer(整数编号)。就是指定Mappr输出的键值对到哪一个reducer上去。系统缺省的Partitioner是HashPartitioner,它以key的Hash值对Reducer的数目取模,得到对应的Reducer。这样保证如果有相同的key值,肯定被分配到同一个reducre上。如果有N个reducer,编号就为0,1,2,3……(N-1)。
Reducer是所有用户定制Reducer类的基类,和Mapper类似,它也有setup,reduce,cleanup和run方法,其中setup和cleanup含义和Mapper相同,reduce是真正合并Mapper结果的地方,它的输入是key和这个key对应的所有value的一个迭代器,同时还包括Reducer的上下文。系统中定义了两个非常简单的Reducer,IntSumReducer和LongSumReducer,分别用于对整形/长整型的value求和。
下面是一个partition的示例。
http://blog.csdn.net/xw13106209/article/details/6912069
import java.io.IOException;import java.util.*;import org.apache.hadoop.fs.Path;import org.apache.hadoop.conf.*;import org.apache.hadoop.io.*;import org.apache.hadoop.mapred.*;import org.apache.hadoop.util.*;/** * 输入文本,以tab间隔 kaka 1 28 hua 0 26 chao 1 tao 1 22 mao 0 29 22 * */// Partitioner函数的使用public class MyPartitioner {// Map函数public static class MyMap extends MapReduceBase implementsMapper<LongWritable, Text, Text, Text> {public void map(LongWritable key, Text value,OutputCollector<Text, Text> output, Reporter reporter)throws IOException {String[] arr_value = value.toString().split("\t");// 测试输出// for(int i=0;i<arr_value.length;i++)// {// System.out.print(arr_value[i]+"\t");// }// System.out.print(arr_value.length);// System.out.println();Text word1 = new Text();Text word2 = new Text();if (arr_value.length > 3) {word1.set("long");word2.set(value);} else if (arr_value.length < 3) {word1.set("short");word2.set(value);} else {word1.set("right");word2.set(value);}output.collect(word1, word2);}}public static class MyReduce extends MapReduceBase implementsReducer<Text, Text, Text, Text> {public void reduce(Text key, Iterator<Text> values,OutputCollector<Text, Text> output, Reporter reporter)throws IOException {int sum = 0;System.out.println(key);while (values.hasNext()) {output.collect(key, new Text(values.next().getBytes()));}}}// 接口Partitioner继承JobConfigurable,所以这里有两个override方法public static class MyPartitionerPar implements Partitioner<Text, Text> {/** * getPartition()方法的 输入参数:键/值对<key,value>与reducer数量numPartitions * 输出参数:分配的Reducer编号,这里是result * */@Overridepublic int getPartition(Text key, Text value, int numPartitions) {// TODO Auto-generated method stubint result = 0;System.out.println("numPartitions--" + numPartitions);if (key.toString().equals("long")) {result = 0 % numPartitions;} else if (key.toString().equals("short")) {result = 1 % numPartitions;} else if (key.toString().equals("right")) {result = 2 % numPartitions;}System.out.println("result--" + result);return result;}@Overridepublic void configure(JobConf arg0) {// TODO Auto-generated method stub}}// 输入参数:/home/hadoop/input/PartitionerExample// /home/hadoop/output/Partitionerpublic static void main(String[] args) throws Exception {JobConf conf = new JobConf(MyPartitioner.class);conf.setJobName("MyPartitioner");// 控制reducer数量,因为要分3个区,所以这里设定了3个reducerconf.setNumReduceTasks(3);conf.setMapOutputKeyClass(Text.class);conf.setMapOutputValueClass(Text.class);// 设定分区类conf.setPartitionerClass(MyPartitionerPar.class);conf.setOutputKeyClass(Text.class);conf.setOutputValueClass(Text.class);// 设定mapper和reducer类conf.setMapperClass(MyMap.class);conf.setReducerClass(MyReduce.class);conf.setInputFormat(TextInputFormat.class);conf.setOutputFormat(TextOutputFormat.class);FileInputFormat.setInputPaths(conf, new Path(args[0]));FileOutputFormat.setOutputPath(conf, new Path(args[1]));JobClient.runJob(conf);}}
- 酱油HADOOP 1
- 酱油
- CCF 201709-1 打酱油
- 201709-1 打酱油 ccf
- 【ccf-201709-1】打酱油
- CCF 201709-1 打酱油
- ccfcsp 201709-1 打酱油
- CCF-201709-1-打酱油
- CCF2017-09-1 打酱油
- CCF201709-1 打酱油(100分)
- CCF认证 201709-1打酱油
- 【CCF201709-1打酱油】C++代码实现
- CCF CSP 201709-1 打酱油
- CCF考试——201709-1打酱油
- CCF201709-1打酱油(简单的贪心)
- 酱油路过
- 打酱油
- 打酱油
- JVM使用方法区信息例子
- Javascript获取页面、屏幕尺寸大小参数
- 验证URL是可用的 相同的地址先验证一次 做HashMap缓存
- 使用内存映射文件来提高你程序的性能
- 随手记-嵌入式linux
- 酱油HADOOP 1
- iPhone应用程序编程指南-图形和描画
- javaScript 验证 电话号码(包含 手机和座机) 和邮箱验证
- C# 泛型
- ScrollView/HorizontalScrollView常用技巧,附源码
- 如何解散modalViewController的modalViewController
- nose基础
- 只设置hidden:true时会显示出来一个:的标签(extjs生成的lable标签在IE6下会有:标签)
- Delete the System Apps by adb