之前跑了一下hadoop里面自带的例子,现在顺一下如何通过源代码来运行程序。

我懒得装eclipse,就全部用命令行了。

整体参考官网上的:http://hadoop.apache.org/docs/r2.6.0/hadoop-mapreduce-client/hadoop-mapreduce-client-core/MapReduceTutorial.html#Example:_WordCount_v1.0

注意:所有的代码都放在hadoop的根文件夹下面,否则会提示找不到主类!

①写源代码,  保存为WordCount.java

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; public class WordCount { 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);
}
}
} 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();
Job job = Job.getInstance(conf, "word count");
job.setJarByClass(WordCount.class);
job.setMapperClass(TokenizerMapper.class);
job.setCombinerClass(IntSumReducer.class);
job.setReducerClass(IntSumReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}

②把源代码编译打包成jar文件

$ bin/hadoop com.sun.tools.javac.Main WordCount.java
$ jar cf wc.jar WordCount*.class

这样就生成了jar文件

③运行文件

假设已经有一些文件上传到了/user/kzy/input2/hadoop 文件夹, 已经有/user/kzy/output文件夹,那么下面的语句令输出都放在新建的wc文件夹里

bin/hadoop jar wc.jar WordCount /user/kzy/input2/hadoop /user/kzy/output/wc

【hadoop2.6.0】通过代码运行程序流程的更多相关文章

  1. 【目录】 hadoop2.6.0

    hadoop2.6.0 安装+例子运行 数据丢失问题解决 通过代码运行程序流程 利用Hadoop的 Java API 利用JAVA API 实现数据上传

  2. 编写简单的Mapreduce程序并部署在Hadoop2.2.0上运行

    今天主要来说说怎么在Hadoop2.2.0分布式上面运行写好的 Mapreduce 程序. 可以在eclipse写好程序,export或用fatjar打包成jar文件. 先给出这个程序所依赖的Mave ...

  3. 使用命令行编译打包运行自己的MapReduce程序 Hadoop2.6.0

    使用命令行编译打包运行自己的MapReduce程序 Hadoop2.6.0 网上的 MapReduce WordCount 教程对于如何编译 WordCount.java 几乎是一笔带过… 而有写到的 ...

  4. 在Ubuntu下配置运行Hadoop2.4.0单节点配置

    还没有修改hosts,请先按前文修改. 还没安装java的,请按照前文配置. (1)增加用户并设立公钥: sudo addgroup hadoop sudo adduser --ingroup had ...

  5. 一套代码小程序&Web&Native运行的探索05——snabbdom

    接上文:一套代码小程序&Web&Native运行的探索04——数据更新 对应Git代码地址请见:https://github.com/yexiaochai/wxdemo/tree/ma ...

  6. 一套代码小程序&Web&Native运行的探索04——数据更新

    接上文:一套代码小程序&Web&Native运行的探索03 对应Git代码地址请见:https://github.com/yexiaochai/wxdemo/tree/master/m ...

  7. 一套代码小程序&Web&Native运行的探索03——处理模板及属性

    接上文:一套代码小程序&Web&Native运行的探索02 对应Git代码地址请见:https://github.com/yexiaochai/wxdemo/tree/master/m ...

  8. Tensorflow版Faster RCNN源码解析(TFFRCNN) (3)推断(测试)过程使用RPN时代码运行流程

    本blog为github上CharlesShang/TFFRCNN版源码解析系列代码笔记第三篇   推断(测试)过程不使用RPN时代码运行流程 作者:Jiang Wu  原文见:https://hom ...

  9. Tensorflow版Faster RCNN源码解析(TFFRCNN) (2)推断(测试)过程不使用RPN时代码运行流程

    本blog为github上CharlesShang/TFFRCNN版源码解析系列代码笔记第二篇   推断(测试)过程不使用RPN时代码运行流程 作者:Jiang Wu  原文见:https://hom ...

随机推荐

  1. php连接mysql

    一.php连接mysql的函数 1.mysql_connect 作用:连接mysql eg:$con=mysql_connect('localhost','root','123456'); 2.mys ...

  2. 微信事业群WXG成立 致力于打造微信大平台

    今天,微信之父张小龙带领微信团队成立微信事业群(WeiXin Group,简称WXG),致力于打造微信大平台,由他们负责微信基础平台.微信开放平台.微信支付拓展.O2O等微信延伸业务的发展,并包括邮箱 ...

  3. MAVEN for mac 安装

    http://blog.csdn.net/anialy/article/details/22217937 下载 maven  http://mirrors.hust.edu.cn/apache/mav ...

  4. springMVC之配置

    1.项目结构 2.所需jar包 3.web.xml文件 <?xml version="1.0" encoding="UTF-8"?> <web ...

  5. mysql workbench建表时PK,NN,UQ,BIN,UN,ZF,AI

    1. [intrinsic column flags] (基本字段类型标识) - PK: primary key (column is part of a pk) 主键 - NN: not null ...

  6. Eclipse 语法提示

    新建一个txt 拷贝下面的文本,然后保存修改扩展名为.epf #Sat Nov :: CST /instance/org.eclipse.jdt.core/org.eclipse.jdt.core.c ...

  7. delay() .split()

    delay(500) 延时多少秒后执行,结合animate()使用 delay(500).animate({},时间) .split() stringObject.split(separator,ho ...

  8. OpenCV成长之路(5):图像直方图的应用

    正如第4篇文章所说的图像直方图在特征提取方面有着很重要的作用,本文将举两个实际工程中非常实用的例子来说明图像直方图的应用. 一.直方图的反向映射. 我们以人脸检测举例,在人脸检测中,我们第一步往往需要 ...

  9. Tomcat端口占用

    Tomcat端口占用问题 1.打开cmd,运行 netstat -aon!findstr "8080"查看该端口中运行的服务: 2.运行taskkill /pid PID /F 强 ...

  10. 【Eclipse】在Eclipse工具中自定义类注释

    直接上图:这个公司基本都已经定制好了,自己写demo的时候可以适当定制自己的注释 package com.zlg.controller; zlg  : 此处输入zlg(对应模版的名称) 然后ALT+/ ...