Write Key-Value Fields to MapWriter

Updated: Jun 7, 2023

In this example, you will learn how to load the data from a CSV file into a HashMap using DataPipeline.

A HashMap is a datatype based on key-value pair in Java that enables fast access to data that has unique keys or identifiers.

See the CSV Examples for more use-cases.


Input CSV File


Country,Country code
Åland Islands,AX


Java Code Listing

package com.northconcepts.datapipeline.examples.cookbook;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import com.northconcepts.datapipeline.core.DataReader;
import com.northconcepts.datapipeline.core.DataWriter;
import com.northconcepts.datapipeline.csv.CSVReader;
import com.northconcepts.datapipeline.job.Job;
import com.northconcepts.datapipeline.memory.MapWriter;

public class WriteKeyValueFieldsToMapWriter {

	public static void main(String[] args) {
        DataReader reader = new CSVReader(new File("example/data/input/countries_with_country-code.csv"))
        Map countryCodeMap = new HashMap<>();
        //Create MapWriter, specify key, value and map
        DataWriter writer = new MapWriter("Country", "Country code", countryCodeMap);
        Job.run(reader, writer);
        for (Map.Entry entry : countryCodeMap.entrySet()) {
            System.out.println(entry.getKey() + " --> " + entry.getValue());


Code Walkthrough

  1. A DataReader is initialized to read values from the local CSV file countries_with_country-code.csv.
  2. setFieldNamesInFirstRow(true) is invoked so that the reader picks the headers from the first row of the CSV.
  3. A HashMap is created to hold the values from the reader created above.
  4. A DataWriter is initialized through a MapWriter which takes three parameters "Country" as key, "Country code" as value and countryCodeMap as the HashMap which holds the data.
  5. Finally, the countryCodeMap can be used in a loop with its keys to output the data on the console.


Console Output

Papua New Guinea --> PG
Cambodia --> KH
Kazakhstan --> KZ
Paraguay --> PY
Åland Islands --> AX
Syria --> SY
Bahamas --> BS
Solomon Islands --> SB
Montserrat --> MS
Mobile Analytics