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 自定义 Partitioner 实现
Hadoop 自定义 Partitioner 实现
hadoop自定义类型编程
Hadoop 自定义 Partitioner 源代码
博客Hadoop机架感知配置自主实现的jar包 http://blog.csdn.net/lemonZhaoTao/article/details/70991813
05-mr程序中自定义分组的实现.avi 06-shuffle机制.avi 07-mr程序的组件全貌.avi 08-textinputformat对切片规划的源码分析.avi 09-倒排索引的mr实现.avi 10-多个job在同一个main方法中提交.avi 第五天 hadoop2...
在windows环境下开发hadoop时,需要配置HADOOP_HOME环境变量,变量值D:\hadoop-common-2.7.3-bin-master,并在Path追加%HADOOP_HOME%\bin,有可能出现如下错误: org.apache.hadoop.io.nativeio.NativeIO$Windows....
45_hadoop2.x_温度排序,分区,分组,自定义封装类02 46_hadoop2.x_温度排序,分区,分组,自定义封装类03 47_hadoop2.x_温度排序,分区,分组,自定义封装类04 48_hadoop2.x_温度排序,分区,分组,自定义封装类05 ...
第一天 hadoop的基本概念 伪分布式hadoop集群安装 hdfs mapreduce 演示 01-hadoop职位需求状况.avi 02-hadoop课程安排.avi 03-hadoop应用场景.avi 04-hadoop对海量数据处理的... 05-mr程序中自定义分组的实现.avi
从hadoop hdfs中读取数据,进行groupby 显示统计结果count、avg、max,用文字和柱状图两种图形界面表示
可以自定义安装jdk和hadoop(格式为*tar.gz) 注意事项 1、安装完jdk和hadoop请手动source /etc/profile 刷新环境变量 2测试脚本环境为centOS6,其他操作系统会有些配置不一样,请手动调整 资源描述: 安装Linux教程...
《Hadoop大数据开发实战》教学教案—01初识Hadoop.pdf《Hadoop大数据开发实战》教学教案—01初识Hadoop.pdf《Hadoop大数据开发实战》教学教案—01初识Hadoop.pdf《Hadoop大数据开发实战》教学教案—01初识Hadoop.pdf...
Hadoop 是一个处理、存储和分析海量的分布式、非结构化数据的开源框架。最初由 Yahoo 的工程师 Doug Cutting 和 Mike Cafarella Hadoop 是一个处理、存储和分析海量的分布式、非结构化数据的开源框架。最初由 Yahoo...
Hadoop是一个由Apache基金会所开发的分布式系统基础架构。用户可以在不了解分布式底层细节的情况下,开发分布式程序。充分利用集群的威力进 Hadoop是一个由Apache基金会所开发的分布式系统基础架构。用户可以在不...
本书从hadoop的缘起开始,由浅入深,结合理论和实践,全方位地介绍hado叩这一高性能处理海量数据集的理想工具。全书共14章,3个附录,涉及的主题包括:haddoop简介:mapreduce简介:hadoop分布式文件系统;hadoop的i...
hadoop_tutorial hadoop入门经典 Hadoop 是一个能够对大量数据进行分布式处理的软件框架。Hadoop 是可靠的,因为它假设计算元素和存储会失败,因此它维护多个工作数据副本,确保能够针对失败的节点重新分布处理。...
hadoop2.7.3 Winutils.exe hadoop.dll
hadoop的dll文件 hadoop.zip
hadoop-annotations-3.1.1.jar hadoop-common-3.1.1.jar hadoop-mapreduce-client-core-3.1.1.jar hadoop-yarn-api-3.1.1.jar hadoop-auth-3.1.1.jar hadoop-hdfs-3.1.1.jar hadoop-mapreduce-client-hs-3.1.1.jar ...