6.手写MR框架

本文介绍了如何手写一个简单的MapReduce框架,通过HdfsWordCount案例,详细讲解了Mapper类的设计与Context的使用,帮助读者理解MapReduce的核心工作流程。
摘要由CSDN通过智能技术生成

在这里插入图片描述
myjob.properties:
IN_PATH=/mrtest/in
OUT_PATH=/mrtest/out/rs.txt
MAPPER_CLASS=com.mydemo.mr.WordCountMapper

1.HdfsWordCount:

public class HdfsWordCount {

	public static void main(String[] args) throws IOException, InterruptedException, URISyntaxException, ClassNotFoundException, InstantiationException, IllegalAccessException {
		// TODO Auto-generated method stub
		//反射,加载配置信息
		Properties pro = new Properties();
		
		pro.load(HdfsWordCount.class.getClassLoader().getResourceAsStream("myjob.properties"));
		
		Path inpath = new Path(pro.getProperty("IN_PATH"));
		
		Path outpath = new Path(pro.getProperty("OUT_PATH"));
		//构造工具类
		Context con_map = new Context();
		
		Class<?> Mapper = Class.forName(pro.getProperty("MAPPER_CLASS"));
		
		WordCountMapper Map = (WordCountMapper) Mapper.newInstance();
		
		Configuration conf = new Configuration();
		
		FileSystem fs = FileSystem.get(new URI("hdfs://192.168.232.132:9000"), conf, "root");
		
		RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(inpath, false);
		
		while(listFiles.hasNext()) {
			LocatedFileStatus file = listFiles.next();
			
			FSDataInputStream in = fs.open(file.getPath());
			
			BufferedReader br = new BufferedReader(new InputStreamReader(in, "utf-8"));
			
			String read = null;
			while((read=br.readLine())!=null) {
				Map.Mapper_map(read, con_map);
			}
			
		br.close();
		in.close();
		}
		
		Path Out = new Path("/mrtest/out");
		if(fs.exists(Out)) {
			fs.mkdirs(Out);
		}
		HashMap<Object, Object> contextMap = con_map.get_Conmap();
		
		FSDataOutputStream out1 = fs.create(outpath);
		
		//遍历hashmap
		Set<Entry<Object, Object>> entrySet = contextMap.entrySet();
		for(Entry<Object, Object> entry:entrySet) {
			//写数据
			out1.write((entry.getKey().toString()+"\t"+entry.getValue()+"\n").getBytes());
		}
		
		out1.close();
		fs.close();
		
		System.out.println("数据统计结果完成....");
		
	}

}

2.Mapper:

public interface Mapper {

	public void Mapper_map(String line, Context con);
	
}

3.WordCountMapper:

public class WordCountMapper implements Mapper {

	@Override
	public void Mapper_map(String line, Context con) {
		// TODO Auto-generated method stub
		String [] line_sq = line.split(" ");
		
		for(String k: line_sq) {
			Object key = con.read_map(k);
			if(key==null) {
				con.write_map(k, 1);
			}else {
				Object va = con.read_map(k);
				int value = (int) va;
				con.write_map(k, value+1);
				
			}
		}
	}

}

4.Context:

public class Context {

	private HashMap<Object, Object> hm = new HashMap<>();
	
	public void write_map(Object key, Object value) {
		
		hm.put(key, value);
	}
	
	public Object read_map(Object key) {
		return hm.get(key);
	}
	
	public HashMap<Object, Object> get_Conmap(){
		return hm;
	}



	
	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值