Hadoop实践小项目——统计学生成绩,按总成绩排序

1、创建一个学生成绩的文件

vi data

 2、将txt文件上传到hdfs上

hdfs -fs -put /hadoop/data

3、 在eclipse上实现统计学生成绩的代码

(1)ScoreSortEntity.class

 (2)ScoreSortReduce.class

package demo2;

import java.io.IOException;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Counter;
import org.apache.hadoop.mapreduce.Reducer;

public class ScoreSortReduce extends Reducer< ScoreSortEntity, Text,Text, ScoreSortEntity> {
	@Override
	protected void reduce(ScoreSortEntity scoreSortEntity, Iterable<Text> iterable,Context context)
			throws IOException, InterruptedException {
		try {
			context.write(iterable.iterator().next(),scoreSortEntity);
		} catch (Exception e) {
			Counter countPrint = context.getCounter("Reduce-OutValue",e.getMessage());
			countPrint.increment(1l);
		}
	}



}

(3)ScoreSortMapper.class

package demo2;

import java.io.IOException;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Counter;
import org.apache.hadoop.mapreduce.Mapper;

public class ScoreSortMapper extends Mapper<LongWritable, Text,  ScoreSortEntity, Text>{

	@Override
	protected void map(LongWritable key, Text value, Context context)
			throws IOException, InterruptedException {
		String[] fields=value.toString().split("\t");
		try {
			ScoreSortEntity entity=new ScoreSortEntity(
					fields[0],
					Integer.parseInt(fields[1]),
					Integer.parseInt(fields[2]),
					Integer.parseInt(fields[3]),
					Integer.parseInt(fields[4]));
			context.write(entity, new Text(fields[0]));
		} catch (Exception e) {
			Counter countPrint = context.getCounter("Map-Exception",e.toString());
			countPrint.increment(1l);
		}
	}
}

 (4)ScoreSortDemo.class

package demo2;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
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.io.Text;

public class ScoreSortDemo {

	public static void main(String[] args) throws Exception {
		Configuration conf=new Configuration();
		Job job=Job.getInstance(conf);
		//设置jar
		job.setJarByClass(ScoreSortDemo.class);
		//设置Map和Reduce类
		job.setMapperClass(ScoreSortMapper.class);
		job.setReducerClass(ScoreSortReduce.class);
		//设置Map输出
		job.setMapOutputKeyClass(ScoreSortEntity.class);
		job.setMapOutputValueClass(Text.class);
		//设置Reduce输出
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(ScoreSortEntity.class);
		Path inputPath=	new Path("/hadoop/data");
		Path outputPath = new Path("/hadoop/dataout");
		outputPath.getFileSystem(conf).delete(outputPath, true);//如果输出路径存在,删除之
		//设置输入输出路径
		FileInputFormat.setInputPaths(job, inputPath);
		FileOutputFormat.setOutputPath(job,outputPath);
		boolean waitForCompletion= job.waitForCompletion(true);
		System.exit(waitForCompletion?0:1);
	}

}

将写好的的代码打架包

可参考(81条消息) 在eclipse上实现一个 WordCount 程序,并将 WordCount 程序打包发布到 Hadoop 分布式中运行。_柿子镭的博客-CSDN博客

4、在hadoop中运行

hadoop jar jar_004.jar demo2.ScoreSortDemo

查看结果

hadoop fs -cat /hadoop/dataout/part-r-00000

 

  • 13
    点赞
  • 80
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值