1、预先准备2个文件file1.txt和file2.txt。文件内容为网页上摘下,不具有代表性,只为举例。

file1.txt内容为:

With this setup, whenever you change the content of ambari-web files on the host machine, brunch will pick up changes in the background and update. Because of the symbolic link, the changes are automatically picked up by Ambari Server. All you have to do is hit refresh on the browser to see the frontend code changes reflected.
Not seeing code changes as expected? If you have run the maven command to build Ambari previously, you will see files called app.js.gz and vendor.js.gz under the public folder. You need to delete these files for the frontend code changes to be effective, as the app.js.gz and vendor.js.gz files take precedence over app.js and vendor.js, respectively.

file2.txt内容为:

Apache Eagle (incubating) is a highly extensible, scalable monitoring and alerting platform, designed with its flexible application framework and proven big data technologies, such as Kafka, Spark and Storm. It ships a rich set of applications for big data platform monitoring, service health check, JMX metrics, daemon logs, audit logs and yarn applications. External Eagle developers can define applications to monitoring their NoSQLs or Web Servers, and publish to Eagle application repository at your own discretion. It also provides the state-of-art alert engine to report security breaches, service failures, and application anomalies, highly customizable by the alert policy definition.

并将它们传到hdfs中。

hadoop fs -put ~/file* /user/input/

2、使用Eclipse创建一个Java工程

3、导入Hadoop的Jar文件:

将集群中HADOOP_HOME/share/hadoop/目录下的jar包down下来,放到项目新建文件来个lib中,然后把lib目录下的Jar包加入到classpath中。

4、导入Hadoop的配置文件:

将HADOOP_HOME/etc/hadoop/目录下的core-site.xml、hdfs-site.xml、mapper-site.xml、yarn-site.xml文件down下,放到src目录下。

5、MR代码实现

1)、WordMapper类实现

WordMapper.java

import java.io.IOException;

import java.util.StringTokenizer;

import org.apache.hadoop.io.IntWritable;

import org.apache.hadoop.io.Text;

import org.apache.hadoop.mapreduce.Mapper;

public class WordMapper extends Mapper<Object, Text, Text, IntWritable> {

private final static IntWritable one=new IntWritable(1);

private Text word=new Text();

/**

* 把字符串解析成Key-Value形式,发给Reduce来统计

* key 每行文件的偏移量

* value 每行文件的内容

* context map的上下文

*/

public void map(Object key,Text value,Context context) throws IOException, InterruptedException{

StringTokenizer st=new StringTokenizer(value.toString());

while(st.hasMoreTokens()){

word.set(st.nextToken());

context.write(word, one);

}

}

}

2)、WordReducer类实现

WordReducer.java

import java.io.IOException;

import org.apache.hadoop.io.IntWritable;

import org.apache.hadoop.io.Text;

import org.apache.hadoop.mapreduce.Reducer;

public class WordReducer extends Reducer<Text, IntWritable, Text, IntWritable> {

private IntWritable result=new IntWritable();

/**

* 获取map方法的Key-Value结果,相同的Key发送到同一个reduce里处理,

* 然后迭代Key,把Value相加,结果写到HDFS

* key  map端输出的key

* values  map端输出的集合

* context  reduce端的上下文

*/

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);

}

}

3)、WordMain驱动类实现:

WordMain.java

import java.io.IOException;

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.lib.input.FileInputFormat;

import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import org.apache.hadoop.util.GenericOptionsParser;

/**

* 驱动类,用来启动了个MR作业

* @author liudebin

*

*/

public class WordMain {

public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {

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(WordMain.class);//主类

job.setMapperClass(WordMapper.class);//Mapper

job.setCombinerClass(WordReducer.class);//作业合成类

job.setReducerClass(WordReducer.class);//Reducer

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);//等待完成退出

}

}

6、打包、部署和运行

在集群中的Master节点部署打包后的JAR文件,Hadoop会自己把任务传送到各个Slave

1)、打包JAR文件,将项目export成Jar文件。注意,不要export  lib目录下的文件,因为集群环境已经有这些文件。

2)、将打包好的文件上传到集群的Master节点,并运行如下命令:

hadoop jar wordcount.jar com.upit.mr.op.WordMain /user/input/file* /user/output

注意:如果想要在Eclipse下直接运行程序,需要在Eclipse中添加hadoop的插件,并配置。

