我需要在我的mapreduce程序中使用全局变量,如何在以下代码中进行设置并在reducer中使用全局变量。
public class tfidf
{
public static tfidfMap..............
{
}
public static tfidfReduce.............
{
}
public static void main(String args[])
{
Configuration conf=new Configuration();
conf.set("","");
}
}
最佳答案
模板代码可能看起来像这样(Reducer未显示,但是相同的主体)
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Mapper.Context;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
public class ToolExample extends Configured implements Tool {
@Override
public int run(String[] args) throws Exception {
Job job = new Job(getConf());
Configuration conf = job.getConfiguration();
conf.set("strProp", "value");
conf.setInt("intProp", 123);
conf.setBoolean("boolProp", true);
// rest of your config here
// ..
return job.waitForCompletion(true) ? 0 : 1;
}
public static class MyMapper extends
Mapper<LongWritable, Text, LongWritable, Text> {
private String strProp;
private int intProp;
private boolean boolProp;
@Override
protected void setup(Context context) throws IOException,
InterruptedException {
Configuration conf = context.getConfiguration();
strProp = conf.get("strProp");
intProp = conf.getInt("intProp", -1);
boolProp = conf.getBoolean("boolProp", false);
}
}
public static void main(String args[]) throws Exception {
System.exit(ToolRunner.run(new ToolExample(), args));
}
}