gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 16:38:35 25 4
gpt4 key购买 nike

我不断收到以下错误:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com