matadata:

hadoop  a
spark a
hive a
hbase a
tachyon a
storm a
redis a

自定义分组

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.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;
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.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser; public class MyGroup {
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 databaseV1 <inputpath> <outputpath>");
} Job job = Job.getInstance(conf, MyGroup.class.getSimpleName() + "1");
job.setJarByClass(MyGroup.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
job.setMapperClass(MyMapper1.class);
job.setGroupingComparatorClass(MyGroupComparator.class);
job.setReducerClass(MyReducer1.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
job.waitForCompletion(true);
}
public static class MyMapper1 extends Mapper<LongWritable, Text, Text, Text>{
@Override
protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, Text>.Context context)
throws IOException, InterruptedException {
String[] spl=value.toString().split("\t");
context.write(new Text(spl[0].trim()), new Text(spl[1].trim()));
}
}
public static class MyReducer1 extends Reducer<Text, Text, Text, Text>{
@Override
protected void reduce(Text k2, Iterable<Text> v2s, Reducer<Text, Text, Text, Text>.Context context)
throws IOException, InterruptedException {
Long count=0L;
for (@SuppressWarnings("unused") Text v2 : v2s) {
count++;
context.write(new Text("in--"+k2), new Text(count.toString()));
}
context.write(new Text("out--"+k2), new Text(count.toString()));
}
}
public static class MyGroupComparator extends WritableComparator{
public MyGroupComparator(){
super(Text.class,true);
}
@SuppressWarnings("rawtypes")
public int compare(WritableComparable a, WritableComparable b) {
Text p1 = (Text) a;
Text p2 = (Text) b;
p1.compareTo(p2);
return 0;
}
}
}

结果

in--hadoop      1
in--hbase 2
in--hive 3
in--redis 4
in--spark 5
in--storm 6
in--tachyon 7
out--tachyon 7

然后看下默认分组

public static class MyGroupComparator extends WritableComparator{
public MyGroupComparator(){
super(Text.class,true);
}
@SuppressWarnings("rawtypes")
public int compare(WritableComparable a, WritableComparable b) {
Text p1 = (Text) a;
Text p2 = (Text) b;
return p1.compareTo(p2);
}
}

结果

in--hadoop      1
out--hadoop 1
in--hbase 1
out--hbase 1
in--hive 1
out--hive 1
in--redis 1
out--redis 1
in--spark 1
out--spark 1
in--storm 1
out--storm 1
in--tachyon 1
out--tachyon 1

通过对比,自定义分组就很容易理解了

Hadoop自定义分组Group的更多相关文章

  1. 2 weekend110的hadoop的自定义排序实现 + mr程序中自定义分组的实现

    我想得到按流量来排序,而且还是倒序,怎么达到实现呢? 达到下面这种效果, 默认是根据key来排, 我想根据value里的某个排, 解决思路:将value里的某个,放到key里去,然后来排 下面,开始w ...

  2. Hadoop mapreduce自定义分组RawComparator

    本文发表于本人博客. 今天接着上次[Hadoop mapreduce自定义排序WritableComparable]文章写,按照顺序那么这次应该是讲解自定义分组如何实现,关于操作顺序在这里不多说了,需 ...

  3. 【Hadoop】Hadoop MR 自定义分组 Partition机制

    1.概念 2.Hadoop默认分组机制--所有的Key分到一个组,一个Reduce任务处理 3.代码示例 FlowBean package com.ares.hadoop.mr.flowgroup; ...

  4. 关于MapReduce中自定义分组类(三)

    Job类  /**    * Define the comparator that controls which keys are grouped together    * for a single ...

  5. Table.Group分组…Group(Power Query 之 M 语言)

    数据源: 10列55行数据,其中包括含有重复项的"部门"列和可求和的"金额"列. 目标: 按"部门"列进行分组,显示各部门金额小计. 操作过 ...

  6. Oracle 表分组 group by和模糊查询like

    分组group by写法 select 字段名 from 表名 group by 字段名 查询这个字段名里的种类分组后可以加聚合函数select 字段名,聚合函数 from 表名 group by 字 ...

  7. 大数据量场景下storm自定义分组与Hbase预分区完美结合大幅度节省内存空间

    前言:在系统中向hbase中插入数据时,常常通过设置region的预分区来防止大数据量插入的热点问题,提高数据插入的效率,同时可以减少当数据猛增时由于Region split带来的资源消耗.大量的预分 ...

  8. storm自定义分组与Hbase预分区结合节省内存消耗

    Hbas预分区 在系统中向hbase中插入数据时,常常通过设置region的预分区来防止大数据量插入的热点问题,提高数据插入的效率,同时可以减少当数据猛增时由于Region split带来的资源消耗. ...

  9. MySQL数据分组Group By 和 Having

    现有以下的学生信息表: 若果现在想计算每个班的平均年龄,使用where的操作如下: SELECT Cno AS 班级, AVG(Sage) AS 平均年龄 FROM stu ; 这样的话,有多少个班就 ...

随机推荐

  1. js定时器的使用(实例讲解)

    在javascritp中,有两个关于定时器的专用函数,分别为: 1.倒计定时器:timename=setTimeout("function();",delaytime);2.循环定 ...

  2. Java --HashMap源码解析

    兴趣所致研究一下HashMap的源码,写下自己的理解,基于JDK1.8. 本文大概分析HashMap的put(),get(),resize()三个方法. 首先让我们来看看put()方法. public ...

  3. cnodejs社区论坛6--评论功能

  4. 神奇的Bank系统之旅哦

        奋斗不能等待,我们不能等到垂暮之年再去“全力以赴”.让我们从现在开始,为理想而努力,为人生而拼搏.精诚所至,金石为开,相信奋斗会让我们的青春之花绽放得更加绚烂,让我们的人生之路走下来不留遗憾. ...

  5. mybatis公用代码抽取到单独的mapper.xml文件

    同任何的代码库一样,在mapper中,通常也会有一些公共的sql代码段会被很多业务mapper.xml引用到,比如最常用的可能是分页和数据权限过滤了,尤其是在oracle中的分页语法.为了减少骨架性代 ...

  6. Web 开发最有用的50款 jQuery 插件集锦——《图片特效篇》

    <Web 开发最有用的50款 jQuery 插件集锦>系列文章向大家分享最具创新的50款 jQuery 插件,这些插件分成以下类别:网页布局插件,导航插件,表格插件,滑块和转盘插件,图表插 ...

  7. Framework7 – 赞!功能齐全的 iOS7 App 前端框架

    Framework7 是一个功能很全的 HTML 框架,用来构建 iOS7 应用程序. Framework7 允许您灵活搭建列表视图(表视图) .你可以让他们作为导航菜单,你可以在列表里面使用图标,输 ...

  8. Spring MVC 处理模型数据(@ModelAttribute)

    SpringMVC中的模型数据是非常重要的,因为MVC中的控制(C)请求处理业务逻辑来生成数据模型(M),而视图(V)就是为了渲染数据模型的数据. 直白来讲,上面这句话的意思就是:当有一个查询的请求, ...

  9. document.querySelector和querySelectorAll方法

    querySelector和querySelectorAll是W3C提供的新的查询接口,其主要特点如下: 1.querySelector只返回匹配的第一个元素,如果没有匹配项,返回null.  2.q ...

  10. SharePoint 2013 报:网站在改进过程中处于只读状态,对此给您带来的不便,我们深表歉意

    SharePoint 2013备份过程意外中断,导致再打开站点报:网站在改进过程中处于只读状态,对此给您带来的不便,我们深表歉意 英文:We apologize for any inconvenien ...