我想获得以字母" a-z"开头的字数。为此我正在尝试使用此代码,但它只打印以" z"开头的单词数量。
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
String line = value.toString();
StringTokenizer itr = new StringTokenizer(line);
while( itr.hasMoreTokens()){
String token= itr.nextToken();
if(token.startsWith("a-z")){
word.set("A_Count");
//word.set("Z_Count");
context.write(word, ONE);
}
}
}
答案 0 :(得分:0)
我试过了,它运行正常。
public class Map extends Mapper<LongWritable, Text, Text, IntWritable> {
private final IntWritable VALUE = new IntWritable(1);
private final Text KEY = new Text("COUNT");
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
StringTokenizer tokenizer = new StringTokenizer(line);
while (tokenizer.hasMoreTokens()) {
String token = tokenizer.nextToken();
if(token.startsWith("a-z")){
context.write(KEY, VALUE);
}
}
}
我的减速机看起来像
public class Reduce extends Reducer<Text, IntWritable, IntWritable, NullWritable> {
public void reduce(Text key, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
context.write(new IntWritable(sum),NullWritable.get());
}
}