我已经链接了两个Map减少作业。 Job1将只有一个reducer,我正在计算浮点值。我想在Job2的reducer中使用这个值。这是我的主要方法设置。
public static String GlobalVriable;
public static void main(String[] args) throws Exception {
int runs = 0;
for (; runs < 10; runs++) {
String inputPath = "part-r-000" + nf.format(runs);
String outputPath = "part-r-000" + nf.format(runs + 1);
MyProgram.MR1(inputPath);
MyProgram.MR2(inputPath, outputPath);
}
}
public static void MR1(String inputPath)
throws IOException, InterruptedException, ClassNotFoundException {
Configuration conf = new Configuration();
conf.set("var1","");
Job job = new Job(conf, "This is job1");
job.setJarByClass(MyProgram.class);
job.setMapperClass(MyMapper1.class);
job.setReducerClass(MyReduce1.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(FloatWritable.class);
FileInputFormat.addInputPath(job, new Path(inputPath));
job.waitForCompletion(true);
GlobalVriable = conf.get("var1"); // I am getting NULL here
}
public static void MR2(String inputPath, String outputPath)
throws IOException, InterruptedException, ClassNotFoundException {
Configuration conf = new Configuration();
Job job = new Job(conf, "This is job2");
...
}
public static class MyReduce1 extends
Reducer<Text, FloatWritable, Text, FloatWritable> {
public void reduce(Text key, Iterable<FloatWritable> values, Context context)
throws IOException, InterruptedException {
float s = 0;
for (FloatWritable val : values) {
s += val.get();
}
String sum = Float.toString(s);
context.getConfiguration().set("var1", sum);
}
}
如您所见,我需要多次迭代整个程序。我的Job1正在从输入中计算单个数字。因为它只是一个数字和很多迭代我不想把它写到HDFS并从中读取。有没有办法分享Myreducer1中计算的值并在Myreducer2中使用它。
更新:我尝试使用conf.set&amp;传递值。 conf.get。该值未被传递。
答案 0 :(得分:5)
以下是通过计数器传递浮点值的方法......
首先,在第一个reducer中,通过乘以1000将浮点值转换为long(例如,保持3位精度)并将结果放入计数器中:
public void cleanup(Context context) {
long result = (long) (floatValue * 1000);
context.getCounter("Result","Result").increment(result);
}
在驱动程序类中,检索long值并将其转换回float:
public static void MR1(String inputPath)
throws IOException, InterruptedException, ClassNotFoundException {
Configuration conf = new Configuration();
Job job = new Job(conf, "This is job1");
job.setJarByClass(MyProgram.class);
job.setMapperClass(MyMapper1.class);
job.setReducerClass(MyReduce1.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(FloatWritable.class);
FileInputFormat.addInputPath(job, new Path(inputPath));
job.waitForCompletion(true);
long result = job.getCounters().findCounter("Result","Result").getValue();
float value = ((float)result) / 1000;
}
答案 1 :(得分:1)
您可以使用ZooKeeper。它非常适合任何跨职位协调或这样的消息传递。
答案 2 :(得分:0)
您不能只将MR1
的返回类型更改为int
(或任何适当的数据类型)并返回您计算的数字:
int myNumber = MyProgram.MR1(inputPath);
然后将参数添加到MR2
并使用您计算的数字调用它:
MyProgram.MR2(inputPath, outputPath, myNumber);