• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
导航菜单
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • windows
  • 服务器硬件
  • 服务器运维
  • 云计算
  • 虚拟化
  • IIS教程
  • Linux
  • Apache
  • Ftp
  • DNS
  • Nginx
您的位置:首页 > 服务器 >云计算 > MapReduce 按照Value值进行排序输出,mapreducevalue

MapReduce 按照Value值进行排序输出,mapreducevalue

作者:网友 字体:[增加 减小] 来源:互联网

本文主要包含mapreduce key value,mapreduce求最大值,mapreduce,mapreduce是什么,mapreduce原理等服务器相关知识,网友希望可以进行参考

MapReduce 按照Value值进行排序输出,mapreducevalue


文件输入:

A    1
B    5
C    4
E    1
D    3
W    9
P    7
Q    2

文件输出:

W    9
P    7
B    5
C    4
D    3
Q    2
E    1
A    1

代码如下:

package comparator;

import java.io.IOException;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.IntWritable.Comparator;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableComparable;
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;

public class comparator{

    /**
     * @param args
     * @throws IOException
     * @throws IllegalArgumentException
     * @throws InterruptedException
     * @throws ClassNotFoundException
     */
    public static class myComparator extends Comparator {
        @SuppressWarnings("rawtypes")
        public int compare( WritableComparable a,WritableComparable b){
            return -super.compare(a, b);
        }
        public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {
            return -super.compare(b1, s1, l1, b2, s2, l2);
        }
    }
    
    public static class Map extends Mapper<Object,Text,IntWritable,Text>{
        public void map(Object key,Text value,Context context) throws NumberFormatException, IOException, InterruptedException{
            String[] split = value.toString().split("\t");
            context.write(new IntWritable(Integer.parseInt(split[1])),new Text(split[0]) );
        }
    }
    public static class Reduce extends Reducer<IntWritable,Text,Text,IntWritable>{
        public void reduce(IntWritable key,Iterable<Text>values,Context context) throws IOException, InterruptedException{
            for (Text text : values) {
                context.write( text,key);
            }
        }
    }
    public static void main(String[] args) throws IllegalArgumentException, IOException, ClassNotFoundException, InterruptedException {
        // TODO Auto-generated method stub

           Job job = new Job();
           job.setJarByClass(comparator.class);
           
           job.setNumReduceTasks(1);   //设置reduce进程为1个,即output生成一个文件
           
           job.setMapperClass(Map.class);      
           job.setReducerClass(Reduce.class);
           
           job.setMapOutputKeyClass(IntWritable.class);
            job.setMapOutputValueClass(Text.class);
           
           job.setOutputKeyClass(Text.class);    //为job的输出数据设置key类
           job.setOutputValueClass(IntWritable.class);   //为job的输出设置value类
           
           job.setSortComparatorClass( myComparator.class);           //自定义排序
           
           FileInputFormat.addInputPath(job, new Path(args[0]));   //设置输入文件的目录
           FileOutputFormat.setOutputPath(job,new Path(args[1])); //设置输出文件的目录
           
           System.exit(job.waitForCompletion(true)?0:1);   //提交任务
    }

}

版权声明:本文为博主原创文章,未经博主允许不得转载。

分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • MapReduce 按照Value值进行排序输出,mapreducevalue

相关文章

  • Java 调用Hive 自定义UDF,java调用hiveudf
  • R720 disable hyperthreading,r720hyperthreading
  • Hadoop向HDFS写入、删除、读取文件,
  • hbase-1.0.1的60010页面无法打开,hbase-1.0.160010
  • 基于Hive及Sqoop的每日PV、UV、IP定时分析,hivesqoop
  • openstack-计算节点安装(Node),openstack-node
  • Redis源码学习-Lua脚本,redis源码-lua
  • Andrew Ng机器学习课程9-补充,andrew9-
  • 大数据分析与应用的8个场景
  • Libvirt中windows虚拟机的动态内存管理,libvirt虚拟机

文章分类

  • windows
  • 服务器硬件
  • 服务器运维
  • 云计算
  • 虚拟化
  • IIS教程
  • Linux
  • Apache
  • Ftp
  • DNS
  • Nginx

最近更新的内容

    • 分布式系列,分布式系统
    • 《转》openstack中删除虚拟主机,状态一直deleting,《转》openstack
    • hive 如何处理科学计数法,hive处理科学计数
    • 分布式数据库MVCC读写设计,数据库mvcc读写
    • 在Windows上使用Eclipse配置Hadoop MapReduce开发环境,hadoopmapreduce
    • Ceph集群磁盘没有剩余空间的解决方法,ceph集群
    • Hadoop之——分布式集群安装过程简化版,hadoop集群简化版
    • NOSQL(三)分布式数据模型,nosql模型
    • RedHadoop创始人童小军在北京开讲“Hadoop2.0集群优化与管理”啦!,redhadoophadoop2.0
    • Hadoop 1.x的Shuffle源码分析之2,hadoopshuffle

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有