3、MR开发入门的更多相关文章

  1. Unity3d Hololens MR开发入门

    一.Hololens概述 Hololens有以下特性 1.空间映射借助微软特殊定制的全息处理单元(HPU),HoloLens 实现了对周边环境的快速扫描和空间匹配.这保证了 HoloLens能够准确地 ...

  2. (二)Hololens Unity 开发入门 之 Hello HoloLens~

    学习源于官方文档 微软官文~ 笔记一部分是直接翻译官方文档,部分各人理解不一致的和一些比较浅显的保留英文原文 (二)Hololens Unity 开发入门 之 Hello HoloLens~ 本文主要 ...

  3. 移动H5开发入门教程:12点webAPP前端开发经验

    如果你是一名移动H5前端开发人员,25学堂的小编认为下面的分享的12点webAPP前端开发经验是你必须掌握的基础知识点.算是一篇移动H5开发入门教程吧! 1. viewport:也就是可视区域.对于桌 ...

  4. openresty 前端开发入门五之Mysql篇

    openresty 前端开发入门五之Mysql篇 这章主要演示怎么通过lua连接mysql,并根据用户输入的name从mysql获取数据,并返回给用户 操作mysql主要用到了lua-resty-my ...

  5. java WEB开发入门

    WEB开发入门 1 进入web JAVASE:标准- standard   JAVA桌面程序 GUI    SOCKET JAVAEE:企业-浏览器控制  web 2 软件结构 C/S :client ...

  6. [译]:Xamarin.Android开发入门——Hello,Android Multiscreen深入理解

    原文链接:Hello, Android Multiscreen_DeepDive. 译文链接:Xamarin.Android开发入门--Hello,Android Multiscreen深入理解. 本 ...

  7. [译]:Xamarin.Android开发入门——Hello,Android深入理解

    返回索引目录 原文链接:Hello, Android_DeepDive. 译文链接:Xamarin.Android开发入门--Hello,Android深入理解 本部分介绍利用Xamarin开发And ...

  8. [译]:Xamarin.Android开发入门——Hello,Android快速上手

    返回索引目录 原文链接:Hello, Android_Quickstart. 译文链接:Xamarin.Android开发入门--Hello,Android快速上手 本部分介绍利用Xamarin开发A ...

  9. VR原理讲解及开发入门

    本文是作者obuil根据多年心得专门为想要入门的VR开发者所写,由52VR网站提供支持.   1. VR沉浸感和交互作用产生的原理:   在之前,我们观看一个虚拟的创造内容是通过平面显示器的,52VR ...

随机推荐

  1. linux---网络相关配置,ssh服务,bash命令及优先级,元字符

    - 二:临时配置网络(ip,网关,dns)+永久配置 临时配置: [root@nfs-server ~]# ifconfig ens32: flags=4163<UP,BROADCAST,RUN ...

  2. Prism5.0开发人员指南内容(纯汉语版)

    Prism指南包含以下内容: 下载并安装Prism Prism5.0新内容 介绍 初始化应用程序 组件间的依赖管理 模块化应用程序开发 实现MVVM模式 进击的MVVM 组合式用户界面 导航 松耦合组 ...

  3. hdu2853

    题解: KM算法模板 然后我把另一边加了点 然后写了#define int long long 然后莫名挂... 然后去掉就过了 代码: #include<cstdio> #include ...

  4. wget 认知及常用命令【转载】

    https://www.cnblogs.com/lxz88/p/6278268.html https://www.cnblogs.com/cindy-cindy/p/6847502.html

  5. js的 style.width 取不到元素的宽度值

    以前一直用jquery的.width()方法来获取一个元素的当前的宽度.不管该元素是否设置了宽度,CSS样式是内联.外联or内嵌,都可用此方式获得元素当前的宽度. 今天想用原生JS想获取一个元素宽度时 ...

  6. IOC与DI简介

    IOC:控制反转(Inverse Of Control) 在没用spring框架之前我们是这样写程序的: private UserDao userDao = new UserDaoImpl(); 也就 ...

  7. android mvp RxJava 框架结构分析

    一个MVP结构:M是model,V是Fragment,P是提供者,P持有V和Model,控制获取数据并给V赋值.(结合了RXJava Retrofit和okHttp)

  8. C++11_新语法

    版权声明:本文为博主原创文章,未经博主允许不得转载. 本节主要介绍C++的新特性,对于C++的基础语法不再讲解.由于编译器的不同.在某些地方可能有些差异,但是无太大影响. 讲解本节知识之前先确认你的C ...

  9. charles抓包unknow

    如果能抓到包,可是解析不出请求,那一定是证书问题,注意以下几点: 1.设备安装证书,注意要抓包的每一个设备都要安装证书,每一个设备! 2.pc端也要安装证书 如果以上两点都做到一定可以解析https请 ...

  10. final方法,abstract方法和abstract类,native方法

    final方法 1.为了确保某个函数的行为在继承过程中保持不变,并且不能被覆盖(override),可以使用final方法. 2.为了效率上的考虑,将方法声明为final,让编译器对此方法的调用进行优 ...