hadoop中如何实现GenericWritable
发表于:2025-12-02 作者:千家信息网编辑
千家信息网最后更新 2025年12月02日,这篇文章主要介绍了hadoop中如何实现GenericWritable,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。package c
千家信息网最后更新 2025年12月02日hadoop中如何实现GenericWritable
这篇文章主要介绍了hadoop中如何实现GenericWritable,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
package com.test;import java.io.IOException;import java.util.Iterator;import java.util.StringTokenizer;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.conf.Configured;import org.apache.hadoop.fs.Path;import org.apache.hadoop.io.GenericWritable;import org.apache.hadoop.io.IntWritable;import org.apache.hadoop.io.LongWritable;import org.apache.hadoop.io.Text;import org.apache.hadoop.io.Writable;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.KeyValueTextInputFormat;import org.apache.hadoop.mapreduce.lib.input.MultipleInputs;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.Tool;import org.apache.hadoop.util.ToolRunner;/** * 业务场景: * 含有两个文件,两个文件中单词之间的分隔方式不一样,但是统计出单词在两个文件中公共出现的次数 * * 文件来源1,逗号分隔text1.txt * hello,what * you,haha * 文件来源2,制表符分隔text2.txt * girl boy * father mother */public class WordCountGenericWritable extends Configured implements Tool { public static class Map1 extends Mapper { public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); StringTokenizer st = new StringTokenizer(line, ","); while(st.hasMoreElements()) { context.write(new Text(st.nextElement().toString()), new MyGenericWritable(new LongWritable(1))); } } } public static class Map2 extends Mapper { public void map(Text key, Text value, Context context) throws IOException, InterruptedException { context.write(key, new MyGenericWritable(new Text("1"))); context.write(value, new MyGenericWritable(new Text("1"))); } } public static class Reduce extends Reducer { public void reduce(Text key, Iterable values, Context context) throws IOException, InterruptedException { int count = 0; Iterator it = values.iterator(); while(it.hasNext()) { MyGenericWritable myGw = it.next(); Writable value = myGw.get(); if(value instanceof LongWritable) { count = count + Long.valueOf(((LongWritable)value).get()).intValue(); } if(value instanceof Text) { count = count + Long.valueOf(((Text)value).toString()).intValue(); } } context.write(key, new IntWritable(count)); } } public int run(String[] args) throws IOException, InterruptedException, ClassNotFoundException { Configuration conf = this.getConf(); Job job = new Job(conf); job.setJobName(WordCountGenericWritable.class.getSimpleName()); job.setJarByClass(WordCountGenericWritable.class); MultipleInputs.addInputPath(job, new Path("hdfs://grid131:9000/text1.txt"), TextInputFormat.class, Map1.class); MultipleInputs.addInputPath(job, new Path("hdfs://grid131:9000/text2.txt"), KeyValueTextInputFormat.class, Map2.class); FileOutputFormat.setOutputPath(job, new Path(args[1])); job.setReducerClass(Reduce.class); job.setOutputFormatClass(TextOutputFormat.class); //当map的输出类型和reduce的输出类型不一致的时候,需要单独设置map输出类型 job.setMapOutputKeyClass(Text.class); job.setMapOutputValueClass(MyGenericWritable.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); job.waitForCompletion(true); return job.isSuccessful()?0:1; } public static void main(String[] args) throws Exception { int exit = ToolRunner.run(new WordCount(), args); System.exit(exit); } }class MyGenericWritable extends GenericWritable { public MyGenericWritable() { } public MyGenericWritable(LongWritable longWritable) { super.set(longWritable); } public MyGenericWritable(Text text) { super.set(text); } @Override protected Class extends Writable>[] getTypes() { return new Class[]{LongWritable.class, Text.class}; } } 感谢你能够认真阅读完这篇文章,希望小编分享的"hadoop中如何实现GenericWritable"这篇文章对大家有帮助,同时也希望大家多多支持,关注行业资讯频道,更多相关知识等着你来学习!
文件
篇文章
两个
类型
输出
单词
来源
一致
业务
之间
价值
兴趣
制表符
同时
场景
方式
时候
更多
朋友
次数
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
提供同行评审的数据库
oracle替换数据库服务
中国的网络安全建设
国家颁发的网络安全相关法规
网络安全中国科技大学
昆明 网络技术公司地址
重庆大坪医院网络安全
哈工大数据库战德臣作业
sql数据库 命令备份
网络技术外聘顾问人员费用
网络安全培训公安认证
sci 网络安全学习
简单的实现js调用数据库
长沙理工大学 数据库 中标
万户网络安全工程师
自带防火墙的中小企业应用服务器
软件开发兼职团队
我与网络安全的故事作文
数据库技术与应用开发教程
广西总工会网络安全员
提供同行评审的数据库
武汉达梦数据库是外包吗
我的世界地球服务器进不去
河南通信软件开发服务有哪些
app软件开发的服务哪里好
网络安全法安全保护措施有哪些
我的世界为什么服务器不会卡
艾洛裳网络技术有限公司
登录服务器显示站点不安全
vfp数据库程序设计函数