package cn.itcast.hadoop;
import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Mapper.Context;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class MyWordCount {
public static class MyMapper extends
Mapper<Object, Text, Text, IntWritable> {
private final static IntWritable one = new IntWritable(1);// 类似于int类型
private Text word = new Text(); // 可以理解成String类型
public void map(Object key, Text value, Context context)
throws IOException, InterruptedException {
System.err.println(key + "," + value);
// 默认情况下即根据空格分隔字符串
StringTokenizer itr = new StringTokenizer(value.toString());
while (itr.hasMoreTokens()) {
word.set(itr.nextToken());
context.write(word, one);
}
};
}
// Reducer<KEYIN, VALUEIN, KEYOUT, VALUEOUT>
public static class MyReducer extends
Reducer<Text, IntWritable, Text, IntWritable> {
private IntWritable result = new IntWritable();
protected void reduce(Text key, Iterable<IntWritable> values,
Context context) throws IOException, InterruptedException {
System.err.println(key + "," + values);
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
result.set(sum);
;
context.write(key, result);// 这是最后结果
};
}
public static void main(String[] args) throws Exception {
// 声明配置信息
Configuration conf = new Configuration();
// 声明Job
Job job = new Job(conf, "Word Count");
// 设置工作类
job.setJarByClass(MyWordCount.class);
// 设置mapper类
job.setMapperClass(MyMapper.class);
// 可选
job.setCombinerClass(MyReducer.class);
// 设置合并计算类
job.setReducerClass(MyReducer.class);
// 设置key为String类型
job.setOutputKeyClass(Text.class);
// 设置value为int类型
job.setOutputValueClass(IntWritable.class);
// 设置或是接收输入输出
FileInputFormat.setInputPaths(job, new Path("/test/wordcount.txt"));
FileOutputFormat.setOutputPath(job, new Path("/test/count"));
// 执行
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Hadoop客户端Java代码.zip (24个子文件)
Hadoop客户端Java代码
hadoopTest
.classpath 2KB
src
cn
itcast
hadoop
TestGetHostName.java 791B
TestUploadFile.java 714B
TestDeleteFile.java 558B
TestCreateFile.java 710B
TestGetFileLocation.java 822B
TestRename.java 541B
Test.java 667B
TestListFiles.java 641B
TestExistFile.java 520B
MyWordCount.java 2KB
bin
cn
itcast
hadoop
TestDeleteFile.class 1KB
TestUploadFile.class 1KB
Test.class 1KB
TestRename.class 1KB
TestExistFile.class 1KB
MyWordCount$MyMapper.class 3KB
TestGetHostName.class 1KB
MyWordCount.class 2KB
TestListFiles.class 1KB
TestCreateFile.class 1KB
TestGetFileLocation.class 2KB
MyWordCount$MyReducer.class 3KB
.project 439B
共 24 条
- 1
资源评论
小小哭包
- 粉丝: 1900
- 资源: 3864
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功