版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、云计算及应用实验报告 项目名称 使用MapReduce分析数据 专业班级 学 号 姓 名 实验成绩:批阅教师:2016年6 月3 日实验3使用MapReduce分析数据实验学时: 2 实验地点: 实验日期: 一、实验目的1.掌握MapReduce的使用方法2.学会使用MapReduce分析数据二、实验内容1.运行MapReduce的wordcount实例2.使用MapReduce完成二度朋友的实验三、实验方法包括实验方法、原理、技术、方案等。四、实验步骤1. 打开hadoop的各个节点2. 运行hadoop中的wordcount实例3. 编写并编译二度朋友实验的代码4. 将要处理的数据传送到h
2、adoop中5. 运行代码处理数据。五、实验结果记录实验输出数据和结果。六、实验结论1. 创建wordcount_in文件夹2. 创建text.txt文件用来保存要处理的数据3. 将要处理的文件text.txt上传到hadoop4. 运行hadoop-example实例5 查看运行的结果6. 编写二度朋友实验的代码import java.io.IOException;import java.util.Random;import java.util.Vector;import org.apache.hadoop.conf.Configuration;import org.apache.hadoo
3、p.fs.FileSystem;import org.apache.hadoop.fs.Path;import org.apache.hadoop.io.Text;import org.apache.hadoop.mapreduce.Job;import org.apache.hadoop.mapreduce.Mapper;import org.apache.hadoop.mapreduce.Reducer;import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;import org.apache.hadoop.mapreduc
4、e.lib.output.FileOutputFormat;import org.apache.hadoop.util.GenericOptionsParser;public class deg2friend public static class job1Mapper extends Mapperprivate Text job1map_key = new Text();private Text job1map_value = new Text(); public void map(Object key, Text value, Context context) throws IOExcep
5、tion, InterruptedException String eachterm = value.toString().split(,|t); for(int i=1; ieachterm.length; i+) if(pareTo(eachtermi)0) job1map_value.set(eachtermi+t+eachterm0); job1map_key.set(eachterm0); context.write(job1map_key, job1map_value); job1map_key.set(eachtermi); context.write(
6、job1map_key, job1map_value); public static class job1Reducer extends Reducer private Text job1reduce_key = new Text();private Text job1reduce_value = new Text();public void reduce(Text key, Iterable values, Context context) throws IOException, InterruptedException String someperson = key.toString();
7、Vector hisfriends = new Vector();for (Text val : values) String eachterm = val.toString().split(t);if(eachterm0.equals(someperson)hisfriends.add(eachterm1);job1reduce_value.set(deg1friend);context.write(val, job1reduce_value);else if(eachterm1.equals(someperson)hisfriends.add(eachterm0);job1reduce_v
8、alue.set(deg1friend);context.write(val, job1reduce_value);for(int i = 0; ihisfriends.size(); i+)for(int j = 0; jhisfriends.size(); j+)if (hisfriends.elementAt(i).compareTo(hisfriends.elementAt(j)0)/job1reduce_key.set(hisfriends.elementAt(j)+t+hisfriends.elementAt(i);/ public static class job2Mapper
9、extends Mapperprivate Text job2map_key = new Text();private Text job2map_value = new Text(); public void map(Object key, Text value, Context context) throws IOException, InterruptedException String lineterms = value.toString().split(t); if(lineterms.length = 3) job2map_key.set(lineterms0+t+lineterms
10、1); job2map_value.set(lineterms2); context.write(job2map_key,job2map_value); public static class job2Reducer extends Reducer private Text job2reducer_key = new Text();private Text job2reducer_value = new Text();public void reduce(Text key, Iterable values, Context context) throws IOException, Interr
11、uptedException Vector relationtags = new Vector();String deg2friendpair = key.toString();for (Text val : values) relationtags.add(val.toString();boolean isadeg1friendpair = false;boolean isadeg2friendpair = false;int surport = 0;for(int i = 0; irelationtags.size(); i+)if(relationtags.elementAt(i).eq
12、uals(deg1friend)isadeg1friendpair = true;else if(relationtags.elementAt(i).equals(deg2friend)isadeg2friendpair = true;surport += 1; if (!isadeg1friendpair) & isadeg2friendpair)job2reducer_key.set(String.valueOf(surport);job2reducer_value.set(deg2friendpair); context.write(job2reducer_key,job2reducer
13、_value);public static void main(String args) throws Exception Configuration conf = new Configuration(); String otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs(); if (otherArgs.length != 2) System.err.println(Usage: deg2friend ); System.exit(2); Job job1 = new Job(conf, deg2friend);
14、 job1.setJarByClass(deg2friend.class); job1.setMapperClass(job1Mapper.class); job1.setReducerClass(job1Reducer.class); job1.setOutputKeyClass(Text.class); job1.setOutputValueClass(Text.class); /定义一个临时目录,先将任务的输出结果写到临时目录中, 下一个排序任务以临时目录为输入目录。 FileInputFormat.addInputPath(job1, new Path(otherArgs0);Path
15、 tempDir = new Path(deg2friend-temp- + Integer.toString(new Random().nextInt(Integer.MAX_VALUE); FileOutputFormat.setOutputPath(job1, tempDir);if(job1.waitForCompletion(true)Job job2 = new Job(conf, deg2friend);job2.setJarByClass(deg2friend.class);FileInputFormat.addInputPath(job2, tempDir);job2.setMapperClass(job2Mapper.class); job2.setReducerClass(job2Reducer.class);FileOutputFormat.setOutputPath(job2, new Path(otherArgs1);job2.setOutputKeyClass(Text.class);job2.setOutputValueClass(Text.class);FileSystem.get(conf).deleteOnExit(tempDir);System.exit(job2.wa
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 福建省泉州市泉港区2025-2026学年上学期期末八年级数学试卷(含答案)
- 飞腾培训课件
- 钢结构环保施工技术要点
- 2026河北雄安人才服务有限公司商业招商岗招聘1人考试备考试题及答案解析
- 2026广东广州市黄埔区大沙街道招聘编外聘用人员1人参考考试题库及答案解析
- 2026山东事业单位统考烟台莱州市招聘63人备考考试试题及答案解析
- 2026年甘肃白银市平川区容通水务有限公司招聘参考考试题库及答案解析
- 2026年上半年黑龙江省农业农村厅事业单位公开招聘工作人员19人考试备考试题及答案解析
- 法律事务办管理制度(3篇)
- 工资薪酬管理制度是什么(3篇)
- 预中标协议书电子版
- 龟的解剖课件
- 蒙牛乳业股份有限公司盈利能力分析
- 2025年碳排放管理师考试试题及答案
- 八年级英语教学设计案例分析Unit3
- 2025年高尔基《童年》阅读测试+答案
- 95-1轻机枪射击课件
- 跟单转正述职报告
- GB/T 46425-2025煤矸石山生态修复技术规范
- 2024-2025学年度黄河水利职业技术学院单招《职业适应性测试》考前冲刺试卷附答案详解【综合卷】
- 中资企业在泰国发展报告(2024-2025)-境外商会联席会议-202509
评论
0/150
提交评论