Hadoop中如何自定义Map端输出Key
发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,这篇文章主要为大家展示了"Hadoop中如何自定义Map端输出Key",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"Hadoop中如何自定义Map端输出Ke
千家信息网最后更新 2026年11月01日Hadoop中如何自定义Map端输出Key
这篇文章主要为大家展示了"Hadoop中如何自定义Map端输出Key",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"Hadoop中如何自定义Map端输出Key"这篇文章吧。
package sort;import java.io.DataInput;import java.io.DataOutput;import java.io.IOException;import java.net.URI;import java.net.URISyntaxException;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.FileSystem;import org.apache.hadoop.fs.Path;import org.apache.hadoop.io.LongWritable;import org.apache.hadoop.io.RawComparator;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.output.FileOutputFormat;/** * 自定义排序 * 初始结果: * 3 3 * 3 2 * 3 1 * 2 2 * 2 1 * 1 1 * 输出结果: 1 1 2 1 2 2 3 1 3 2 3 3 * @author Xr * */public class SortApp { public static final String INPUT_PATH = "hdfs://hadoop:9000/data"; public static final String OUTPUT_PATH = "hdfs://hadoop:9000/datas"; public static void main(String[] args)throws Exception{ Configuration conf = new Configuration(); existsFile(conf); Job job = new Job(conf, SortApp.class.getName()); FileInputFormat.setInputPaths(job, INPUT_PATH); job.setMapperClass(MyMapper.class); //自定义输出Key job.setMapOutputKeyClass(NewKey.class); job.setMapOutputValueClass(LongWritable.class); job.setReducerClass(MyReducer.class); job.setOutputKeyClass(LongWritable.class); job.setOutputValueClass(LongWritable.class); FileOutputFormat.setOutputPath(job, new Path(OUTPUT_PATH)); job.waitForCompletion(true); } private static void existsFile(Configuration conf) throws IOException, URISyntaxException { FileSystem fs = FileSystem.get(new URI(OUTPUT_PATH),conf); if(fs.exists(new Path(OUTPUT_PATH))){ fs.delete(new Path(OUTPUT_PATH),true); } }}class MyMapper extends Mapper{ @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String string = value.toString(); String[] split = string.split("\t"); NewKey k2 = new NewKey(); k2.set(Long.parseLong(split[0]),Long.parseLong(split[1])); context.write(k2, new LongWritable(Long.parseLong(split[1]))); }}class MyReducer extends Reducer{ @Override protected void reduce(NewKey key2, Iterable values,Context context) throws IOException, InterruptedException { long max = Long.MIN_VALUE; for(LongWritable v2 : values){ long l = v2.get(); if(l>max){ max = l; } } context.write(new LongWritable(key2.first),new LongWritable(max)); }} class NewKey implements WritableComparable{ long first; long second; @Override public void write(DataOutput out) throws IOException { out.writeLong(this.first); out.writeLong(this.second); } public void set(long parseLong, long parseLong2) { this.first = parseLong; this.second = parseLong2; } @Override public void readFields(DataInput in) throws IOException { this.first = in.readLong(); this.second = in.readLong(); } @Override public int compareTo(NewKey o) { if(this.first==o.first){ if(this.second < o.second){ return -1; }else if(this.second == o.second){ return 0; }else{ return 1; } }else{ if(this.first < o.first){ return -1; }else{ return 1; } } }} Name:Xr Date:2014-03-04 21:31 以上是"Hadoop中如何自定义Map端输出Key"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!
输出
内容
篇文章
结果
学习
帮助
易懂
更多
条理
知识
编带
行业
资讯
资讯频道
频道
排序
研究
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
上海喔噻互联网科技有限公司
互联网科技行业发展
善班互联网科技有限公司
友谊互联网络科技
互联网高科技股票
亿人帮互联网科技
互联网科技公司做什么
互联网金融监管科技
美国互联网前沿科技
互联网科技公司需要的职位
青岛中康互联网医疗科技有限公司
北京大学互联网科技怎么样
佳云科技和互联网广告
互联网和黑科技的看法
扫呗互联网科技有限公司
济南小医互联网科技有限公司招聘
全球科技与互联网公司市值变化
长沙渺峰互联网科技有限公司
互联网科技手抄报图片
互联网科技和医药行业
精品咖啡豆
西安互联网科技
互联网科技英文
齐家互联网科技
紫鲸互联网科技
互联网科技股午盘股票涨跌
中正互联网科技招聘
中国最先进互联网科技公司
互联网实体化科技化
科技互联网公司两年变化