`
lanhuidong
  • 浏览: 223905 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

HelloMapReduce

阅读更多

开发环境:eclipse-jee-indigo-SR2-linux-gtk + hadoop-0.20.205.0

1.安装eclipse,直接解压eclipse-jee-indigo-SR2-linux-gtk.tar.gz即可,如果启动时提示找不到jre或者jdk,可以进入/eclipse目录,执行如下命令:

mkdir jre

cd jre

ln -s $JAVA_HOME/bin

 

2.安装hadoop的eclipse插件,在/hadoop-0.20.205.0/contrib/eclipse-plugin/目录下将jar文件复制到/eclipse/plugins目录下,然后重启eclipse即可

 

3.配置Hadoop安装目录:

 

4.创建Hadoop项目

File->New->Other,找到Map/Reduce Project,点击next,如何项目名称,比如HelloMapReduce,然后点击Finish,项目创建完成



 

5.编写java文件

   该程序从data.txt文件中找出每年的最大值,data.txt中的每行表示年份和对应的值

 

package com.nexusy.hadoop;

import java.io.IOException;

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.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class HelloMapReduce {
	
	static class MyMapper extends Mapper<LongWritable, Text, Text, IntWritable>{

		@Override
		protected void map(LongWritable key, Text value, Context context)
				throws IOException, InterruptedException {
			String line = value.toString();
			String year = line.substring(0, 4);
			String num = line.substring(5);
			context.write(new Text(year), new IntWritable(Integer.parseInt(num)));
		}
		
	}
	
	static class MyReducer extends Reducer<Text, IntWritable, Text, IntWritable>{

		@Override
		protected void reduce(Text key, Iterable<IntWritable> values, Context context)
				throws IOException, InterruptedException {
			int maxValue = Integer.MIN_VALUE;
			for(IntWritable value : values){
				maxValue = Math.max(maxValue, value.get());
			}
			context.write(key, new IntWritable(maxValue));
		}
		
	}
	
	public static void main(String[] args) throws Exception{
		Job job = new Job();
		job.setJarByClass(HelloMapReduce.class);
		
		FileInputFormat.addInputPath(job, new Path("data.txt"));
		FileOutputFormat.setOutputPath(job, new Path("output"));
		
		job.setMapperClass(MyMapper.class);
		job.setReducerClass(MyReducer.class);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(IntWritable.class);
		
		System.exit(job.waitForCompletion(true) ? 0 : 1);
	}

}

 

6.在项目根目录下放一个data.txt文件,内容如下

2011 33
2011 600
2011 77
2011 33
2011 665
2011 123
2012 187
2012 25
2012 753
2012 134
2012 234
2012 575
2012 332
2012 100

 

7.运行第5步创建的.java文件

右键选择Run As->Run On Hadoop,然后选择Define a new hadoop server location,并点击next,输入Location Name,然后点击Finish即可。

 

8.控制台输出如下

12/03/04 16:46:30 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
12/03/04 16:46:30 WARN mapred.JobClient: Use GenericOptionsParser for parsing the arguments. Applications should implement Tool for the same.
12/03/04 16:46:30 INFO input.FileInputFormat: Total input paths to process : 1
12/03/04 16:46:30 INFO mapred.JobClient: Running job: job_local_0001
12/03/04 16:46:31 INFO util.ProcessTree: setsid exited with exit code 0
12/03/04 16:46:31 INFO mapred.Task:  Using ResourceCalculatorPlugin : org.apache.hadoop.util.LinuxResourceCalculatorPlugin@138c63
12/03/04 16:46:31 INFO mapred.MapTask: io.sort.mb = 100
12/03/04 16:46:31 INFO mapred.JobClient:  map 0% reduce 0%
12/03/04 16:46:36 INFO mapred.MapTask: data buffer = 79691776/99614720
12/03/04 16:46:36 INFO mapred.MapTask: record buffer = 262144/327680
12/03/04 16:46:36 INFO mapred.MapTask: Starting flush of map output
12/03/04 16:46:36 INFO mapred.MapTask: Finished spill 0
12/03/04 16:46:36 INFO mapred.Task: Task:attempt_local_0001_m_000000_0 is done. And is in the process of commiting
12/03/04 16:46:39 INFO mapred.LocalJobRunner: 
12/03/04 16:46:39 INFO mapred.Task: Task 'attempt_local_0001_m_000000_0' done.
12/03/04 16:46:39 INFO mapred.JobClient:  map 100% reduce 0%
12/03/04 16:46:39 INFO mapred.Task:  Using ResourceCalculatorPlugin : org.apache.hadoop.util.LinuxResourceCalculatorPlugin@1cef4f7
12/03/04 16:46:39 INFO mapred.LocalJobRunner: 
12/03/04 16:46:39 INFO mapred.Merger: Merging 1 sorted segments
12/03/04 16:46:39 INFO mapred.Merger: Down to the last merge-pass, with 1 segments left of total size: 156 bytes
12/03/04 16:46:39 INFO mapred.LocalJobRunner: 
12/03/04 16:46:39 INFO mapred.Task: Task:attempt_local_0001_r_000000_0 is done. And is in the process of commiting
12/03/04 16:46:39 INFO mapred.LocalJobRunner: 
12/03/04 16:46:39 INFO mapred.Task: Task attempt_local_0001_r_000000_0 is allowed to commit now
12/03/04 16:46:39 INFO output.FileOutputCommitter: Saved output of task 'attempt_local_0001_r_000000_0' to output
12/03/04 16:46:42 INFO mapred.LocalJobRunner: reduce > reduce
12/03/04 16:46:42 INFO mapred.Task: Task 'attempt_local_0001_r_000000_0' done.
12/03/04 16:46:43 INFO mapred.JobClient:  map 100% reduce 100%
12/03/04 16:46:43 INFO mapred.JobClient: Job complete: job_local_0001
12/03/04 16:46:43 INFO mapred.JobClient: Counters: 20
12/03/04 16:46:43 INFO mapred.JobClient:   File Output Format Counters 
12/03/04 16:46:43 INFO mapred.JobClient:     Bytes Written=30
12/03/04 16:46:43 INFO mapred.JobClient:   FileSystemCounters
12/03/04 16:46:43 INFO mapred.JobClient:     FILE_BYTES_READ=11442
12/03/04 16:46:43 INFO mapred.JobClient:     FILE_BYTES_WRITTEN=78488
12/03/04 16:46:43 INFO mapred.JobClient:   File Input Format Counters 
12/03/04 16:46:43 INFO mapred.JobClient:     Bytes Read=121
12/03/04 16:46:43 INFO mapred.JobClient:   Map-Reduce Framework
12/03/04 16:46:43 INFO mapred.JobClient:     Map output materialized bytes=160
12/03/04 16:46:43 INFO mapred.JobClient:     Map input records=14
12/03/04 16:46:43 INFO mapred.JobClient:     Reduce shuffle bytes=0
12/03/04 16:46:43 INFO mapred.JobClient:     Spilled Records=28
12/03/04 16:46:43 INFO mapred.JobClient:     Map output bytes=126
12/03/04 16:46:43 INFO mapred.JobClient:     Total committed heap usage (bytes)=279183360
12/03/04 16:46:43 INFO mapred.JobClient:     CPU time spent (ms)=0
12/03/04 16:46:43 INFO mapred.JobClient:     SPLIT_RAW_BYTES=113
12/03/04 16:46:43 INFO mapred.JobClient:     Combine input records=0
12/03/04 16:46:43 INFO mapred.JobClient:     Reduce input records=14
12/03/04 16:46:43 INFO mapred.JobClient:     Reduce input groups=2
12/03/04 16:46:43 INFO mapred.JobClient:     Combine output records=0
12/03/04 16:46:43 INFO mapred.JobClient:     Physical memory (bytes) snapshot=0
12/03/04 16:46:43 INFO mapred.JobClient:     Reduce output records=2
12/03/04 16:46:43 INFO mapred.JobClient:     Virtual memory (bytes) snapshot=0
12/03/04 16:46:43 INFO mapred.JobClient:     Map output records=14

 

9.最后在项目根目录下生成output目录及两个文件_SUCCESS和part-r-00000,如下图所示
   

 

PS:

1.如果JVM的内存太小将不能运行mapreduce任务,本人开始时虚拟机只有512MB内存,启动hadoop和eclipse后只剩几十MB内存,于是报OutOfMemeryError错误,将虚拟机内存调整为2GB后正常运行。

2.伪分布模式下启动Hadoop时,要先执行hadoop namenode -format,然后才是start-dfs.sh,start-mapred.sh
 
 

  • 大小: 7.5 KB
  • 大小: 24 KB
  • 大小: 40.6 KB
  • 大小: 52.1 KB
分享到:
评论

相关推荐

    MapReduce版的HelloWorld

    一个MapReduce版的单词计数程序,相当于MapReduce的HelloWorld,用装有Maven插件的Eclipse打开

    hadoop mapreduce helloworld 能调试

    hadoop mapreduce helloworld 能调试 详细内容请看:http://blog.csdn.net/wild46cat/article/details/53641765

    实验项目 MapReduce 编程

    4 分别在自编 MapReduce 程序 WordCount 运行过程中和运行结束后查看 MapReduce Web 界面。 5. 分别在自编 MapReduce 程序 WordCount 运行过程中和运行结束后练习 MapReduce Shell 常用命令。 。。

    MapReduce_mapReduce_

    MapReduce--1--入门程序WordCountMapReduce界的helloworld程序就是WordCount程序。所谓WordCount,就是单词计数,就是用来统计一篇或者一堆文本文件中的各单词的出现次数。

    集群MapReduce的包.rar

    单词计数是最简单也是最能体现MapReduce思想的程序之一,可以成为MapReduce版的“Hello World”.单词计数主要完成功能是:统计一系列文本文件中每个单词出现的次数。

    MapReduce实例分析:单词计数

    单词计数是最简单也是最能体现 MapReduce 思想的程序之一,可以称为 MapReduce 版“Hello World”。单词计数的主要功能是统计一系列文本文件中每个单词出现的次数。本节通过单词计数实例来阐述采用 MapReduce 解决...

    MapReduce实现二度好友推荐算法

    hadoop之MapReduce实现二度好友算法,包含输入数据demo,完整运算代码,在windows10下成功运行,输出结果为cat hello:2,hadoop:2,mr:1,world:1类似。

    hadoop-mapreduce:hadoop MapReduce

    hadoop jar /Users/hello/Desktop/accessMR.jar accessMR.AccessMR /logs/access.log /user/output检查输出。 hadoop fs -cat /user/output/part-00000运行hadoop命令的快捷方式: 编辑〜/ .profile并设置h

    JavaHolaMundo:Java MapReduce

    Java Hello世界 java MapReduce嗨,我是Deyvy

    mapreduce-wordcount

    MapReduce的简单HelloWorld scala项目-字数统计 Hadoop版本:3.2.1 怎么跑 组装项目 $ sbt clean assembly 在Hadoop上运行 $ hadoop jar $JAR $INPUT $OUTPUT

    WordCount详解

    一般我们学习一门程序设计语言,最开始上手的程序都是“HelloWorld”, 可以说 WordCount 就是学习掌握 Hadoop MapReduce 编程的“Hello World”。 WordCount的功能是统计输人文件(也可以是输入文件夹内的多个文件...

    大数据学习(八):mapreduce编程案例-倒排索引创建

    hello tom hello jim hello kitty hello rose b.txt hello jerry hello jim hello kitty hello jack c.txt hello jerry hello java hello c++ hello c++ 需要输出如下格式: c++ c.txt--&gt;2 hello a.txt--&gt;4 ...

    Mahout in Action

    Introducing MapReduce 98 ■ Translating to MapReduce: generating user vectors 99 ■ Translating to MapReduce: calculating co-occurrence 100 ■ Translating to MapReduce: rethinking matrix ...

    Professional NoSQL 英文版 Shashank.Tiwari

    CHAPTER 2: HELLO NOSQL: GETTING INITIAL HANDS-ON EXPERIENCE 21 CHAPTER 3: INTERFACING AND INTERACTING WITH NOSQL 43 PART II: LEARNING THE NOSQL BASICS CHAPTER 4: UNDERSTANDING THE STORAGE ...

    java大数据作业_1云计算、大数据、hadoop

    4.在用户目录下的test目录下,创建一个文件hello.txt,文件内容是包含usr的运行进程列表,然后将目录的所有文件发送到主机名为test的服务器的/opt目录上,并使用soft01用户接收这些文件 5.简述3种加密方式的概念意义...

    大数据技术原理与应用.docx

    12. 12单选(2分)在设计词频统计的MapReduce程序时,对于文本行"hello bigdata hello hadoop",经过map函数处理后直接输出的结果应该是(没有发生combine和merge操作)() [单选题] * 大数据技术原理与应用全文共10...

    Hadoop-Programs:Hadoop 程序

    MapReduce 程序需要 jared,而 HBase 程序不需要 jared。 Hadoop 程序应该运行在 Hadoop 的主目录中,而 HBase 程序可以在任何目录中运行。 减速器中的 Hadoop 平均值: 该程序演示了减速器中的一个功能。 ...

    yarn-starter:编写分布式容错YARN应用程序的入门示例

    (又名NextGen MapReduce)对于构建容错的分布式应用程序非常有用。 但是编写,甚至可能成为。 好消息是,简化了与YARN交互的框架应运而生,并加入了Apache基金会: 。 虽然仍处于孵化阶段,但该项目看起来确实很...

    09-Hadoop编程

    Hadoop编程 Hadoop思维导图下载链接 ...do echo “hello sxt $i” &gt;&gt; test.txt;done hdfs dfs -mkdir -p /user/root hdfs dfs -ls -R / hdfs dfs -D dfs.blocksize=1048576 -put ./test.txt /user/root 命令 hadoop jar

Global site tag (gtag.js) - Google Analytics