Java Hadoop mapreduce自定义可写静态上下文

Java Hadoop mapreduce自定义可写静态上下文,java,hadoop,mapreduce,writable,Java,Hadoop,Mapreduce,Writable,我正在做一个大学作业,我们必须使用hadoop mapreduce。我试图创建一个新的自定义可写文件,因为我想将键值对输出为(key,(doc_name,1)) 我遵循了关于自定义可写的不同教程,我的解决方案与其他教程相同。有什么建议吗?静态字段和方法与所有实例共享。它们用于特定于类而不是特定实例的值。尽量远离他们 为了解决您的问题,您需要实例化类的一个实例(创建一个对象),以便运行时可以为实例保留内存;或者将正在访问的部件更改为具有静态访问权限(不推荐!) 关键字this用于引用确实是实例的东

我正在做一个大学作业,我们必须使用hadoop mapreduce。我试图创建一个新的自定义可写文件,因为我想将键值对输出为(key,(doc_name,1))


我遵循了关于自定义可写的不同教程,我的解决方案与其他教程相同。有什么建议吗?

静态字段和方法与所有实例共享。它们用于特定于类而不是特定实例的值。尽量远离他们

为了解决您的问题,您需要实例化类的一个实例(创建一个对象),以便运行时可以为实例保留内存;或者将正在访问的部件更改为具有
静态访问权限(不推荐!


关键字
this
用于引用确实是实例的东西(因此是this thing),而不是
静态的东西,在这种情况下应该由类名引用。您在不允许的静态环境中使用它。

谢谢您的回答。你是说我需要创建一个实例,是哪个类?我通过“context.write(key,newcustom(val.toString(),1));”在Reducer中创建了一个Custom实例,这是完全错误的。类
Custom
需要定义为static,否则,如果不先实例化
检测器,就无法创建它的实例。“不涉及现场访问。”托马斯荣布卢特我用“一般术语”回答,因此是第二段和第三段
未明确使用,但在创建
自定义
的新实例时使用,该实例并非如您所指出的
静态
,并由所有其他
静态
类使用编写自定义可写实例是一种过激行为。尝试使用Avro:
public class Detector {

    private static final Path TEMP_PATH = new Path("temp");
    private static final String LENGTH = "gramLength";
    private static final String THRESHOLD = "threshold";


    public class Custom implements Writable {

        private Text document;
        private IntWritable count;

        public Custom(){
            setDocument("");
            setCount(0);
        }

        public Custom(String document, int count) {
            setDocument(document);
            setCount(count);
        }

        @Override
        public void readFields(DataInput in) throws IOException {
            // TODO Auto-generated method stub
            document.readFields(in);
            count.readFields(in);
        }

        @Override
        public void write(DataOutput out) throws IOException {
            document.write(out);
            count.write(out);
        }

        public int getCount() {
            return count.get();
        }

        public void setCount(int count) {
            this.count = new IntWritable(count);
        }

        public String getDocument() {
            return document.toString();
        }

        public void setDocument(String document) {
            this.document = new Text(document);
        }

    }

    public static class NGramMapper extends Mapper<Text, Text, Text, Text> {
        private int gramLength;
        private Pattern space_pattern=Pattern.compile("[ ]");
        private StringBuilder gramBuilder= new StringBuilder();

        @Override
        protected void setup(Context context) throws IOException,      InterruptedException{
            gramLength=context.getConfiguration().getInt(LENGTH, 0);
        }

        public void map(Text key, Text value, Context context) throws IOException, InterruptedException {
            String[] tokens=space_pattern.split(value.toString());
            for(int i=0;i<tokens.length;i++){
                gramBuilder.setLength(0);
                if(i+gramLength<=tokens.length){
                    for(int j=i;j<i+gramLength;j++){
                        gramBuilder.append(tokens[j]);
                        gramBuilder.append(" ");
                    }
                    context.write(new Text(gramBuilder.toString()), key);
                }
            }
        }
    }


    public static class OutputReducer extends Reducer<Text, Text, Text, Custom> {

        public void reduce(Text key, Iterable<Text> values, Context context)
                throws IOException, InterruptedException {
            for (Text val : values) {
                context.write(key,new Custom(val.toString(),1));
            }
        }
    }

    public static void main(String[] args) throws Exception {

        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(conf);
        conf.setInt(LENGTH, Integer.parseInt(args[0]));
        conf.setInt(THRESHOLD, Integer.parseInt(args[1]));

        // Setup first MapReduce phase
        Job job1 = Job.getInstance(conf, "WordOrder-first");
        job1.setJarByClass(Detector.class);
        job1.setMapperClass(NGramMapper.class);
        job1.setReducerClass(OutputReducer.class);
        job1.setMapOutputKeyClass(Text.class);
        job1.setMapOutputValueClass(Text.class);
        job1.setOutputKeyClass(Text.class);
        job1.setOutputValueClass(Custom.class);
        job1.setInputFormatClass(WholeFileInputFormat.class);
        FileInputFormat.addInputPath(job1, new Path(args[2]));
        FileOutputFormat.setOutputPath(job1, new Path(args[3]));

        boolean status1 = job1.waitForCompletion(true);
        if (!status1) {
            System.exit(1);
        }
    }
}
Detector.java:147: error: non-static variable this cannot be referenced from a static context
context.write(key,new Custom(val.toString(),1));