MapReduce:基于物品的协同过滤算法的MapReduce实现

本文详细介绍了如何使用MapReduce框架实现基于用户相似性函数的算法,包括文件初始化、读取输入数据、执行Map和Reduce操作,以及输出结果的过程。重点在于通过MapReduce的分而治之策略,高效地处理大规模数据并计算用户间的相似度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

基于用户的相似性函数的定义:


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


public class MapperReduce {
	String input="F:\\input.txt";
	String output="F:\\output.txt";
	public void set_input(String input){
		this.input=input;
	}
	public void set_output(String output){
		this.output=output;
	}
	public void clear_tmpfile() throws IOException{
		FileWriter fw=new FileWriter("F:\\map_result.txt"); 
		String line="";
		fw.write(line);
		fw.close();
	}
	public MapperReduce(String input,String output){//文件初始化,如果存在则删除
		this.input=input;
		this.output=output;
		File file=new File("F:\\map_result.txt");
		if(file.exists()){
			file.delete();
		}
		File file1=new File(this.output);
		if(file1.exists()){
			file1.delete();
		}
	}
	
	public Map<String,List<String>> shuff() throws IOException{//shuffle过程,将mapper处理结果相同key的记录合并
		BufferedReader br=new BufferedReader(new FileReader("F:\\map_result.txt"));
        String line="";
        Map<String, List<String>> map=new HashMap<String, List<String>>();
        while((line=br.readLine())!=null){
        	String[] content=line.split(" ",2);
        	if(map.containsKey(content[0])){
        		List<String> list=new ArrayList<String>();
        		list=map.get(content[0]);
        		if(content.length==2){
        		    list.add(content[1]);
        		}else{
        			list.add(" ");
        		}
        		map.remove(content[0]);
        		map.put(content[0],list);
        	}else{
        			List<String> lis
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值