我们如何使用Map Reduce算法检查数据文件中列的值是否符合给定条件?
例如:对于C1
列,我们要检查此列的值是否符合条件C1 in ("A", "B", "C")
。
我期望的输出是将符合我条件的所有行的标识符保存在一个表中,而在其他表中保存其他不匹配的行的标识符。我当前的代码是:
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]));
}
}
}
主要类的代码是:
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开始。
之后,关于您的问题,您不需要 reducer 。仅 map 作业就足够了。您可以仅在映射器中应用条件,然后发出满足条件的记录。
映射器的伪代码很简单:
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标题,其中包含诸如您所描述的过滤条件示例)。
关于java - 如何使用map reduce程序检查列的值是否与条件匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30803412/