使用 Hadoop 计数器 - 多个作业
Using Hadoop Counters - Multiple jobs
我正在使用 Hadoop 进行 mapreduce 项目。我目前有 3 个顺序工作。
我想使用 Hadoop 计数器,但问题是我想在第一个作业中进行实际计数,但在第三个作业的减速器中访问计数器值。
我怎样才能做到这一点?我应该在哪里定义enum
?我需要通过它扔第二份工作吗?它也有助于查看一些代码示例来执行此操作,因为我还找不到任何东西。
注意:我使用的是 Hadoop 2.7.2
编辑:我已经尝试过 here 中解释的方法,但没有成功。我的情况不同,因为我想从不同的工作访问计数器。 (不是从映射器到减速器)。
我尝试做的事情:
第一份工作:
public static void startFirstJob(String inputPath, String outputPath) throws IOException, ClassNotFoundException, InterruptedException {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "wordCount");
job.setJarByClass(WordCount.class);
job.setMapperClass(WordCountMapper.class);
job.setCombinerClass(WordCountReducer.class);
job.setReducerClass(WordCountReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(LongWritable.class);
job.setInputFormatClass(SequenceFileInputFormat.class);
job.setOutputFormatClass(SequenceFileOutputFormat.class);
FileInputFormat.addInputPath(job, new Path(inputPath));
FileOutputFormat.setOutputPath(job, new Path(outputPath));
job.waitForCompletion(true);
}
在不同的 class:
中定义了计数器枚举
public class CountersClass {
public static enum N_COUNTERS {
SOMECOUNT
}
}
正在尝试读取计数器:
Cluster cluster = new Cluster(context.getConfiguration());
Job job = cluster.getJob(JobID.forName("wordCount"));
Counters counters = job.getCounters();
CountersClass.N_COUNTERS mycounter = CountersClass.N_COUNTERS.valueOf("SOMECOUNT");
Counter c1 = counters.findCounter(mycounter);
long N_Count = c1.getValue();
在第一份工作结束时获取计数器并将它们的值写入文件并在后续工作中读取。如果您想从 reducer 中读取它,则将其写入 HDFS;如果您将在应用程序代码中读取和初始化,则将其写入本地文件。
Counters counters = job.getCounters();
Counter c1 = counters.findCounter(COUNTER_NAME);
System.out.println(c1.getDisplayName()+":"+c1.getValue());
读写文件属于基础教程。
经典的解决方案是将作业的计数器值放入您需要访问它的后续作业的配置中:
因此请确保在计数作业中正确递增它 mapper/reducer:
context.getCounter(CountersClass.N_COUNTERS.SOMECOUNT).increment(1);
然后计算作业完成后:
job.waitForCompletion(true);
Counter someCount = job.getCounters().findCounter(CountersClass.N_COUNTERS.SOMECOUNT);
//put counter value into conf object of the job where you need to access it
//you can choose any name for the conf key really (i just used counter enum name here)
job2.getConfiguration().setLong(CountersClass.N_COUNTERS.SOMECOUNT.name(), someCount.getValue());
下一篇是在另一个工作的 mapper/reducer 中访问它。只需覆盖设置()
例如:
private long someCount;
@Override
protected void setup(Context context) throws IOException,
InterruptedException {
super.setup(context);
this.someCount = context.getConfiguration().getLong(CountersClass.N_COUNTERS.SOMECOUNT.name(), 0));
}
我正在使用 Hadoop 进行 mapreduce 项目。我目前有 3 个顺序工作。
我想使用 Hadoop 计数器,但问题是我想在第一个作业中进行实际计数,但在第三个作业的减速器中访问计数器值。
我怎样才能做到这一点?我应该在哪里定义enum
?我需要通过它扔第二份工作吗?它也有助于查看一些代码示例来执行此操作,因为我还找不到任何东西。
注意:我使用的是 Hadoop 2.7.2
编辑:我已经尝试过 here 中解释的方法,但没有成功。我的情况不同,因为我想从不同的工作访问计数器。 (不是从映射器到减速器)。
我尝试做的事情: 第一份工作:
public static void startFirstJob(String inputPath, String outputPath) throws IOException, ClassNotFoundException, InterruptedException {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "wordCount");
job.setJarByClass(WordCount.class);
job.setMapperClass(WordCountMapper.class);
job.setCombinerClass(WordCountReducer.class);
job.setReducerClass(WordCountReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(LongWritable.class);
job.setInputFormatClass(SequenceFileInputFormat.class);
job.setOutputFormatClass(SequenceFileOutputFormat.class);
FileInputFormat.addInputPath(job, new Path(inputPath));
FileOutputFormat.setOutputPath(job, new Path(outputPath));
job.waitForCompletion(true);
}
在不同的 class:
中定义了计数器枚举public class CountersClass {
public static enum N_COUNTERS {
SOMECOUNT
}
}
正在尝试读取计数器:
Cluster cluster = new Cluster(context.getConfiguration());
Job job = cluster.getJob(JobID.forName("wordCount"));
Counters counters = job.getCounters();
CountersClass.N_COUNTERS mycounter = CountersClass.N_COUNTERS.valueOf("SOMECOUNT");
Counter c1 = counters.findCounter(mycounter);
long N_Count = c1.getValue();
在第一份工作结束时获取计数器并将它们的值写入文件并在后续工作中读取。如果您想从 reducer 中读取它,则将其写入 HDFS;如果您将在应用程序代码中读取和初始化,则将其写入本地文件。
Counters counters = job.getCounters();
Counter c1 = counters.findCounter(COUNTER_NAME);
System.out.println(c1.getDisplayName()+":"+c1.getValue());
读写文件属于基础教程。
经典的解决方案是将作业的计数器值放入您需要访问它的后续作业的配置中:
因此请确保在计数作业中正确递增它 mapper/reducer:
context.getCounter(CountersClass.N_COUNTERS.SOMECOUNT).increment(1);
然后计算作业完成后:
job.waitForCompletion(true);
Counter someCount = job.getCounters().findCounter(CountersClass.N_COUNTERS.SOMECOUNT);
//put counter value into conf object of the job where you need to access it
//you can choose any name for the conf key really (i just used counter enum name here)
job2.getConfiguration().setLong(CountersClass.N_COUNTERS.SOMECOUNT.name(), someCount.getValue());
下一篇是在另一个工作的 mapper/reducer 中访问它。只需覆盖设置() 例如:
private long someCount;
@Override
protected void setup(Context context) throws IOException,
InterruptedException {
super.setup(context);
this.someCount = context.getConfiguration().getLong(CountersClass.N_COUNTERS.SOMECOUNT.name(), 0));
}