IntWritable数据类型
时间: 2023-11-13 18:05:42 浏览: 175
IntWritable是Hadoop中的一种数据类型,用于表示整数。它是Writable接口的实现类之一,可以在Hadoop的MapReduce程序中作为输入和输出的数据类型。
IntWritable类有两个主要的方法:get()和set()。get()方法返回IntWritable对象的值,而set()方法设置IntWritable对象的值。
下面是一个使用IntWritable作为MapReduce程序的输入和输出类型的示例:
```java
public static class MyMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
StringTokenizer tokenizer = new StringTokenizer(line);
while (tokenizer.hasMoreTokens()) {
word.set(tokenizer.nextToken());
context.write(word, one);
}
}
}
public static class MyReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
private IntWritable result = new IntWritable();
public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
result.set(sum);
context.write(key, result);
}
}
```
阅读全文