使用MapReduce分析报告大数据.docx
《使用MapReduce分析报告大数据.docx》由会员分享,可在线阅读,更多相关《使用MapReduce分析报告大数据.docx(13页珍藏版)》请在冰豆网上搜索。
![使用MapReduce分析报告大数据.docx](https://file1.bdocx.com/fileroot1/2022-12/31/07149593-e93b-4bde-a30b-2dc5186ac957/07149593-e93b-4bde-a30b-2dc5186ac9571.gif)
使用MapReduce分析报告大数据
《云计算与应用》
实验报告
项目名称使用MapReduce分析数据
专业班级
学号
姓名
实验成绩:
批阅教师:
2016年6月3日
实验3《使用MapReduce分析数据》
实验学时:
2实验地点:
实验日期:
一、实验目的
1.掌握MapReduce的使用方法
2.学会使用MapReduce分析数据
二、实验容
1.运行MapReduce的wordcount实例
2.使用MapReduce完成二度朋友的实验
三、实验方法
包括实验方法、原理、技术、方案等。
四、实验步骤
1.打开hadoop的各个节点
2.运行hadoop中的wordcount实例
3.编写并编译二度朋友实验的代码
4.将要处理的数据传送到hadoop中
5.运行代码处理数据。
五、实验结果
记录实验输出数据和结果。
六、实验结论
1.创建wordcount_in文件夹
2.创建text.txt文件用来保存要处理的数据
3.
4.运行hadoop-example实例
5查看运行的结果
6.编写二度朋友实验的代码
importjava.io.IOException;
importjava.util.Random;
importjava.util.Vector;
importorg.apache.hadoop.conf.Configuration;
importorg.apache.hadoop.fs.FileSystem;
importorg.apache.hadoop.fs.Path;
importorg.apache.hadoop.io.Text;
importorg.apache.hadoop.mapreduce.Job;
importorg.apache.hadoop.mapreduce.Mapper;
importorg.apache.hadoop.mapreduce.Reducer;
importorg.apache.hadoop.mapreduce.lib.input.FileInputFormat;
importorg.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
importorg.apache.hadoop.util.GenericOptionsParser;
publicclassdeg2friend{
publicstaticclassjob1MapperextendsMapper
privateTextjob1map_key=newText();
privateTextjob1map_value=newText();
publicvoidmap(Objectkey,Textvalue,Contextcontext)throwsIOException,InterruptedException{
Stringeachterm[]=value.toString().split(",|\t");
for(inti=1;iif(eachterm[0]pareTo(eachterm[i])<0){
job1map_value.set(eachterm[0]+"\t"+eachterm[i]);
}
elseif(eachterm[0]pareTo(eachterm[i])>0){
job1map_value.set(eachterm[i]+"\t"+eachterm[0]);
}
job1map_key.set(eachterm[0]);
context.write(job1map_key,job1map_value);
job1map_key.set(eachterm[i]);
context.write(job1map_key,job1map_value);
}
}
}
publicstaticclassjob1ReducerextendsReducer{
privateTextjob1reduce_key=newText();
privateTextjob1reduce_value=newText();
publicvoidreduce(Textkey,Iterablevalues,Contextcontext)throwsIOException,InterruptedException{
Stringsomeperson=key.toString();
Vectorhisfriends=newVector();
for(Textval:
values){
Stringeachterm[]=val.toString().split("\t");
if(eachterm[0].equals(someperson)){
hisfriends.add(eachterm[1]);
job1reduce_value.set("deg1friend");
context.write(val,job1reduce_value);
}
elseif(eachterm[1].equals(someperson)){
hisfriends.add(eachterm[0]);
job1reduce_value.set("deg1friend");
context.write(val,job1reduce_value);
}
}
for(inti=0;ifor(intj=0;jif(hisfriends.elementAt(i)pareTo(hisfriends.elementAt(j))<0){
job1reduce_key.set(hisfriends.elementAt(i)+"\t"+hisfriends.elementAt(j));
job1reduce_value.set("deg2friend");
context.write(job1reduce_key,job1reduce_value);
}
//elseif(hisfriends.elementAt(i)pareTo(hisfriends.elementAt(j))>0){
//job1reduce_key.set(hisfriends.elementAt(j)+"\t"+hisfriends.elementAt(i));
//}
}
}
}
}
publicstaticclassjob2MapperextendsMapper
privateTextjob2map_key=newText();
privateTextjob2map_value=newText();
publicvoidmap(Objectkey,Textvalue,Contextcontext)throwsIOException,InterruptedException{
Stringlineterms[]=value.toString().split("\t");
if(lineterms.length==3){
job2map_key.set(lineterms[0]+"\t"+lineterms[1]);
job2map_value.set(lineterms[2]);
context.write(job2map_key,job2map_value);
}
}
}
publicstaticclassjob2ReducerextendsReducer{
privateTextjob2reducer_key=newText();
privateTextjob2reducer_value=newText();
publicvoidreduce(Textkey,Iterablevalues,Contextcontext)throwsIOException,InterruptedException{
Vectorrelationtags=newVector();
Stringdeg2friendpair=key.toString();
for(Textval:
values){
relationtags.add(val.toString());
}
booleanisadeg1friendpair=false;
booleanisadeg2friendpair=false;
intsurport=0;
for(inti=0;iif(relationtags.elementAt(i).equals("deg1friend")){
isadeg1friendpair=true;
}elseif(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_value);
}
}
}
publicstaticvoidmain(String[]args)throwsException{
Configurationconf=newConfiguration();
String[]otherArgs=newGenericOptionsParser(conf,args).getRemainingArgs();
if(otherArgs.length!
=2){
System.err.println("Usage:
deg2friend");
System.exit
(2);
}
Jobjob1=newJob(conf,"deg2friend");
job1.setJarByClass(deg2friend.class);
job1.setMapperClass(job1Mapper.class);
job1.setReducerClass(job1Reducer.class);
job1.setOutputKeyClass(Text.class);
job1.setOutputValueClass(Text.class);
//定义一个临时目录,先将任务的输出结果写到临时目录中,下一个排序任务以临时目录为输入目录。
FileInputFormat.addInputPath(job1,newPath(otherArgs[0]));
PathtempDir=newPath("deg2friend-temp-"+Integer.toString(newRandom().nextInt(Integer.MAX_VALUE)));
FileOutputFormat.setOutputPath(job1,tempDir);
if(job1.waitForpletion(true))
{
Jobjob2=newJob(conf,"deg2friend");
job2.setJarByClass(deg2friend.class);
FileInputFormat.addInputPath(job2,tempDir);
job2.setMapperClass(job2Mapper.class);
job2.setReducerClass(job2Reducer.class);
FileOutputFormat.setOutputPath(job2,newPath(otherArgs[1]));
job2.setOutputKeyClass(Text.class);
job2.setOutputValueClass(Text.class);
FileSystem.get(conf).deleteOnExit(tempDir);
System.exit(job2.waitForpletion(true)?
0:
1);
}
System.exit(job1.waitForpletion(true)?
0:
1);
}
}
7编译代码
8将编译成的class文件打成jar包
9.将要处理的数据上传到hadoop
10.运行生成的jar包
11.查看运行的结果
七、实验小结
通过这次实验,我对MapReduce的原理有了大致了解,同时了解了一些MapReduce的使用方法,通过二度人脉算法,学会了如何使用MapReduce分析数据。
提高了对数据分析的能力。