java - 这是否以正确的方式使用 Log4j Hadoop?

标签 java hadoop log4j

我不断收到以下错误:

OpcodeCount.java:24: error: <identifier> expected
LOG.warn("something :)");
        ^
OpcodeCount.java:24: error: illegal start of type

下面这样调用Lo​​g4j不可以吗?

public class OpcodeCount {

  // debugging output
  private static final Logger LOG = org.apache.log4j.Logger.getLogger(this.getClass());
  LOG.warn("something :)");

这是我的其余代码:

import org.apache.log4j.Logger;
import java.io.IOException;
import java.util.StringTokenizer;

import org.apache.hadoop.conf.Configuration;
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.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class OpcodeCount {

  // debugging output
  private static final Logger LOG = org.apache.log4j.Logger.getLogger(this.getClass());
  LOG.warn("something :)");

  public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable>{

    private final static IntWritable one = new IntWritable(1);
    private Text word = new Text();

    // debugging output
    private static final Logger LOG = org.apache.log4j.Logger.getLogger(this.getClass());
    LOG.warn("something :)");

    public void map(Object key, Text value, Context context) throws IOException, InterruptedException {

      StringTokenizer itr = new StringTokenizer(value.toString());
      while (itr.hasMoreTokens()) {
        word.set(itr.nextToken());
        context.write(word, one);
      }
    }
  }

  public static class IntSumReducer extends Reducer<Text,IntWritable,Text,IntWritable> {
    private IntWritable result = new IntWritable();

    // debugging output
    private static final Logger LOG = org.apache.log4j.Logger.getLogger(this.getClass());
    LOG.warn("something :)");

    public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
      int sum = 0;
      for (IntWritable val : values) {
        sum += val.get();
      }
      result.set(sum);
      context.write(key, result);
    }
  }

  public static void main(String[] args) throws Exception {
    Configuration conf = new Configuration();
    Job job = Job.getInstance(conf, "opcode count");
    job.setJarByClass(OpcodeCount.class);
    job.setMapperClass(TokenizerMapper.class);
    job.setCombinerClass(IntSumReducer.class);
    job.setReducerClass(IntSumReducer.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);
    FileInputFormat.addInputPath(job, new Path(args[0]));
    FileOutputFormat.setOutputPath(job, new Path(args[1]));
    System.exit(job.waitForCompletion(true) ? 0 : 1);
  }
}

最佳答案

Log4j 不是问题,因为它是 Java 编译器抛出的错误。

您不能在另一个方法或静态初始化程序 block 之外调用实例方法。

.warn() 移动到 map()

关于java - 这是否以正确的方式使用 Log4j Hadoop?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46502727/

相关文章:

java - 尽管有正确的凭据,Spring Security 仍给出 403

java - setDefaultLookAndFeelDecorated 影响 JFrame 调整大小行为

java - 以编程方式添加 Log4J 附加程序

java - 如何使用java代码设置hadoop tmp目录

hadoop - hadoop在Java src代码中在哪里使用yarn.log.file

java - 第三方 jar 创建日志文件

java - 保存控制台而不更改 System.out.println ("blabla");

java - Java 中 findInLine 方法中的字符串模式

java - Hibernate问题无法保存到数据库

java - 使用sdkman安装后如何设置gradle路径