老师你好,
请问在Mapper中可以使用条件语句吗?我想用if过滤一下条件, 然后把符合条件的的字符串放进上下文中, 这么做可以吗?我是这么写的:
public static class Task_A_Mapper extends Mapper<Object, Text, Text, Text>{
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
// Split value based on ','
String[] words = value.toString().split(",");
// Output (Name, Hobby) if found Nation == Romania
if (words[2]=="Romania"){
context.write(new Text(words[1]), new Text(words[4]));
}
}
}
public static class Task_A_Reducer extends Reducer<Text, Text, Text, Text>{
public void reduce(Text key, Text values, Context context) throws IOException, InterruptedException {
context.write(key, values);
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "Task_A");
job.setJarByClass(Task_A.class);
job.setMapperClass(Task_A_Mapper.class);
job.setCombinerClass(Task_A_Reducer.class);
job.setReducerClass(Task_A_Reducer.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job, new Path("input/MyPage.csv"));
FileOutputFormat.setOutputPath(job, new Path("output"));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
我读取的文件是csv, 所以用逗号分割,数据都长这个样子:
1,Norma Fisher,Romania,34,Watch Movies 等等
但是我在if后面上下文那里打断点, 并没有停下来诶, 这个程序可以跑, 但是最后结果是空的。