MapReduce部分API编程练习(好友推荐)

1、主方法

package com.bjsxt.FOF;

import java.io.IOException;

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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class MyFOF {
	public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
		Configuration conf=new Configuration(true);
		Job job=Job.getInstance(conf);
		job.setJarByClass(MyFOF.class);
		job.setJobName("ooxx");
		
		Path filein=new Path("/user/local");
		FileInputFormat.addInputPath(job, filein);
		
		Path fileout=new Path("/data");
		if(fileout.getFileSystem(conf).exists(fileout)) {
			fileout.getFileSystem(conf).delete(fileout);
		}
		FileOutputFormat.setOutputPath(job, fileout);
		
//		job.setInputFormatClass(cls);
		
		job.setMapperClass(MyMapper.class);
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(IntWritable.class);
		
//		job.setPartitionerClass(MyPartitioner.class);
//		job.setSortComparatorClass(MyComparator.class);
//		job.setGroupingComparatorClass(MyGrouping.class);
		
//		job.setCombinerClass(MyCombiner.class);
		
		job.setReducerClass(MyReducer.class);
		job.setNumReduceTasks(2);
		
		job.waitForCompletion(true);		
	}
}

2、map方法

package com.bjsxt.FOF;

import java.io.IOException;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.util.StringUtils;

public class MyMapper extends Mapper<LongWritable, Text, Text, IntWritable>{
	Text mkey=new Text();
	IntWritable mval=new IntWritable();
	@Override
	protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context)
			throws IOException, InterruptedException {
		
		//相同的key为一组,这一组key调用一次reduce方法
		//value::::tom hello hadoop cat
		String[] split = StringUtils.split(value.toString(),' ');
		for(int i=1;i<split.length;i++) {
			mkey.set(getfof(split[0],split[i]));
			mval.set(0);
			context.write(mkey, mval);
			
			for(int j=i+1;j<split.length;j++) {
				mkey.set(getfof(split[i],split[j]));
				mval.set(1);
				context.write(mkey, mval);
			}
		}
	}
	public static String getfof(String s1,String s2) {
		if(s1.compareTo(s2)<0) {
			return s1+":"+s2;
		}else {
			return s2+":"+s1;
		}
	}

}

3、reduce方法

package com.bjsxt.FOF;

import java.io.IOException;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.MRBench.Reduce;
import org.apache.hadoop.mapreduce.Reducer;

public class MyReducer extends Reducer<Text, IntWritable, Text, IntWritable>{
	
	IntWritable rval=new IntWritable();
	@Override
	protected void reduce(Text key, Iterable<IntWritable> values,
			Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
		//相同的key为一组,这一组数据调用一次reduce方法
		//方法内迭代这一组数据
		//hadoop:hadoop 0
		int flag=0;
		int sum=0;
		for(IntWritable v:values) {
			if(v.get()==0) {
				flag=1;
			}
			sum+=v.get();
			if(flag==0) {
				rval.set(sum);
				context.write(key, rval);
			}			
		}	
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MapReduce是一种用于处理大规模数据集的编程模型和软件框架。它可以将大规模数据集分解成多个小的数据块,并在分布式计算集群上进行并行处理。二度好友推荐是一种基于社交网络的推荐算法,它可以根据用户的好友关系,推荐用户可能认识但尚未添加为好友的人。 在MapReduce中实现二度好友推荐算法的一种常见方法是使用两个MapReduce作业。第一个作业用于计算用户的直接好友关系,第二个作业用于计算用户的二度好友关系。 以下是一个简单的示例,演示了如何使用MapReduce实现二度好友推荐算法: 1. 第一个作业(计算直接好友关系): ```python # Mapper函数 def mapper1(key, value): user, friends = value.split("\t") for friend in friends.split(","): yield (user, friend), 1 # Reducer函数 def reducer1(key, values): yield key, sum(values) # 执行第一个作业 input_data = # 输入数据,格式为:user1\tfriend1,friend2,friend3... output_data1 = map_reduce(input_data, mapper1, reducer1) ``` 2. 第二个作业(计算二度好友关系): ```python # Mapper函数 def mapper2(key, value): user1, user2 = key yield (user1, user2), value # Reducer函数 def reducer2(key, values): user1, user2 = key mutual_friends = [] for value in values: mutual_friends.append(value) yield (user1, user2), mutual_friends # 执行第二个作业 output_data2 = map_reduce(output_data1, mapper2, reducer2) ``` 请注意,上述示例中的`map_reduce`函数是一个抽象的函数,用于执行MapReduce作业。具体的实现细节可以根据使用的编程语言和框架进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值