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

MapReduce处理数据去重与数据排序,mapreduce排序

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

本文主要包含mapreduce过滤数据,mapreduce数据流图,mapreduce数据倾斜,mapreduce数据清洗,mapreduce数据去重等服务器相关知识,网友希望可以进行参考

MapReduce处理数据去重与数据排序,mapreduce排序


一:MapReduce处理数据去重

Map的key具有数据去重的功能

/*
 * 去除数据中相同数据
 * 数据去重问题
 * 以整个数据作为key发送出去, value为null
 */
public class DelsameMap 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 line = value.toString();
		if (line.length() > 0) {
			context.write(new Text(line.trim()), new Text(""));
		}

	}
}


 

public class DelsameRedu extends Reducer<Text, Text, Text, NullWritable> {
	@Override
	protected void reduce(Text key, Iterable<Text> values,
			Reducer<Text, Text, Text, NullWritable>.Context context)
			throws IOException, InterruptedException {
		context.write(key, NullWritable.get());
	}
}
public class DelsameMain {
	public static void main(String[] args) throws Exception {
		Configuration conf = new Configuration();
		Job job = new Job(conf);
		job.setJarByClass(DelsameMain.class);

		job.setMapperClass(DelsameMap.class);
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(Text.class);

		job.setReducerClass(DelsameRedu.class);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(NullWritable.class);

		FileInputFormat.addInputPath(job, new Path(args[0]));
		FileOutputFormat.setOutputPath(job, new Path(args[1]));
		job.waitForCompletion(true);
	}
}


二:MapReduce处理数据排序

将原始数据作为map输出的key设置为int类型。map会自动的根据key进行排序

/*
 * mapreduce处理数据排序
 *将原始数据作为map输出的key设置为int类型。map会自动的根据key进行排序
 */
public class SortMap extends Mapper<LongWritable, Text, IntWritable, Text> {
	@Override
	protected void map(LongWritable key, Text value,
			Mapper<LongWritable, Text, IntWritable, Text>.Context context)
			throws IOException, InterruptedException {
		String line = value.toString();
		if (line.length() > 0) {
			context.write(new IntWritable(Integer.parseInt(line.trim())),
					new Text(""));
		}
	}
}


/*
 * 将values作为次序key。将map排序好的key作为value输出
 */
public class SortRedu extends
		Reducer<IntWritable, Text, IntWritable, IntWritable> {
	private IntWritable num = new IntWritable(1);

	@Override
	protected void reduce(IntWritable key, Iterable<Text> values,
			Reducer<IntWritable, Text, IntWritable, IntWritable>.Context context)
			throws IOException, InterruptedException {

		// 将values作为排序的次序。将map拍好序的key作为reduce的value输出
		for (Text val : values) {
			context.write(num, key);
			num = new IntWritable(num.get() + 1);
		}
	}
}


public class SortMain {
	public static void main(String[] args) throws Exception {
		Configuration conf = new Configuration();
		Job job = new Job(conf);
		job.setJarByClass(SortMain.class);

		job.setMapperClass(SortMap.class);
		job.setMapOutputKeyClass(IntWritable.class);
		job.setMapOutputValueClass(Text.class);

		job.setReducerClass(SortRedu.class);
		job.setOutputKeyClass(IntWritable.class);
		job.setOutputValueClass(IntWritable.class);

		FileInputFormat.addInputPath(job, new Path(args[0]));
		FileOutputFormat.setOutputPath(job, new Path(args[1]));

		job.waitForCompletion(true);
	}
}



 

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

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

  • MapReduce处理数据去重与数据排序,mapreduce排序

相关文章

  • 在windows下的安装Docker的教程
  • VMware中centos系统连接wifi的图文方法
  • 初次使用Docker的体验笔记总结
  • Docker镜像制作详解介绍
  • CentOS7 Nexus安装步骤详细介绍
  • Docker 命令教程(附中文解释)
  • Docker容器中运行flume及启动不输出运行日志问题
  • 关于docker容器优雅退出的问题详解
  • 如何Docker化Python Django应用程序
  • Docker学习之常用的基础命令总结

文章分类

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

最近更新的内容

    • VM12 虚拟机使用桥接模式却连不上网的解决方法(图文讲解)
    • 详解在docker中制作自己的JDK+tomcat镜像
    • Docker容器配置Nginx实例分享
    • 在 docker 之间导出导入镜像的方法
    • VMware中虚拟机的NAT设置方法
    • VirtualBox 2.2.0使用主机网络上网配置教程
    • 解决ubuntu下vi上下左右方向键出现字母backspace键不能删除字符 问题
    • Docker daemon 无法启动: does not match with stored UUID错误解决办法
    • docker中Dockerfile方式建立镜像HelloWorld
    • 从零搭建docker私有仓库的步骤

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

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