hadoop学习(3)
来源:互联网 发布:java大数据 编辑:程序博客网 时间:2024/06/05 20:25
求学生成绩平均值 数据格式 学生名称 成绩
Map函数
package com.hadoop.myhadoop1;import java.io.IOException;import java.util.StringTokenizer;import org.apache.hadoop.io.IntWritable;import org.apache.hadoop.io.LongWritable;import org.apache.hadoop.io.Text;import org.apache.hadoop.mapreduce.Mapper;public class Map extends Mapper<LongWritable,Text,Text,LongWritable> { @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { // TODO Auto-generated method stub String line = value.toString(); System.out.println(line); StringTokenizer tokel = new StringTokenizer(line,"\n"); while (tokel.hasMoreTokens()) { StringTokenizer toke2 = new StringTokenizer(tokel.nextToken()); String strname = toke2.nextToken(); String strscorent = toke2.nextToken(); Text name = new Text(strname); LongWritable scorent = new LongWritable(Integer.parseInt(strscorent)); context.write(name, scorent); } }}
Reduce函数
package com.hadoop.myhadoop1;import java.io.IOException;import org.apache.hadoop.io.LongWritable;import org.apache.hadoop.io.Text;import org.apache.hadoop.mapreduce.Reducer;public class Reduce extends Reducer<Text, LongWritable, Text, LongWritable> { @Override protected void reduce(Text key, Iterable<LongWritable> value, Context context) throws IOException, InterruptedException { // TODO Auto-generated method stub int sum = 0; int count = 0; for (LongWritable scort : value) { sum += scort.get(); count++; } long aver = sum/count; context.write(key, new LongWritable(aver)); }}
Main函数
package com.hadoop.myhadoop1;import java.io.IOException;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.Path;import org.apache.hadoop.io.LongWritable;import org.apache.hadoop.io.Text;import org.apache.hadoop.mapreduce.Job;import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;import org.apache.hadoop.util.GenericOptionsParser;public class Process { public static void main(String[] args) throws IOException, ReflectiveOperationException, InterruptedException { Configuration conf = new Configuration(); conf.set("fs.default.name", "hdfs://192.168.1.108:9000"); String[] ars = new String[]{"input","output"}; String[] otherArgs = new GenericOptionsParser(conf,ars).getRemainingArgs(); if (otherArgs.length != 2) { System.err.println("Usage:wodcount"); System.exit(2); } Job job = new Job(conf, "Score_Process"); job.setJarByClass(Process.class); job.setMapperClass(Map.class); job.setCombinerClass(Reduce.class); job.setReducerClass(Reduce.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(LongWritable.class); FileInputFormat.setInputPaths(job, new Path(otherArgs[0])); FileOutputFormat.setOutputPath(job, new Path(otherArgs[1])); System.out.println(job.waitForCompletion(true)? 1 : 0); }}
0 0
- hadoop学习(3)
- hadoop学习笔记(3) 初识Hadoop
- hadoop学习笔记(3) 初识Hadoop
- hadoop学习笔记(3) 初识Hadoop
- Hadoop学习(3)-Hadoop配置单机版
- Hadoop学习笔记(3)-搭建Hadoop伪分布式
- Hadoop 2.7.3安装与配置------Hadoop学习(1)
- hadoop学习(3)—— hadoop基础模块介绍
- YARN(Hadoop)学习笔记(3)
- 【hadoop学习】--(3)安装mahout
- Spring For Hadoop学习笔记(3)
- 学习hadoop(3)join日志
- Hadoop使用学习笔记(3)
- java操作HDFS------Hadoop学习(3)
- hadoop学习3
- Hadoop学习记录-3
- Hadoop学习笔记(3)
- Hadoop学习笔记 3
- 基于mindwave脑电波进行疲劳检测算法的设计(2)
- gitHUb上边一个效果很好的仪表盘Library
- 第五章作业
- Springmvc+mybatis
- Java消息服务(Java Message Service,JMS)
- hadoop学习(3)
- 若你喜欢怪人 其实我很美
- 向Composite风格数据窗口传递参数
- IP、子网掩码、网关
- sqlplus中单&与双&&的区别
- 第六章作业
- 一致性 hash 算法
- 第14周-项目2-二进制文件浏览器
- mysql主从同步问题解决