java - 实现Hadoop计数器时主线程中的IllegalStateException

标签 java hadoop mapreduce

我已经在Hadoop中使用Counters来计算不同类型的LIC客户。下面是我的代码-

map

import java.io.IOException;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.mapreduce.Mapper;

public class Map 
            extends Mapper<LongWritable,Text,Text,IntWritable>
{

@Override
protected void map(LongWritable key,Text value,Context context) 
                        throws IOException,InterruptedException
{   
    String line = value.toString().trim();
    String[] part = line.split(" ");

    if(part.length > 1)
    {
        String str = part[0].toLowerCase();

        if(str.equals("LIC1"))
            context.getCounter("LIC1","lic1").increment(1);
        else if(str.equals("LIC2"))
            context.getCounter("LIC2", "lic2").increment(1);
        else
            context.getCounter("Others", "others").increment(1);

        context.write(new Text(str),new IntWritable(1));
    }
}
}

驾驶员等级
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.IntWritable;
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("Counter Example");

    FileInputFormat.addInputPath(job,new Path(args[0]));
    FileOutputFormat.setOutputPath(job,new Path(args[1]));

    job.setMapperClass(Map.class);
    job.setNumReduceTasks(0);

    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);

    long lic1 = job.getCounters().findCounter("LIC1", "lic1").getValue();
    long lic2 = job.getCounters().findCounter("LIC2", "lic2").getValue();
    long others = job.getCounters().findCounter("Others", 
         "others").getValue();

    System.out.println("LIC1= "+lic1);
    System.out.println("LIC2= "+lic2);
    System.out.println("Others= "+others);

    System.exit(job.waitForCompletion(true)?0:1);

}

}

驱动程序类抛出以下异常
Exception in thread "main" java.lang.IllegalStateException: Job in state 
DEFINE instead of RUNNING
at org.apache.hadoop.mapreduce.Job.ensureState(Job.java:64)
at org.apache.hadoop.mapreduce.Job.getCounters(Job.java:425)
at MapReduceDriver.main(MapReduceDriver.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke 
  (NativeMethodAccessorImpl.java:62)
  at 
  sun.reflect.DelegatingMethodAccessorImpl.invoke 
  (DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:498)
  at org.apache.hadoop.util.RunJar.main(RunJar.java:156

您能帮我找出我们的代码有什么问题吗?

最佳答案

job.getCounters()方法仅适用于正在运行的作业。而且我看不到提交作业以在MapReduceDriver类中运行的代码。添加job.submit()提交作业以运行到集群中并立即返回。

关于java - 实现Hadoop计数器时主线程中的IllegalStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44953258/

相关文章:

java - 在 spring 中创建同名的替换休息 Controller 会导致 bean 名称冲突?

rest - 将 Ambari 与非 HDP 组件一起使用

hadoop - 为什么增加集群数量可以加快 Hadoop MapReduce 中的查询速度?

java - 运行 MapReduce 程序时出现 ClassNotFound 异常

hadoop - 为什么输入输出一般都是 "write once"或者 "immutable"文件?另外,MapReduce 程序的结构是什么?

java - 尝试将 Java Spring 应用程序连接到远程 MongoDB 时,打开套接字时出现异常,连接被拒绝

java - 我的 PITEST 无法运行。覆盖生成minion异常退出。我需要帮助来正确配置我的 pom.xml

java - 如何使用 .hasMoreElements() 循环 JButtonGroup?

hadoop - 有没有办法为 Hadoop 中的推测执行配置超时?

hadoop - 安装 hadoop 2.0.0