用Java来模拟Hadoop

该博客介绍了如何使用Java程序模拟Hadoop进行文件切分,并统计'学生'文件中名字包含'白'的人数。通过创建多个线程执行MapReduce任务,实现了对文件的分布式处理。Map阶段将文件读取并切分成块,统计名字含'白'的学生,Reduce阶段汇总所有结果,得出最终统计信息。

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

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

//使用java程序模拟hadoop切分文件,统计students文件中,姓名包含'白'汉字的人数。
public class HomeWork2 {
    public static void main(String[] args) {
        BufferedReader br = null;
        BufferedWriter bw = null;
        int index = 0;
        ArrayList<String> list1 = null;
        int count = 0;
        String line = null;
        int rowNum = 0;
        try {
            br = new BufferedReader(new FileReader("data/students.txt"));
            bw = new BufferedWriter(new FileWriter("blocks/block---" + index));
            list1 = new ArrayList<>();
            while ((line = br.readLine()) != null) {
                list1.add(line);
                count++;
                if (count == 140) {
                    rowNum = 128 * index;
                    for (int i = rowNum; i <= rowNum + 127; i++) {
                        String s = list1.get(i);
                        bw.write(s);
                        bw.newLine();
                        bw.flush();
                    }
                    index++;
                    count = 12;
                    bw = new BufferedWriter(new FileWriter("blocks/block---" + index));
                }
            }
            for(int i = list1.size()-count;i<list1.size();i++){
                String s = list1.get(i);
                bw.write(s);
                bw.newLine();
                bw.flush();
            }
            bw.close();
            br.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
class Map{
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(8);
        File blocks = new File("blocks");
        File[] files = blocks.listFiles();
        int index =0;
        for(File f:files){
            MapTask mapTask = new MapTask(f, index);
            executorService.submit(mapTask);
            index++;
        }
        executorService.shutdown();

    }

}
class MapTask implements Runnable{
    private  File file;
    private  int index;

    public MapTask() {
    }

    public MapTask(File file, int index) {
        this.file = file;
        this.index = index;
    }


    @Override
    public void run() {
        BufferedReader br =null;
        BufferedWriter bw =null;
        String line =null;
        HashMap<String, Integer> map = new HashMap<>();
        try {
             br = new BufferedReader(new FileReader(file));
             bw = new BufferedWriter(new FileWriter("result/res--"+index));
             while((line=br.readLine())!=null){
                 String[] split = line.split(",");
                 String name =split[1];
                 if(name.contains("白")){
                     if (!map.containsKey("白")) {
                         map.put("白", 1);
                     } else {
                         //否则value值加1
                         map.put("白", map.get("白") + 1);
                     }
                 }
             }
            Set<java.util.Map.Entry<String, Integer>> entries = map.entrySet();
             for(java.util.Map.Entry<String, Integer> m:entries){
                 String key = m.getKey();
                 Integer value = m.getValue();
                 bw.write("姓"+key+"的有"+value);
                 bw.newLine();
                 bw.flush();
             }
             br.close();
             bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
class Reduce{
    public static void main(String[] args) throws Exception {
        BufferedReader br =null;
        BufferedWriter bw =null;
        String line = null;
        HashMap<String, Integer> hashMap = new HashMap<>();
        File result = new File("result");
        File[] files = result.listFiles();
        for(File f:files){
            br = new BufferedReader(new FileReader(f));
            while((line=br.readLine())!=null){
                String[] s = line.split("有");
                String s1 = s[0];
                String s2 = s[1];
                Integer i = Integer.valueOf(s2);
                if(!hashMap.containsKey(s1)){
                    hashMap.put(s1,i);
                }else{
                    hashMap.put(s1,hashMap.get(s1)+i);
                }
            }
            Set<java.util.Map.Entry<String, Integer>> entries = hashMap.entrySet();
            bw =new BufferedWriter(new FileWriter("result/total"));
            for(java.util.Map.Entry<String, Integer> m:entries){
                String key = m.getKey();
                Integer value = m.getValue();
                String num = String.valueOf(value);
                bw.write(key+"有"+num+"人");
                bw.newLine();
                bw.flush();
            }
            br.close();
            bw.close();
        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值