代码编写Hadoop已知各文件数据按位分布情况,汇总后找到分布中Top10的数据分布以及所在文件信息;

时间: 2024-03-25 07:37:56 浏览: 19
以下是Hadoop MapReduce的代码实现: Mapper1: ```java public class BitCountMapper extends Mapper<LongWritable, Text, IntWritable, IntWritable> { @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String[] tokens = value.toString().trim().split(","); for (int i = 0; i < tokens.length; i++) { int bit = i + 1; int count = Integer.parseInt(tokens[i].trim()); context.write(new IntWritable(bit), new IntWritable(count)); } } } ``` Reducer1: ```java public class BitCountReducer extends Reducer<IntWritable, IntWritable, IntWritable, IntWritable> { @Override protected void reduce(IntWritable key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int totalCount = 0; for (IntWritable value : values) { totalCount += value.get(); } context.write(key, new IntWritable(totalCount)); } } ``` Mapper2: ```java public class Top10Mapper extends Mapper<LongWritable, Text, IntWritable, IntWritable> { @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String[] tokens = value.toString().trim().split("\t"); int bit = Integer.parseInt(tokens[0].trim()); int count = Integer.parseInt(tokens[1].trim()); context.write(new IntWritable(count), new IntWritable(bit)); } } ``` Reducer2: ```java public class Top10Reducer extends Reducer<IntWritable, IntWritable, IntWritable, Text> { private static final int TOP_N = 10; @Override protected void reduce(IntWritable key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { for (IntWritable value : values) { String fileName = ((FileSplit)context.getInputSplit()).getPath().getName(); context.write(value, new Text(fileName)); } } @Override protected void cleanup(Context context) throws IOException, InterruptedException { Configuration conf = context.getConfiguration(); int n = conf.getInt("n", TOP_N); PriorityQueue<Pair<Integer, String>> queue = new PriorityQueue<>(n, Comparator.comparingInt(Pair::getKey)); while (context.nextKey()) { int count = context.getCurrentKey().get(); String fileName = context.getCurrentValue().toString(); Pair<Integer, String> pair = Pair.of(count, fileName); queue.offer(pair); if (queue.size() > n) { queue.poll(); } } while (!queue.isEmpty()) { Pair<Integer, String> pair = queue.poll(); context.write(new IntWritable(pair.getKey()), new Text(pair.getValue())); } } } ``` Driver: ```java public class Top10BitCount { public static void main(String[] args) throws Exception { Configuration conf1 = new Configuration(); Job job1 = Job.getInstance(conf1, "BitCount"); job1.setJarByClass(Top10BitCount.class); job1.setMapperClass(BitCountMapper.class); job1.setReducerClass(BitCountReducer.class); job1.setOutputKeyClass(IntWritable.class); job1.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job1, new Path(args[0])); FileOutputFormat.setOutputPath(job1, new Path(args[1])); job1.waitForCompletion(true); Configuration conf2 = new Configuration(); conf2.setInt("n", 10); Job job2 = Job.getInstance(conf2, "Top10"); job2.setJarByClass(Top10BitCount.class); job2.setMapperClass(Top10Mapper.class); job2.setReducerClass(Top10Reducer.class); job2.setOutputKeyClass(IntWritable.class); job2.setOutputValueClass(Text.class); FileInputFormat.addInputPath(job2, new Path(args[1])); FileOutputFormat.setOutputPath(job2, new Path(args[2])); job2.waitForCompletion(true); } } ``` 以上代码实现了对输入文件的数据按位分布情况进行汇总,然后找到分布中Top10的数据分布以及所在文件信息。其中,Mapper1和Reducer1实现了按位分布的汇总,Mapper2和Reducer2实现了Top10的查找和输出。最后,在Driver中串联两个Job完成任务。

相关推荐

最新推荐

recommend-type

Hadoop平台搭建(单节点,伪分布,分布式文件系统及其上MapReduce程序测试)

Hadoop平台搭建(单节点,伪分布,分布式文件系统及其上MapReduce程序测试) 原创,保证质量,辛苦了很多天,故3分!
recommend-type

基于Hadoop的数据仓库Hive学习指南.doc

该文档目录如下: ...1.1 基于Hadoop的数据仓库Hive学习指南 1.2实验环境 1.3实验原理 1.3.1 Hive简介 1.3.2 Hive安装 1.3.3安装并配置mysql 1.3.5 Hive简单编程实践 1.3.4 Hive的常用HiveQL操作
recommend-type

第二章 分布式文件系统HDFS+MapReduce(代码实现检查文件是否存在&WordCount统计).docx

第二章 分布式文件系统HDFS+MapReduce(代码实现检查文件是否存在&WordCount统计),课程依赖上一个章节:第一章 大数据安装教程(Virtual&ubuntu&hadoop单机)
recommend-type

构建企业级数仓-Hadoop可行性分析报告.docx

本文档是基于传统数仓转型到大数据平台(Hadoop)的分析报告,主要分为引言,可行性研究前提,对现有数据仓库的分析,Hadoop可行性分析,数据同步,数据处理与计算,可选方案,社会因素方面可行性等内容
recommend-type

hadoop与mysql数据库的那点事(1)

转眼间已经接触了hadoop两周了,从之前的极力排斥到如今的有点喜欢,刚开始被搭建hadoop开发环境搞得几乎要放弃,如今学会了编写小程序,每天都在成长一点挺好的,好好努力,为自己的装备库再填一件武器挺好的,学习...
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

可见光定位LED及其供电硬件具体型号,广角镜头和探测器,实验设计具体流程步骤,

1. 可见光定位LED型号:一般可使用5mm或3mm的普通白色LED,也可以选择专门用于定位的LED,例如OSRAM公司的SFH 4715AS或Vishay公司的VLMU3500-385-120。 2. 供电硬件型号:可以使用常见的直流电源供电,也可以选择专门的LED驱动器,例如Meanwell公司的ELG-75-C或ELG-150-C系列。 3. 广角镜头和探测器型号:一般可采用广角透镜和CMOS摄像头或光电二极管探测器,例如Omron公司的B5W-LA或Murata公司的IRS-B210ST01。 4. 实验设计流程步骤: 1)确定实验目的和研究对象,例如车辆或机器人的定位和导航。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。