第一个map reduce程序
完成了第一个mapReduce例子,记录一下。
实验环境:
hadoop在三台ubuntu机器上部署
开发在window7上进行
hadoop版本2.2.0
下载了hadoop-eclipse-plugin-2.2.0.jar放入eclipse的plugin文件夹中,重启后有如下标识

下方右击: add hadoop location

此时,eclipse 左侧会有

上图即简单的实现了一个嵌于eclipse中的用于访问hdfs系统的client端,其中可以增删改查文件。
-------------------------------
下面研究一下编程吧...
1 新建一个map/Reduce Project,本例中由于eclpise与hadoop不在一台主机上,着实费了一些时间
如下图,此时应该选择第二项(specify hadoop library locaiton), 导入的应该是hadoop目录下的lib/native文件夹,这里如果选错了,project是建不起来的!

2. 取出\hadoop-2.2.0\share\hadoop\mapreduce\sources\hadoop-mapreduce-examples-2.2.0-sources.jar下,解压后导入eclipse,好了,可以开始研究代码了。.
基本流程如下
(input) <k1, v1> -> map -> <k2, v2> -> combine -> <k2, v2> -> reduce -> <k3, v3> (output)
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hadoop.examples; 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; public class WordCount {
/*Mapper(14-26行)中的map方法(18-25行)通过指定的 TextInputFormat(49行)一次处理一行。然后,它通过StringTokenizer 以空格为分隔符将一行切分为若干tokens,之后,输出< <word>, 1> 形式的键值对。*/
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);
}
}
}
/*
做Combiner: 每次map运行之后,会对输出按照key进行排序,然后把输出传递给本地的combiner(按照作业的配置与Reducer一样),进行本地聚合
做Reducer中的reduce方法 仅是将每个key(本例中就是单词)出现的次数求和。
*/
public static class IntSumReducer
extends Reducer<Text,IntWritable,Text,IntWritable> {
private IntWritable result = new IntWritable(); 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 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); // combiner与reducer用的类一样,减轻reducer负担
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);
}
}
3. wordCount执行效果如下
root@kali:/data/hadoop/share/hadoop# hadoop jar ./mapreduce/hadoop-mapreduce-examples-2.2..jar wordcount /abc/start-all.sh /abd/out
// :: INFO Configuration.deprecation: session.id is deprecated. Instead, use dfs.metrics.session-id
// :: INFO jvm.JvmMetrics: Initializing JVM Metrics with processName=JobTracker, sessionId=
// :: INFO input.FileInputFormat: Total input paths to process :
// :: INFO mapreduce.JobSubmitter: number of splits:
// :: INFO Configuration.deprecation: user.name is deprecated. Instead, use mapreduce.job.user.name
// :: INFO Configuration.deprecation: mapred.jar is deprecated. Instead, use mapreduce.job.jar
// :: INFO Configuration.deprecation: mapred.output.value.class is deprecated. Instead, use mapreduce.job.output.value.class
// :: INFO Configuration.deprecation: mapreduce.combine.class is deprecated. Instead, use mapreduce.job.combine.class
// :: INFO Configuration.deprecation: mapreduce.map.class is deprecated. Instead, use mapreduce.job.map.class
http://phz50.iteye.com/blog/932373
http://www.ibm.com/developerworks/cn/java/j-javadev2-15/
http://www.cnblogs.com/flyoung2008/archive/2011/12/09/2281400.html
http://f.dataguru.cn/thread-167597-1-1.html hadoop在eclipse中上传文件size为0
http://cs.smith.edu/dftwiki/index.php/Hadoop_Tutorial_1_--_Running_WordCount step by step!
第一个map reduce程序的更多相关文章
- Hadoop学习笔记2 - 第一和第二个Map Reduce程序
转载请标注原链接http://www.cnblogs.com/xczyd/p/8608906.html 在Hdfs学习笔记1 - 使用Java API访问远程hdfs集群中,我们已经可以完成了访问hd ...
- map reduce程序示例
map reduce程序示例 package test2; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop. ...
- 使用Python实现Map Reduce程序
使用Python实现Map Reduce程序 起因 想处理一些较大的文件,单机运行效率太低,多线程也达不到要求,最终采用了集群的处理方式. 详细的讨论可以在v2ex上看一下. 步骤 MapReduce ...
- eclipse 中运行 Hadoop2.7.3 map reduce程序 出现错误(null) entry in command string: null chmod 0700
运行map reduce任务报错: (null) entry in command string: null chmod 0700 解决办法: 在https://download.csdn.net/d ...
- ODPS 下一个map / reduce 准备
阿里接到一个电话说练习和比赛智能二选一, 真的很伤心, 练习之前积极老龄化的权利. 要总结ODPS下一个 写map / reduce 并进行购买预测过程. 首先这里的hadoop输入输出都是表的形式, ...
- java 写一个 map reduce 矩阵相乘的案例
1.写一个工具类用来生成 map reduce 实验 所需 input 文件 下面两个是原始文件 matrix1.txt 1 2 -2 0 3 3 4 -3 -2 0 2 3 5 3 -1 2 -4 ...
- Hadoop 使用Combiner提高Map/Reduce程序效率
众所周知,Hadoop框架使用Mapper将数据处理成一个<key,value>键值对,再网络节点间对其进行整理(shuffle),然后使用Reducer处理数据并进行最终输出. 在上述过 ...
- Hadoop实战:使用Combiner提高Map/Reduce程序效率
好不easy算法搞定了.小数据測试也得到了非常好的结果,但是扔到进群上.挂上大数据就挂了.无休止的reduce不会结束了. .. .. .... .. ... .. ================= ...
- Map/Reduce 工作机制分析 --- 作业的执行流程
前言 从运行我们的 Map/Reduce 程序,到结果的提交,Hadoop 平台其实做了很多事情. 那么 Hadoop 平台到底做了什么事情,让 Map/Reduce 程序可以如此 "轻易& ...
随机推荐
- android 判断左右滑动,上下滑动的GestureDetector简单手势检测
直接加入监听GestureDetector放在需要判断滑动手势的地方: import android.app.Activity; import android.os.Bundle; import an ...
- hashMap put方法 第二行代码 1
public interface Zi<K,V> { //我是接口 //通过Zi<K,V> zi的方式可以让我实现多态 //多态的好处是: //1. 应用程序不必为每一个派生类 ...
- 接口、抽象类、泛型、hashMap
看到hashMap的put方法的第一行代码就懵逼了 就不继续往下看了 用简单的代码还原第一行代码 TsInter.java 接口 为什么要使用接口,比如写文章一样,我先列个大纲 //interface ...
- C艹函数与结构体
传递指针 代码: #include <iostream> #include <cmath> struct polar{ double distance; double angl ...
- python-迭代器与生成器的区别
这里涉及几个知识点:迭代器.生成器.yieId 先用个例子看一下迭代器与生成器的区别吧 #L是个list,迭代用for循环即可,L取出来是存放在内存中的,再多次去循环取出都可以>>> ...
- Python——eventlet.greenpool
该模块提供对 greenthread 池的支持. greenthread 池提供了一定数量的备用 greenthread ,有效限制了孵化 greenthread 过多导致的内存不足,当池子中没有足够 ...
- EXP无法导出空表的表结构解决办法
原文链接:http://www.cnblogs.com/Mr_JinRui/archive/2012/11/05/2755035.html 早的一次使用oracle 11g导出数据发现有的表丢失了,感 ...
- CSS :after、before、<!DOCTYPE>
<!DOCTYPE> <!DOCTYPE> 声明必须是 HTML 文档的第一行,位于 <html> 标签之前. CSS :选择器 after,before
- ICMP Ping模版实现对客户端网络状态的监控
Zabbix使用外部命令fping处理ICMP ping的请求,fping不包含在zabbix的发行版本中,需要额外去下载安装fping程序,安装完毕之后需要zabinx_server.conf中的参 ...
- php 统计fasta 序列长度和GC含量
最近php7的消息铺天盖地, 忍不住想尝试下.星期天看了下语法, 写个小脚本练下手: 这个脚本读取fasta 文件, 输出序列的长度和GC含量: <?php $fasta = "tes ...