Explaining MapReduce Through Basic Java Codes

Step 1: Java Project

Step 2: Create Folder File Structure

WordCountProject/
├── input/
├── output/
└── src/
     └── main/
          └── java/
               └── WordCount.java

Generate the structure:

mkdir -p input
touch src/input.csv
mkdir -p output
mkdir -p src/main/java
touch src/main/java/WordCount.java

Step 3: Add Content to Java File

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class WordCount {
    public static void main(String[] args) {
        // Check if the correct number of arguments is provided
        if (args.length < 2) {
            System.err.println("Usage: java WordCount <input_file> <output_file>");
            System.exit(1);
        }

        String inputFilePath = args[0];  // First argument: input CSV file
        String outputFilePath = args[1]; // Second argument: output CSV file

        // Step 1: Map phase
        Map<String, Integer> wordCountMap = new HashMap<>();

        // Read the input CSV file
        try (BufferedReader br = new BufferedReader(new FileReader(inputFilePath))) {
            String line;
            while ((line = br.readLine()) != null) {
                String word = line.trim();
                if (!word.isEmpty()) {
                    wordCountMap.put(word, wordCountMap.getOrDefault(word, 0) + 1);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        // Step 2: Write results to output CSV file
        try (BufferedWriter bw = new BufferedWriter(new FileWriter(outputFilePath))) {
            bw.write("Word,Count\n"); // Write header
            for (Map.Entry<String, Integer> entry : wordCountMap.entrySet()) {
                bw.write(entry.getKey() + "," + entry.getValue() + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("Word count results written to " + outputFilePath);
    }
}

Step 4: Add Content to Input CSV File

apple
banana
apple
orange
banana
apple
grape
kiwi
banana
orange
peach
apple
grape
kiwi
banana
kiwi
peach
grape
apple
banana
orange
kiwi
peach
apple
banana
grape
apple
orange
banana
peach
kiwi
apple
grape
banana
kiwi
orange
peach
banana
apple
kiwi
grape
orange
kiwi
peach
banana
apple
banana
grape
kiwi
orange
peach
banana
apple
grape
kiwi
orange
peach
banana
apple
kiwi
grape
orange
banana
apple
kiwi
grape
orange
peach
kiwi
apple
banana
orange
kiwi
grape
peach
banana
kiwi
apple
orange
banana
peach
kiwi
grape
apple
banana
orange
kiwi
peach

Step 5: Compile the Java File

javac src/main/java/WordCount.java

Step 6: Run the Java File

java -cp src/main/java WordCount input/input.csv output/output.csv

Step 7: Observe the output File

Word,Count
banana,18
orange,13
apple,16
kiwi,17
peach,12
grape,12