MapReduce Cross 示例

package com.bsr.cross;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
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;
/**
* 第一次mr--目的是获取某一人是哪些人的好友
*
*
*/
public class Cross { //输入:A:B,C,D,F,E,O
//输出:B->A C->A D->A F->A E->A O->A
public static class Map extends Mapper<LongWritable, Text, Text, Text>{
@Override
protected void map(LongWritable key, Text value,Context context)
throws IOException, InterruptedException {
String[] value1=value.toString().split(":");
String[] value2=value1[1].split(",");
for (String string : value2) {
context.write(new Text(string), new Text(value1[0]));
}
} }
public static class Reduce extends Reducer<Text, Text, Text, Text>{
// 输入<B->A><B->E><B->F>....
// 输出 B A,E,F,J
@Override
protected void reduce(Text key, Iterable<Text> value,Context context)
throws IOException, InterruptedException {
StringBuffer sb=new StringBuffer();
for (Text text : value) {
sb.append(text+",");
}
context.write(key, new Text(sb.toString()));
} } public static void main(String[] args) throws Exception {
//读取classpath下的所有xxx-site.xml配置文件,并进行解析
Configuration conf=new Configuration();
FileSystem fs = FileSystem.get(configuration);
String s = "/wc/output3";
Path path = new Path(s);
fs.delete(path, true); Job job=Job.getInstance(conf); //通过主类的类加载器机制获取到本job的所有代码所在的jar包
job.setJarByClass(Cross.class); //指定本job使用的mapper类
job.setMapperClass(Map.class); //指定本job使用的reducer类
job.setReducerClass(Reduce.class); //指定mapper输出的kv数据类型
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class); //指定reducer输出的kv数据类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class); //指定本job要处理的文件所在的路径
FileInputFormat.setInputPaths(job, new Path("/wc/data/"));
FileOutputFormat.setOutputPath(job, new Path("/wc/output3")); //将本job向hadoop集群提交执行
boolean flag=job.waitForCompletion(true);
System.exit(flag?0:1); } }

进行了逻辑的转换;

MapReduce Cross 示例的更多相关文章

  1. MapReduce编程示例

    1.将hadoop插件放入eclipse/plugins目录中 2.eclipse配置hadoop 依赖包目录 Window—Preferences 3.新建Map/Reduce Project项目 ...

  2. MongoDB MapReduce 的示例。

    // JavaScript source code db.runCommand({ mapreduce: "page", map: function Map() { emit( t ...

  3. PoweJob高级特性-MapReduce完整示例

    由于网上搜索 PowerJob MapReduce 都是设计原理,demo也展示个空壳子,没有演示Map到Reduce结果怎么传递,对于没有MR开发经验的人来说并没有什么帮助,所以这里写了一个有完整计 ...

  4. Hadoop学习之旅三:MapReduce

    MapReduce编程模型 在Google的一篇重要的论文MapReduce: Simplified Data Processing on Large Clusters中提到,Google公司有大量的 ...

  5. MapReduce工作流多种实现方式

    学习 hadoop,必不可少的就是编写 MapReduce 程序.当然,对于简单的分析程序,我们只需一个 MapReduce 任务就能搞定,然而对于比较复杂的分析程序,我们可能需要多个Job或者多个M ...

  6. MapReduce链接作业

    对于简单的分析程序,我们只需一个MapReduce就能搞定,然而对于比较复杂的分析程序,我们可能需要多个Job或者多个Map或者Reduce进行计算.下面我们来说说多个Job或者多个MapReduce ...

  7. MongoDB聚合(count、distinct、group、MapReduce)

    1. count:返回集合中文档的数量. db.friend.count() db.friend.count({'age':24}) 增加查询条件会使count查询变慢. 2. distinct:找出 ...

  8. Hadoop — MapReduce原理解析

    1. 概述 Mapreduce是一个分布式运算程序的编程框架,是用户开发"基于hadoop的数据分析应用"的核心框架: Mapreduce核心功能是将用户编写的业务逻辑代码和自带默 ...

  9. python - hadoop,mapreduce demo

    Hadoop,mapreduce 介绍 59888745@qq.com 大数据工程师是在Linux系统下搭建Hadoop生态系统(cloudera是最大的输出者类似于Linux的红帽), 把用户的交易 ...

随机推荐

  1. VS2017 移动开发(Android and IOS) 序

    序 公司原因,要求用C#开发移动端app,老板觉得用现在会的C#做会比较快... 从零开始,折腾一个多星期,重装系统三遍(强迫症),其它各种折腾,终于手机运行上了第一个APP,看看就好... 不得不吐 ...

  2. HTML5——loading

    https://www.cnblogs.com/wangmeijian/p/4449150.html https://www.cnblogs.com/yunser/p/canvas-baidu-loa ...

  3. vue路由细节探讨

    1.使用router-link 不会让页面刷新,使用a标签会使页面刷新.2.router-link 里面的to="/路由地址" tag=""自定义标签" ...

  4. [分享] IMX6嵌入式开发板linux QT挂载U盘及TF卡

    本文转自迅为开发板:http://www.topeetboard.com 开发平台:iMX6开发板 linux QT 系统下挂载 u 盘如下图所示,qt 启动之后,在超级终端中使用命令“mknod / ...

  5. cut - 在文件的每一行中提取片断

    总览 (SYNOPSIS) ../src/cut [OPTION]... [FILE]... 描述 (DESCRIPTION) 在 每个文件 FILE 的 各行 中, 把 提取的 片断 显示在 标准输 ...

  6. CREATE GROUP - 定义一个新的用户组

    SYNOPSIS CREATE GROUP name [ [ WITH ] option [ ... ] ] where option can be: SYSID gid | USER usernam ...

  7. Linux C下变量和常量的存储的本质

    源代码 #cat main.c #include <stdio.h> int i = 100; int main(void) { func(); return 0; } #cat func ...

  8. PHP实现定时任务的几种方式

    关于定时任务,之前以前认识了一种最常用的:crontab定时任务.通过linux的定时任务去实现.今天又认识了一下php实现定时方式的其它方式,总结一下. 一 服务器定时任务 服务器定时任务,其实就是 ...

  9. Mongo索引学习笔记

    索引使用场景 优:加快查询速度 劣:增删改会产生额外的开销.占用空间 tips: 返回集合中一半以上的数据,全表扫描的效率高 索引基础 基础操作 查看索引:db.test.getIndexes() 创 ...

  10. Cropping multiple images the same way

    The tools we’ll be using are =GIMP= and =mogrify= (from the ImageMagick suite), so make sure that yo ...