- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我目前是 Hadoop 的新手。所以我在 MapReduce 中解决了这段代码,它找出了“每年‘数据工程师’工作最多的国家/地区的部分”(例如,如果格式为 (Year,Region ,Count(Jobs)) 是 "2016,'XYZ',35" 和 "2016,'ABC ',25" 和 "2015,'sdf',14",答案将是 "2016,'XYZ' ,35" 和"2015,'sdf',14"),但我无法理解 reducer 中的部分,如下所示:-
if (Top5DataEngineer.size() > 1)
Top5DataEngineer.remove(Top5DataEngineer.firstKey());
}//Ignore this bracket for the time being.
protected void cleanup(Context context) throws IOException,
InterruptedException {
for (Text t : Top5DataEngineer.descendingMap().values())
context.write(NullWritable.get(), t);
}
这是完整的代码:-
import java.io.IOException;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.Partitioner;
import java.util.TreeMap;
import org.apache.hadoop.mapreduce.Reducer;
public class Q_002a {
public static class Q_002a_Mapper extends
Mapper<LongWritable, Text, Text, LongWritable> {
LongWritable one = new LongWritable(1);
public void map(LongWritable key, Text values, Context context)
throws IOException, InterruptedException {
try {
if (key.get() > 0)
{
String[] token = values.toString().split("\t");
if (token[4].equals("DATA ENGINEER")) {
Text answer = new Text(token[8] + "\t" + token[7]);
context.write(answer, one);
}
}
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(e.getMessage());
} catch (ArithmeticException e1) {
System.out.println(e1.getMessage());
}
}
}
public static class Q_002a_Partitioner extends Partitioner<Text, LongWritable> {
@Override
public int getPartition(Text key, LongWritable value, int numReduceTasks) {
String[] str = key.toString().split("\t");
if (str[1].equals("2011"))
return 0;
if (str[1].equals("2012"))
return 1;
if (str[1].equals("2013"))
return 2;
if (str[1].equals("2014"))
return 3;
if (str[1].equals("2015"))
return 4;
if (str[1].equals("2016"))
return 5;
else
return 6;
}
}
public static class Q_002a_Reducer extends
Reducer<Text, LongWritable, NullWritable, Text> {
private TreeMap<LongWritable, Text> Top5DataEngineer = new TreeMap<LongWritable, Text>();
long sum = 0;
public void reduce(Text key, Iterable<LongWritable> values,
Context context) throws IOException, InterruptedException {
sum = 0;
for (LongWritable val : values) {
sum += val.get();
}
Top5DataEngineer.put(new LongWritable(sum), new Text(key + ","
+ sum));
if (Top5DataEngineer.size() > 1)
Top5DataEngineer.remove(Top5DataEngineer.firstKey());
}
protected void cleanup(Context context) throws IOException,
InterruptedException {
for (Text t : Top5DataEngineer.descendingMap().values())
context.write(NullWritable.get(), t);
}
}
public static void main(String args[]) throws IOException,
InterruptedException, ClassNotFoundException {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "Top 5 Data Engineer in a worksite");
job.setJarByClass(Q_002a.class);
job.setMapperClass(Q_002a_Mapper.class);
job.setPartitionerClass(Q_002a_Partitioner.class);
job.setReducerClass(Q_002a_Reducer.class);
job.setNumReduceTasks(6);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(LongWritable.class);
job.setOutputKeyClass(NullWritable.class);
job.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
这是我得到的输出:-
编辑:- 我尝试在 reduce() 方法中的 cleanup() 方法中运行代码,但它没有按预期工作。它只有在 cleanup() 方法中时才能正常运行。对此有任何帮助,我们将不胜感激。
最佳答案
cleanup()
处理阶段完成时将调用方法。而且它只会被调用一次。
在你的例子中 reduce()
方法是“搜索”按城市分类的最大数据工程师职位总数。 Top5DataEngineer
TreeMap 按排序(升序)顺序存储键,并且在每次迭代中,如果它有多个键,它只会删除第一个键(较小的键)。换句话说处理后Iterable<LongWritable>
值,您将在每个“年”分区中获得一个工作岗位数量最多的城市。
reducer 阶段完成后,cleanup()
方法简单地写入每个已处理分区的结果(Top5DataEngineer
映射中的单个/最大 kv 对)。 cleanup()
每个“年”分区将调用一次方法。
希望对你有所帮助。
关于hadoop - cleanup() 方法如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48298798/
我们在 Jboss EAP 6.20 Beta1 上看到以下警告 2014-04-01 03:00:00,539 WARN [LocalManagedConnectionFactory:cleanu
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 9 个月前关闭。 Improve t
我们的 TeamCity 服务器现在运行了大约 3 年,每个工件都保存在硬盘上。现在硬盘快满了。 我查看了清理规则,有: Everything older than the 5th successfu
这个问题已经有答案了: Is there a destructor for Java? (24 个回答) 已关闭 8 年前。 我正在将一些 C++ 代码移植到 Java,这些代码使用非常常见的 C++
我目前是 Hadoop 的新手。所以我在 MapReduce 中解决了这段代码,它找出了“每年‘数据工程师’工作最多的国家/地区的部分”(例如,如果格式为 (Year,Region ,Count(Jo
为了在运行测试之前设置环境,我使用了 GebConfig.groovy 类,它在运行测试时作为第一个组件被触发。但我还需要在所有测试完成后将环境恢复到初始状态。 我试图覆盖扩展 GebReportin
我有这样的设置:我尝试使用自定义事件处理程序来处理 .data 哈希,并在处理程序运行后“垃圾收集”它的项目,以避免保留它们不必要地在内存中等等。 .defer() 函数模拟我得到的东西,它根据提供的
“使用 finally block 允许您运行任何您想要执行的清理类型的语句,无论 protected 代码中发生了什么。”“清理类型语句”和“清理代码”是什么意思? 最佳答案 如评论中所述,“清理”
我不明白 Hadoop 中的清理方法到底是做什么的,它是如何工作的?我有以下 Map-Reduce 代码来计算一堆数字的最大值、最小值和平均值。 public class Statistics {
我对此很陌生,并且对垃圾收集对于无需重新启动的插件的确切工作方式感到有点困惑。 从我的关闭函数中,我当前调用所有必须进行清理的对象。他们将任何事件处理程序删除到他们可能设置的外部代码中,然后删除自己的
使用C++管道api(1.2.0),如何在映射器的map()阶段之后在Mapper.cleanup()中获得调用?基本上,对于每个块,我想在映射阶段将记录存储在内存中,然后再进行一些处理。 欢迎任何提
我编写了一个 eclipse osgi 插件,它对 eclipse 项目中存在的 java 文件运行清理和格式化操作。比如: 运行包含 eclipse 命令的批处理文件 它打开 Eclipse 编辑器
我想知道是否可以在 Spock 测试的生命周期方法中提供以下数据: setup() 方法中的测试名称 cleanup() 方法中的测试名称和测试结果 我问的原因是我想将这些元数据发送到数据接收器,而无
我读到使用 malloc() 时的规则是始终有一个匹配的 free()。如果一个程序中使用了 7 次 malloc(),则必须有相应数量的 free()。但是,这似乎不适用于我在结构内部 malloc
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 5 年前。 Improve this qu
我使用 JUnit 和 FEST 对我们的应用程序进行 Swing 集成测试,我在测试用例中多次启动和停止。 @after 是否应该包含对 robot.cleanUp() 的调用? 最佳答案 一般规则
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
文档和搜索让我对如何清理提交历史感到困惑。 假设我有一个名为 tests 的分支,我用它来将构建推送到 travis-ci,但我的构建失败了。然后我将不得不做一些小的提交来尝试修复错误消息并重新推送
我们每天使用 capistrano(实际上是 webistrano)进行 20 多次部署,但我们遇到了一个问题,即我们服务器上的磁盘空间充满了旧的部署文件夹。 我时不时地运行 deploy:clean
1、cmd指令,进入.svn目录,找到wc.db文件 sqlite 3 打开 2、 对 svn源代码目录 右键, clean up, 稍等1至5分钟左右,然后会提示 清理成功 以上就是小编为
我是一名优秀的程序员,十分优秀!