关于自定义数据类型,http://book.douban.com/annotation/17067489/ 一文中给出了一个比较清晰的说明和解释。
以wordCount为例子
定义自己的数据类型Http类
import java.io.DataInput; import java.io.DataOutput; import java.io.IOException; import org.apache.hadoop.io.WritableComparable; public class Http implements WritableComparable<Http> { public Http(){ } private String value; public Http(String value) { setValue(value); } public String getValue() { return value; } public void setValue(String value) { this.value = value; } public void readFields(DataInput in) throws IOException { value = in.readUTF(); } public void write(DataOutput out) throws IOException { out.writeUTF(value); } public int compareTo(Http http) { return (value.compareTo(http.value)); } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((value == null) ? 0 : value.hashCode()); return result; } @Override public boolean equals(Object obj) { if (!(obj instanceof Http)) return false; Http other = (Http)obj; return this.value.equals(other.value); } @Override public String toString() { return value; } }
编写wordcount程序
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.LongWritable; 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; import org.apache.hadoop.util.GenericOptionsParser; public class WordCountEntry { public static class TokenizerMapper extends Mapper<LongWritable, Http, Http, IntWritable> { private final static IntWritable one = new IntWritable(1); private Http word = new Http(); public void map(LongWritable key, Http value, Context context) throws IOException, InterruptedException { StringTokenizer itr = new StringTokenizer(value.toString()); while (itr.hasMoreTokens()) { word.setValue(itr.nextToken()); context.write(word, one); } } } public static class IntSumReducer extends Reducer<Http, IntWritable, Http, IntWritable> { private IntWritable result = new IntWritable(); public void reduce(Http 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 IOException, InterruptedException, ClassNotFoundException { 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); } Path input = new Path(args[0]); Path output = new Path(args[1]); Job job = new Job(conf, "word count"); job.setJarByClass(WordCountEntry.class); job.setMapperClass(TokenizerMapper.class); job.setCombinerClass(IntSumReducer.class); job.setReducerClass(IntSumReducer.class); job.setOutputKeyClass(Http.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, input); FileOutputFormat.setOutputPath(job, output); System.exit(job.waitForCompletion(true) ? 0 : 1); } }
编写mrUnit测试用例进行mapreduce程序测试
import java.util.ArrayList; import java.util.List; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.mrunit.mapreduce.MapDriver; import org.apache.hadoop.mrunit.mapreduce.ReduceDriver; import org.junit.Before; import org.junit.Test; import com.geo.dmp.WordCountEntry.IntSumReducer; import com.geo.dmp.WordCountEntry.TokenizerMapper; public class WordCountEntryTest { private MapDriver<LongWritable, Http, Http, IntWritable> mapDriver; private ReduceDriver<Http, IntWritable, Http, IntWritable> reduceDriver; @Before public void setUpBeforeClass() throws Exception { TokenizerMapper tm = new TokenizerMapper(); mapDriver = MapDriver.newMapDriver(tm); IntSumReducer isr = new IntSumReducer(); reduceDriver = ReduceDriver.newReduceDriver(isr); } @Test public void TokenizerMapperTest() { mapDriver.withInput(new LongWritable(), new Http("01a55\tablsd")); mapDriver.withOutput(new Http("01a55"), new IntWritable(1)); mapDriver.withOutput(new Http("ablsd"), new IntWritable(1)); mapDriver.runTest(); } @Test public void IntSumReducerTest() { List<IntWritable> values = new ArrayList<IntWritable>(); values.add(new IntWritable(1)); values.add(new IntWritable(1)); reduceDriver.withInput(new Http("01a55"), values); reduceDriver.withOutput(new Http("01a55"), new IntWritable(2)); reduceDriver.runTest(); } }
相关推荐
通过自定义数据类型,可以使用Hadoop处理单表数据查询以及多表相互关联的应用场景。
(1)统计全球每年的最高气温和最低气温。 (2)MapReduce输出结果包含年份、最高气温、...(4)结合Combiner和自定义数据类型完成全球每年最高气温和最低气温的统计。 (5)应用ToolRunner的使用和Eclipse提交MapReduce任务。
(1)统计全球每年的最高气温和最低气温。 (2)MapReduce输出结果包含年份、最高气温、...(4)结合Combiner和自定义数据类型完成全球每年最高气温和最低气温的统计。 (5)应用ToolRunner的使用和Eclipse提交MapReduce任务。
1.5.1 数据类型 1.5.2 操作和函数 1.6 表 1.6.1 托管表(Managed Tables)和外部表(External Tables) 1.6.2 分区(Partitions)和桶(Buckets) 1.6.3 存储格式 1.6.4 导入数据 1.6.5 表的修改 1.6.6 表...
您还可以找到如何在Map Reduce中编写自定义数据类型和自定义分区程序。 #trendfinder文件夹:在Trendfinder文件夹中,您将发现如何使用多个Mappers和Reducers。 在这里,我们根据推文的出现来处理推特数据。 #...
1.5.1 数据类型 1.5.2 操作和函数 1.6 表 1.6.1 托管表(Managed Tables)和外部表(External Tables) 1.6.2 分区(Partitions)和桶(Buckets) 1.6.3 存储格式 1.6.4 导入数据 1.6.5 表的修改 1.6.6 表的丢弃 1.7...
10.2.1 序列化和反序列化技术点64 载入日志文件10.2.2 UDF、分区、分桶和压缩技术点65 编写UDF ...数据类型11.1.5 操作符和函数11.1.6 交互式和非交互式的Pig11.2 使用Pig 在日志数据中发现恶意行为者...
03_MapReduce数据类型及自定义数据类型 04_案例:网站基本指标分析(一) 05_案例:网站基本指标分析(二) 06_分布式安装部署:克隆虚拟机及配置网络 07_分布式安装部署:集群基本配置及服务运行规划 08_分布式安装...
3.4.4 纠删码技术在Hadoop 中的实现…… … ……… ……………… ………… … … …… … … ….. 148 3.5 HDFS 对象存储: Ozone· ……...............…….......….......……….......... .. ... … … … … ...
11.1.4 数据类型 11.1.5 操作符和函数 11.1.6 交互式和非交互式的Pig 11.2 使用Pig 在日志数据中发现恶意行为者 11.2.1 加载数据 技术点67 加载Apache 日志文件 11.2.2 过滤和投影 技术点68 ...
命名法服务:群集内的Hadoop组件(HDFS,Yarn等) 角色:服务实例的角色类型(资源管理器,名称节点,数据节点等。)入门克隆存储库通过安装安装依赖项pipenv install或全局安装依赖项运行init命令./cli.py init初始...
HBase、 Java9 、Java10 、MySQL优化 、JVM原理 、JUC多线程、 CDH版Hadoop Impala、 Flume 、Sqoop、 Azkaban、 Oozie、 HUE、 Kettle、 Kylin 、Spark 、Mllib机器学习、 Flink、 Python、 SpringBoot、 Hadoop3.x...
此文件夹下主要包含2个java文件,TextPair.java和TextPair2.java,都是自定义的数据类型。 TextPair.java重写了write()和readFields()2个函数 TextPair2.java重写的函数较多 extract_mac 主要包括1个java文件和1个...
mr各种应用场景的例子,1.1 内存排序1.2 mr数据类型1.3 自定义mr数据类型1.4 使用自定义数据实现内存排序1.5 二次排序1.6 使用自定义mr数据类型实现二次排序1.7 内存排序找出每一组中的最大值1.8 排序找出每一组中的...
模块自定义数据类型映射减少程序以计算语料库中的实例简单的 bash 脚本来编译 java 类并创建一个 jar (tgc.jar) 数据类型的一些基本单元测试映射器的 Python 原型用法假设 Hadoop 已正确安装和配置:编译和创建 Jar ...
Hive设计特征 Hive 是一种底层封装了Hadoop 的数据仓库处理工具,使用类SQL 的HiveQL 语言实现数据查询,所有Hive 的数据都存储在Hadoop 兼容的文件系统(例如,Amazon S3、HDFS)中。Hive 在加载数据过程中不会对...
2.1 序列化概述 2.2 常用数据序列化类型 2.3 自定义 bean 对象实现序列化接口(Writable) 2.4 序列化案例实操
实木复合地板 parquet-go是Go中的实现。 它提供了读取和写入镶木地板文件的功能,以及提供高级... parquetschema提供了用于解析文本模式定义的功能以及用于手动或以编程方式构造模式定义的数据类型的功能。 floor是围绕
Hadoop MapReduce Mapreduce是一个分布式运算程序的编程框架,是用户开发“基于hadoop的数据分析应用”的核心框架。...2.Mapper的输入数据是KV对的形式(KV的类型可自定义) 3.Mapper的输出数据是KV对的形式(K