知识点
相关文章
更多最近更新
更多Hadoop之MapReduce
2019-03-28 13:54|来源: 网络
在Hadoop中,MapReduce起着非常重要的数据处理作用。HDFS作为数据存储的核心部件起着管理文件资源的作用,而MapReduce作为一种编程计算模型,对数据的分析和处理任务进行调度管理。
在MapReduce中,所有的机器分成两个角色,一个叫JobTracker,另外一个叫TaskTracker。同HDFS的分布式概念一样,JobTracker是用于执行总体调度的,一般运行在NameNode机器上,而TaskTracker则负责在节点DataNode上执行具体的分布式任务的计算。海量的数据的计算任务就被许多个TaskTracker将计算量分布在每一个节点上,从而最大程度的利用资源。
更多Hadoop相关信息见Hadoop 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=13
在MapReduce的入门也有一个HelloWord的例子,源代码如下:
- package com.ant.ren.hadoop.demo;
- import java.io.IOException;
- import java.util.StringTokenizer;
- import org.apache.hadoop.conf.Configured;
- import org.apache.hadoop.fs.Path;
- import org.apache.hadoop.io.IntWritable;
- import org.apache.hadoop.io.LongWritable;
- 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.util.Tool;
- import org.apache.hadoop.util.ToolRunner;
- import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
- import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
- import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
- import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
- public class WordCount extends Configured implements Tool{
- /**
- * you can create any class extends Mapper stand for map
- * in map stage, we just split input file content by StringTokenizer default token: whitespace
- * then use word as key, show up count (1) as value
- **/
- public static class Map extends Mapper<LongWritable, Text, Text, IntWritable>{
- private final static IntWritable one = new IntWritable(1);
- private Text word = new Text();
- public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException{
- String line = value.toString();
- StringTokenizer tokenizer = new StringTokenizer(line);
- while(tokenizer.hasMoreElements()){
- word.set(tokenizer.nextToken());
- context.write(word, one);
- }
- }
- }
- /**
- * you can create any class extends Reducer stand for reduce
- * in reduce stage, we iterate value for the same key (word), calculate the sum of their values
- * then output <word, sum> as reduce result
- **/
- public static class Reduce extends Reducer<Text, IntWritable, Text, IntWritable>{
- public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException{
- int sum = 0;
- for(IntWritable val : values)
- sum += val.get();
- context.write(key, new IntWritable(sum));
- }
- }
- @Override
- public int run(String[] args) throws Exception {
- Job job = new Job(getConf());
- job.setJarByClass(WordCount.class);
- job.setJobName("Word Count Job");
- job.setOutputKeyClass(Text.class);
- job.setOutputValueClass(IntWritable.class);
- job.setMapperClass(Map.class);
- job.setReducerClass(Reduce.class);
- job.setInputFormatClass(TextInputFormat.class);
- job.setOutputFormatClass(TextOutputFormat.class);
- FileInputFormat.setInputPaths(job, new Path(args[0]));
- FileOutputFormat.setOutputPath(job, new Path(args[1]));
- boolean success = job.waitForCompletion(true);
- return success ? 0 : 1;
- }
- public static void main(String[] args) throws Exception{
- int ret = ToolRunner.run(new WordCount(), args);
- System.exit(ret);
- }
- }
相关问答
更多-
HADOOP MapReduce的tasktracker问题[2022-10-29]
首先,一个job具体启动多少个map,是由你配置的inputformat来决定的。inputformat在分配任务之前会对输入进行切片。最终启动的map数目,就是切片的结果数目。具体来看 一、如果使用是自定义的inputformat,那么启动多少个map,是由你实现的public InputSplit[] getSplits(JobConf job, int numSplits)方法决定的,返回的切片有多少个就启动多少个map任务。 二、如果是使用系统系统的TextInputFormat(或FileInpu ... -
hadoop2.x如何通过eclipse执行mapreduce[2023-04-11]
你参考下这个吧eclipse中开发Hadoop2.x的Map/Reduce项目汇总 -
它们被分离出来,因为这两个包都代表2个不同的API。 org.apache.hadoop.mapred是旧的API, org.apache.hadoop.mapreduce是新的。 这样做是为了让程序员以更方便,更简单和复杂的方式编写MapReduce作业。 您可能会发现此演示文稿很有用,其中详细讨论了不同之处。 希望这回答你的问题。 They are separated out because both of these packages represent 2 different APIs. org.a ...
-
用于hadoop mapreduce的罐子(Jars for hadoop mapreduce)[2023-07-28]
本教程提到: 下载Hadoop-core-1.2.1.jar,用于编译和执行MapReduce程序。 访问以下链接http://mvnrepository.com/artifact/org.apache.hadoop/hadoop-core/1.2.1下载jar。 所以在这里你可以找到不同版本的所有罐子 This tutorial mentions : Download Hadoop-core-1.2.1.jar, which is used to compile and execute the MapRe ... -
在Hadoop中,您处理输入拆分而不是块。 输入拆分是完整的数据集。 您希望避免一个映射器超过两个拆分的情况,因为这会降低性能并创建流量。 在文本世界中,假设您在block1中并且您有一个句子,例如“我是一个哈”,而block2继续“doop developer”,那么这会创建网络流量,因为我们始终必须在一个完整的节点上工作输入拆分和一些数据必须转移到另一个节点。 In Hadoop you work on input splits and not on blocks. An input split is ...
-
您可以将job1的输出作为输入链接到job2。 inputdir1 - > outputdir1 - > outputdir2 ... - > outputdir9 - > outputdir10 You can just chain the output of job1 as the input to job2. inputdir1 -> outputdir1 -> outputdir2 ... -> outputdir9 -> outputdir10
-
您可以将LIMIT与任务规范一起使用。 但是,如果您必须一次又一次地执行此操作,那么更好的自动化解决方案是使用OOZIE(hadoop的工作流编辑器),可以在hive中为您的数据创建分区。 You can use LIMIT with task specification. However if you have to do it again and again then a better automated solution is to use OOZIE (work flow editor for ha ...
-
mapper的输出键和值类型应该是reducer的输入类型,因此在你的情况下,reducer必须继承自 Reducer
setOutputKeyClass和setOutputValueClass设置作业输出的类型,即map和reduce。 如果要为映射器指定其他类型,则应使用方法setMapOutputKeyClass和setMapOutputValueClass 。 作为旁注,当您不希望输出中的真值时,为什么要从 ... -
Hadoop和MapReduce(Hadoop and MapReduce)[2023-06-28]
Mapper接口按以下顺序需要4个类型参数:Map输入键,Map输入值,Map输出键和Map输出值。 在您的情况下,由于您正在处理4个整数,其中3个构成您的值,1个是您的密钥,因此使用IntWritable作为Map输入键并且应该使用Text而错误。 此外,您在MapClass定义中指定的类型与传递给Map函数的类型不匹配。 鉴于您正在处理文本文件,您的MapClass应定义如下: public static class MapClass extends MapReduceBase implements M ... -
MapReduce的作用可以称为“执行引擎”。 Pig作为一个系统正在将Pig Latin命令转换为一个或多个MR Jobs。 Pig本身没有能力运行它 - 它将这项工作委托给Hadoop。 我会在编译器和操作系统之间建立类比。 OS执行时编译器创建程序。 在这个比喻中,Pig是编译器,Hadoop是OS。 猪做的更多 - 它运行作业,监视它们等等。所以除了编译器之外,它可以被视为“shell”。 在我的理解中,从以下角度看,Pig不是100%编译器 - 它不会根据命令编译MR作业。 它传递有关应该对已存在 ...