问题陈述 - 找到最大值并将其与 key 一起打印
输入:
Key Value
ABC 10
TCA 13
RTY 23
FTY 45
左侧列中的键将是唯一的。不允许重复。
输出:
FTY 45
由于 45 是所有值中的最大值,因此它必须与 key 一起打印。
我已经根据此链接中共享的伪代码编写了 MapReduce 代码 How to design the Key Value pairs for Mapreduce to find the maximum value in a set?
map -
import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.mapreduce.Mapper;
public class Map
extends Mapper<LongWritable,Text,Text,IntWritable>
{
private Text maxKey = new Text();
private IntWritable maxValue = new IntWritable(Integer.MIN_VALUE);
@Override
protected void map( LongWritable key,Text value,Context context)
throws IOException,InterruptedException
{
String line = value.toString().trim();
StringTokenizer token = new StringTokenizer(line);
if(token.countTokens() == 2)
{
String str = token.nextToken();
while(token.hasMoreTokens())
{
int temp = Integer.parseInt(token.nextToken());
if(temp > maxValue.get())
{
maxValue.set(temp);
maxKey.set(str);
}
}
}
}
@Override
protected void cleanup(Context context)
throws IOException,InterruptedException
{
context.write(maxKey,maxValue);
}
}
减少
import java.io.IOException;
import java.util.Iterator;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.mapreduce.Reducer;
public class Reduce
extends Reducer<Text,IntWritable,Text,IntWritable>
{
private Text maxKey = new Text();
private IntWritable maxValue = new IntWritable(Integer.MIN_VALUE);
@Override
protected void reduce(Text key,Iterable<IntWritable> values,Context context)
throws IOException,
InterruptedException
{
Iterator<IntWritable> itr = values.iterator();
while(itr.hasNext())
{
int temp = itr.next().get();
if(temp > maxValue.get())
{
maxKey.set(key);
maxValue.set(temp);
}
}
}
@Override
protected void cleanup(Context context)
throws IOException,InterruptedException
{
context.write(maxKey,maxValue);
}
}
驱动类:
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class MapReduceDriver
{
public static void main(String[] args) throws Exception
{
Job job = new Job();
job.setJarByClass(MapReduceDriver.class);
job.setJobName("DNA Codon Analysis - Part 2");
FileInputFormat.addInputPath(job,new Path(args[0]));
FileOutputFormat.setOutputPath(job,new Path(args[1]));
job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class);
job.setNumReduceTasks(1);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
System.exit(job.waitForCompletion(true)?0:1);
}
}
程序编译并运行以显示此输出 -
-2147483648
可能是map()和reduce()的maxValue设置不对。如何正确设置值(使用 Integer.MIN_VALUE 初始化和比较后更新)以便 reduce() 函数接收正确的键值对?
最佳答案
由于您的 key 始终是唯一的,因此您将无法在 reducer 中聚合它们。因此,如果您的数据集不是非常大,您可以使用一个公共(public)键写入 mapper 的输出,这将强制 mapper 的所有输出只转到一个 reducer。
然后在 reducer 中,您可以迭代这些值以进行比较并将最大值与键一起写入。
在映射器类中,使用公共(public)键值对将文件写入context
public class Map extends Mapper<LongWritable,Text,Text,Text>{
private final Text commonKey = new Text("CommonKey");
@Override
protected void map( LongWritable key,Text value,Context context)
throws IOException,InterruptedException {
String line = value.toString().trim();
String[] kvpair = line.split("\\s+");
context.write(commonKey, new Text(kvpair[0] + "," + kvpair[1]));
}
}
然后在reducer中,找到最大值并写入context。
public static class Reduce extends Reducer<Text, Text, NullWritable, Text>{
private final Integer MAXIMUM_VALUE = Integer.MIN_VALUE;
public void reduce(Text commonKey, Iterable<Text> values, Context context){
Integer finalMax = MAXIMUM_VALUE;
String finalKey;
for (Text value: values){
String[] kvpair = value.toString().trim().split(",")
if(Integer.parseInt(kvpair[1]) > finalMax){
finalKey = kvpair[0];
finalMax = Integer.parseInt(kvpair[1]);
}
}
context.write(new Text(finalKey), new IntWritable(finalMax) );
}
}
预计代码中会出现一些错误。只是在文本编辑器中编写它,让您对如何以不同方式处理您的问题有一些想法。
关于java - MapReduce键值对产生垃圾值的输出值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44733162/
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我正在使用puppet为ruby程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
这是一道面试题,我没有答对,但还是很好奇怎么解。你有N个人的大家庭,分别是1,2,3,...,N岁。你想给你的大家庭拍张照片。所有的家庭成员都排成一排。“我是家里的friend,建议家庭成员安排如下:”1岁的家庭成员坐在这一排的最左边。每两个坐在一起的家庭成员的年龄相差不得超过2岁。输入:整数N,1≤N≤55。输出:摄影师可以拍摄的照片数量。示例->输入:4,输出:4符合条件的数组:[1,2,3,4][1,2,4,3][1,3,2,4][1,3,4,2]另一个例子:输入:5输出:6符合条件的数组:[1,2,3,4,5][1,2,3,5,4][1,2,4,3,5][1,2,4,5,3][
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我
我想使用spawn(针对多个并发子进程)在Ruby中执行一个外部进程,并将标准输出或标准错误收集到一个字符串中,其方式类似于使用Python的子进程Popen.communicate()可以完成的操作。我尝试将:out/:err重定向到一个新的StringIO对象,但这会生成一个ArgumentError,并且临时重新定义$stdxxx会混淆子进程的输出。 最佳答案 如果你不喜欢popen,这是我的方法:r,w=IO.pipepid=Process.spawn(command,:out=>w,:err=>[:child,:out])
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht
这篇文章是继上一篇文章“Observability:从零开始创建Java微服务并监控它(一)”的续篇。在上一篇文章中,我们讲述了如何创建一个Javaweb应用,并使用Filebeat来收集应用所生成的日志。在今天的文章中,我来详述如何收集应用的指标,使用APM来监控应用并监督web服务的在线情况。源码可以在地址 https://github.com/liu-xiao-guo/java_observability 进行下载。摄入指标指标被视为可以随时更改的时间点值。当前请求的数量可以改变任何毫秒。你可能有1000个请求的峰值,然后一切都回到一个请求。这也意味着这些指标可能不准确,你还想提取最小/
HashMap中为什么引入红黑树,而不是AVL树呢1.概述开始学习这个知识点之前我们需要知道,在JDK1.8以及之前,针对HashMap有什么不同。JDK1.7的时候,HashMap的底层实现是数组+链表JDK1.8的时候,HashMap的底层实现是数组+链表+红黑树我们要思考一个问题,为什么要从链表转为红黑树呢。首先先让我们了解下链表有什么不好???2.链表上述的截图其实就是链表的结构,我们来看下链表的增删改查的时间复杂度增:因为链表不是线性结构,所以每次添加的时候,只需要移动一个节点,所以可以理解为复杂度是N(1)删:算法时间复杂度跟增保持一致查:既然是非线性结构,所以查询某一个节点的时候