2013-02-19 58 views
0

我最近开始在hadoop中工作,并且刚刚学习了一些关于它的基本理论知识。我试图解决一个任务,其中输入将在文本文件中给出,例如input.txt(1 10 37 5 4 98 100等)使用(java编程)在hadoop中查找最大整数值

我需要找到给定输入中的最大整数(即整型)。我试图在arraylist中传递输入,以便可以将第一个整数与所有整数的其余部分进行比较(使用for-loop)。

1)是否可以通过这种方式找到解决方案?如果是的话,我不能在hadoop这里创建一个数组列表,并且需要一些提示:-)

2)我们可以只打印'key'而不是键值对吗?如果有,请帮助我。我试图编码减少功能不打印它,但我得到一些错误。

请引导我一些提示,我可以继续前进。谢谢

回答

0

为此,你最好有一个减速器。

为了保证所有的号码来获得相同的减速机,你必须做两件事情:

  1. :发射的映射
  2. 设置reduce任务为零的所有输入值相同的密钥。

map()方法可能看起来像以下:

@Override 
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { 
      context.write(new Text("MyAwesomeKey"), key); // assuming that your number is being read in the key 
      } 

在你Reduce类,有一个属性max,是这样的: Long max

而且reduce()方法可能看起来像以下:

@Override 
public void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException { 
      context.write(new Text("MyAwesomeKey"), key); // assuming that your number is being read in the key 
      } 

然后覆盖run()也为我们覆盖reduce()

public void run(Context context) throws IOException, InterruptedException { 
    setup(context); 
    while (context.nextKey()) { 
     reduce(context.getCurrentKey(), context.getValues(), context); 
    } 
    context.write(new LongWritable(max),new Text("")); // write the max value 
    cleanup(context); 
    } 

要设置减少任务之一,请执行下列操作在你的工作的run(),注意,这是由上述run()不同:

job.setNumReduceTasks(1); 

注意:以上代码均遵循新的mapreduce API,我相信使用旧的mapred API,我们将无法在减速机完成作业后获得单点挂钩,因为我们可以通过重写Reducer的run()来完成。

+0

非常感谢你的回复。我会努力工作,我会让你知道的。谢谢。 – user2085189 2013-02-20 00:48:26

0

在您的地图步骤中,您可以将所有数字映射到单个键。然后在减少步骤中,您可以取最大值。 reduce步骤将传递给定键的迭代值集合 - 不需要创建自己的ArrayList。

+0

非常感谢你的回复。我会努力工作,我会让你知道的。谢谢。 – user2085189 2013-02-20 00:49:46