2017-09-30 20 views
0

我不斷收到以下錯誤:這是否以正確的方式使用Log4j Hadoop?

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

難道不允許調用的Log4j下列方式?

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); 
    } 
} 

回答

1

的Log4j是沒有問題的,因爲它是Java編譯器引發錯誤。

您無法在另一個方法或靜態初始化程序塊之外調用實例方法。

移動.warn()map()

+0

人爲我終於得到了它working-想以某種方式聊天,我會告訴你我是如何做它 - 我均值它並沒有真正融入一個SO question-但你非常幫助我 - 我想和你分享我最終如何把它放在一起:) –

+0

隨意在這裏輸入。我會再看看https://chat.stackoverflow.com/rooms/155713/discussion-between-cricket-007-and-s-matthew-english –

+0

man-就在你認爲你已經走出困境的時候。被這個野獸弄得一團糟https://stackoverflow.com/questions/46594989/persistent-log-storage-hadoop-cluster-8042-logs-userlogs-visible-in-execution你有沒有處理過這種情況? –