java - Hadoop Mapreduce自定义可写静态上下文

标签 java hadoop mapreduce writable

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

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

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

最佳答案

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

为了解决您的问题,您需要实例化类的实例(创建对象),以便运行时可以为该实例保留内存。或将您正在访问的部分更改为具有static访问权限(不建议使用!)。

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

关于java - Hadoop Mapreduce自定义可写静态上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34679187/

相关文章:

java - Jackson Json 在换行后生成一个空格

scala - 从RDD获取值

python - MapReduce using hadoop streaming via python - 将列表从映射器传递到缩减器并将其作为列表读取

java - Hadoop MapReduce,如何减少自定义对象?

Hadoop Mapreduce 计算大数据的不同向量元素

architecture - 针对大数据问题的可伸缩体系结构解决方案的建议

java - 这个递归循环代码是如何执行的

java - JTextPane 不可变文本 block

java - 防止客户端重载服务器?

python-3.x - 错误 : Python3. 4 -> java.lang.RuntimeException : PipeMapRed. waitOutputThreads():子进程失败,代码为 1