我如何使用 map reduce 程序来检查列的值是否符合条件?

How I can use map reduce program to check if a value of column match with a criteria?

我们如何使用算法 Map Reduce 来检查数据文件中列的值是否符合给定的标准?

例如:对于列 C1,我们要检查该列的值是否符合条件:C1 in ("A", "B", "C").

我想要的输出是在 table 中保存符合我的标准的所有行标识符,在其他 table 中保存其他不匹配的行。我当前的代码是:

public class SmallDataMap extends Mapper<Object, Text, Text, Text> {`

    @Override
    protected void map(Object key, Text value, Context context) throws IOException, InterruptedException {

        String[] tokens = value.toString().split(",");
        if (tokens.length != 8) {
            return;
        }
        String gds = tokens[6];
        if (gds.equals("AMA") || (gds.equals("ABA"))) {
            context.write(new Text(gds), new Text(tokens[0]));
        }

    }
}

主要class的代码是:

public class SmallData {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {

        Configuration conf = new Configuration();
        String[] ourArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
        Job job = Job.getInstance(conf, "Structuration par code gds");

        job.setJarByClass(SmallData.class);
        job.setMapperClass(SmallDataMap.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);

        FileInputFormat.addInputPath(job, new Path(ourArgs[0]));
        FileOutputFormat.setOutputPath(job, new Path(ourArgs[1]));

        job.waitForCompletion(true);
    }
}

我生成了 jar 文件,当我尝试在 Cloudera 中执行我的作业时,我遇到了这个错误:

java.lang.RuntimeException: java.lang.ClassNotFoundException: Class smalldata.SmallDataMap not found

首先了解 MapReduce 的基础知识,以了解其工作原理。它不是一种算法,而是一种编程模型,您可以使用它来编写自己的算法。我推荐 this tutorial 作为开始。

在那之后,关于你的问题,你不需要减速器。一份 map-only 份工作就足够了。您可以在映射器中应用您的标准并发出符合您标准的记录。

映射器的伪代码很简单:

map (LongWritable inputKey, Text inputValue){ //assuming value is stored as Text
    String[] columns = inputValue.get().split(",");
    if (columns.length != 8) return; //or throw IllegalArgumentException
    C1 = columns[6];
    if (C1.equals("A") || C1.equals("B") || C1.equals("C")) {
        emit (inputKey, inputValue); //assuming that you just need to keep the records which meet your criteria
    }
}

有关更多详细信息,您可以阅读一些 MapReduce 设计模式(还有一个 book 具有该标题,其中包含过滤条件的示例,如您描述的那样)。