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
countries_with_country-code.csv
Country,Country code
Afghanistan,AF
Egypt,EG
Åland Islands,AX
Albania,AL
Algeria,DZ
Andorra,AD
Angola,AO
Anguilla,AI
Antarctica,AQ
.
.
.
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")) .setFieldNamesInFirstRow(true); MapcountryCodeMap = 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
- A
DataReader
is initialized to read values from the local CSV filecountries_with_country-code.csv
. setFieldNamesInFirstRow(true)
is invoked so that thereader
picks the headers from the first row of the CSV.- A
HashMap
is created to hold the values from thereader
created above. - A
DataWriter
is initialized through a MapWriter which takes three parameters"Country"
as key,"Country code"
as value andcountryCodeMap
as the HashMap which holds the data. - 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
.
.
.