Java 计数器在减速机代码中不工作

Java 计数器在减速机代码中不工作,java,hadoop,mapreduce,hadoop2,Java,Hadoop,Mapreduce,Hadoop2,我正在从事一个大型hadoop项目,其中有一个小的KPI,我必须只在输出中写入前10个值。 为了完成这个要求,我使用了一个计数器,当计数器等于11时中断循环,但reducer仍然将所有值写入HDFS 这是一个非常简单的java代码,但我被卡住了:( 为了进行测试,我创建了一个独立的类(java应用程序)来完成这项工作,它在那里工作;我想知道为什么它不能在reducer代码中工作 如果我遗漏了什么,请找个人帮我,并提出建议 MAP-REDUCE代码 包装可比性测试; 导入java.io.IOExc

我正在从事一个大型hadoop项目,其中有一个小的KPI,我必须只在输出中写入前10个值。 为了完成这个要求,我使用了一个计数器,当计数器等于11时中断循环,但reducer仍然将所有值写入HDFS

这是一个非常简单的java代码,但我被卡住了:(

为了进行测试,我创建了一个独立的类(java应用程序)来完成这项工作,它在那里工作;我想知道为什么它不能在reducer代码中工作

如果我遗漏了什么,请找个人帮我,并提出建议

MAP-REDUCE代码
包装可比性测试;
导入java.io.IOException;
导入java.nio.ByteBuffer;
导入org.apache.hadoop.conf.Configuration;
导入org.apache.hadoop.fs.Path;
导入org.apache.hadoop.io.IntWritable;
导入org.apache.hadoop.io.IntWritable.Comparator;
导入org.apache.hadoop.io.LongWritable;
导入org.apache.hadoop.io.NullWritable;
导入org.apache.hadoop.io.Text;
导入org.apache.hadoop.io.WritableComparator;
导入org.apache.hadoop.mapreduce.Job;
导入org.apache.hadoop.mapreduce.Mapper;
导入org.apache.hadoop.mapreduce.Reducer;
导入org.apache.hadoop.mapreduce.Mapper.Context;
导入org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
导入org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
导入org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
导入org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
导入org.apache.hadoop.util.GenericOptionsParser;
公共类ValueSortExp2{
公共静态void main(字符串[]args)引发异常{
配置conf=新配置(true);
字符串参数[]=新的GenericOptionsParser(conf,args);
Job Job=新作业(conf,“Test commond”);
job.setJarByClass(ValueSortExp2.class);
//设置MapReduce
job.setMapperClass(MapTask2.class);
job.setReduceClass(ReduceTask2.class);
作业。setNumReduceTasks(1);
//指定键/值
setMapOutputKeyClass(IntWritable.class);
job.setMapOutputValueClass(Text.class);
job.setOutputKeyClass(IntWritable.class);
job.setOutputValueClass(Text.class);
job.setSortComparatorClass(IntComparator2.class);
//输入
addInputPath(作业,新路径(参数[0]);
setInputFormatClass(TextInputFormat.class);
//输出
setOutputPath(作业,新路径(参数[1]);
setOutputFormatClass(TextOutputFormat.class);
int code=job.waitForCompletion(true)?0:1;
系统退出(代码);
}
公共静态类IntComparator2扩展了WritableComparator{
公共IntComparator2(){
super(IntWritable.class);
}
@凌驾
公共整数比较(字节[]b1、整数s1、整数l1、字节[]b2、整数s2、整数l2){
整数v1=ByteBuffer.wrap(b1,s1,l1).getInt();
整数v2=ByteBuffer.wrap(b2,s2,l2).getInt();
返回v1.compareTo(v2)*(-1);
}
}
公共静态类MapTask2扩展了Mapper{
公共void映射(LongWritable键、文本值、上下文上下文)引发IOException、InterruptedException{
字符串标记[]=value.toString().split(\\t”);
//int empId=Integer.parseInt(标记[0]);
int count=Integer.parseInt(标记[2]);
write(新的IntWritable(count),新的Text(value));
}    
}
公共静态类ReduceTask2扩展了Reducer{
int-cnt=0;
公共void reduce(可写键、可写列表、上下文)
抛出java.io.IOException、InterruptedException{
用于(文本值:列表){
cnt++;
如果(cnt==11)
{
打破
}
write(新的IntWritable(cnt),值);
}
}
}
}  
简单的JAVA代码很好
包装可比性测试;
导入java.io.IOException;
导入java.util.ArrayList;
导入org.apache.hadoop.io.IntWritable;
导入org.apache.hadoop.io.Text;
导入org.apache.hadoop.mapreduce.Reducer.Context;
公共类测试数据{
//静态int cnt=0;
公共静态void main(字符串args[])引发IOException、InterruptedException{
ArrayList=新的ArrayList(){{
添加(“A”);
添加(“B”);
添加(“C”);
添加(“D”);
}};
减少(列表);
}
公共静态void reduce(Iterable列表)
抛出java.io.IOException、InterruptedException{
int-cnt=0;
for(字符串值:列表){
cnt++;
如果(cnt==3)
{
打破
}
系统输出打印项次(值);
}
}
}
示例数据——标题只是更多信息,实际数据来自第二行 `ID名称计数(需要显示前10名描述)

1《玩具总动员》(1995)2077

10 GoldenEye(1995)888

100大会堂(1996)128

1000凝乳(1996)20

1001律师,律师协会(1982)0

1002教育署的下一步行动(1996)8

1003极端措施(1996)121

1004《微光人》(1996)101

1005 D3:强大的鸭子(1996)142

第1006(1996)号会议厅78

1007苹果饺子帮(1975)232

1008戴维·克罗基特,野生边境之王(1955年)97

1009逃到巫婆山(1975)291

101瓶火箭(1996)253

1010《爱虫》(1969)242

1011赫比再次骑行(1974)135

1012老耶勒(1957)301

1013母圈闭,(1961)258

1014 Pollyanna(1960)136

1015返乡:不可思议的旅程(1993)234

1016毛茸茸的
package comparableTest;
import java.io.IOException;
import java.nio.ByteBuffer;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.IntWritable.Comparator;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableComparator;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.Mapper.Context;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;

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

        Configuration conf = new Configuration(true);

        String arguments[] = new GenericOptionsParser(conf, args).getRemainingArgs();

        Job job = new Job(conf, "Test commond");
        job.setJarByClass(ValueSortExp2.class);

        // Setup MapReduce
        job.setMapperClass(MapTask2.class);
        job.setReducerClass(ReduceTask2.class);
        job.setNumReduceTasks(1);

        // Specify key / value
        job.setMapOutputKeyClass(IntWritable.class);
        job.setMapOutputValueClass(Text.class);
        job.setOutputKeyClass(IntWritable.class);
        job.setOutputValueClass(Text.class);
        job.setSortComparatorClass(IntComparator2.class);
        // Input
        FileInputFormat.addInputPath(job, new Path(arguments[0]));
        job.setInputFormatClass(TextInputFormat.class);

        // Output
        FileOutputFormat.setOutputPath(job, new Path(arguments[1]));
        job.setOutputFormatClass(TextOutputFormat.class);


        int code = job.waitForCompletion(true) ? 0 : 1;
        System.exit(code);

    }

    public static class IntComparator2 extends WritableComparator {

        public IntComparator2() {
            super(IntWritable.class);
        }

        @Override
        public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {

            Integer v1 = ByteBuffer.wrap(b1, s1, l1).getInt();
            Integer v2 = ByteBuffer.wrap(b2, s2, l2).getInt();

            return v1.compareTo(v2) * (-1);
        }
    }

    public static class MapTask2 extends Mapper<LongWritable, Text, IntWritable, Text> {

            public void  map(LongWritable key,Text value, Context context) throws IOException, InterruptedException {

                String tokens[]= value.toString().split("\\t");

            //    int empId = Integer.parseInt(tokens[0])    ;    
                int count = Integer.parseInt(tokens[2])    ;

                context.write(new IntWritable(count), new Text(value));

            }    

        }


    public static class ReduceTask2 extends Reducer<IntWritable, Text, IntWritable, Text> {
        int cnt=0;
        public void reduce(IntWritable key, Iterable<Text> list, Context context)
                throws java.io.IOException, InterruptedException {


            for (Text value : list ) {
                cnt ++;

                if (cnt==11)
                {
                    break;    
                }

                context.write(new IntWritable(cnt), value);




            }

        }
}
}  
package comparableTest;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer.Context;

public class TestData {

    //static int cnt=0;


    public static void main(String args[]) throws IOException, InterruptedException {

        ArrayList<String> list = new ArrayList<String>() {{
            add("A");
            add("B");
            add("C");
            add("D");
        }};


        reduce(list);


    }

    public static void reduce(Iterable<String> list)
            throws java.io.IOException, InterruptedException {


        int cnt=0;
        for (String value : list ) {
            cnt ++;

            if (cnt==3)
            {
                break;    
            }

            System.out.println(value);    


        }

    }
}