0

I keep getting the following error:

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

Is it not allowed to call Log4j in the following way?

public class OpcodeCount {

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

Here's the rest of my code:

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);
  }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
smatthewenglish
  • 2,831
  • 4
  • 36
  • 72

1 Answers1

1

Log4j isn't the problem since it's the Java compiler throwing the error.

You can't call an instance method outside of another method or a static initializer block.

Move the .warn() into the map()

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • man- finally I got it working- want to chat somehow and I'll tell you how I did it- I mean- it doesn't really fit into an SO question- but you helped me so much- I want to share with you how I finally put it all together :) – smatthewenglish Oct 01 '17 at 18:01
  • Feel free to type in here. I'll look later https://chat.stackoverflow.com/rooms/155713/discussion-between-cricket-007-and-s-matthew-english – OneCricketeer Oct 01 '17 at 18:20
  • man- just when you think you're out of the woods... got blindsided by this beast https://stackoverflow.com/questions/46594989/persistent-log-storage-hadoop-cluster-8042-logs-userlogs-visible-in-execution have you ever dealt with that situation before? – smatthewenglish Oct 05 '17 at 21:38