Serialize and Deserialize Data
Updated: Apr 17, 2022
In this example, we are showing how to read data from a local CSV file into Records, serialize those records and finally, deserialize then print the output into the console.
Java Code
/* * Copyright (c) 2006-2022 North Concepts Inc. All rights reserved. * Proprietary and Confidential. Use is subject to license terms. * * https://northconcepts.com/data-pipeline/licensing/ */ package com.northconcepts.datapipeline.examples.cookbook; import java.io.File; import com.northconcepts.datapipeline.core.DataReader; import com.northconcepts.datapipeline.core.DataWriter; import com.northconcepts.datapipeline.core.StreamWriter; import com.northconcepts.datapipeline.csv.CSVReader; import com.northconcepts.datapipeline.file.FileReader; import com.northconcepts.datapipeline.file.FileWriter; import com.northconcepts.datapipeline.job.Job; public class SerializeDeserializeData { public static void main(String[] args) { File csvFile = new File("example/data/input/credit-balance-01.csv"); File binaryFile = new File("example/data/output/credit-balance-01.bin"); DataReader reader; DataWriter writer; // Serialize records to binary file reader = new CSVReader(csvFile).setFieldNamesInFirstRow(true); writer = new FileWriter(binaryFile); Job.run(reader, writer); // Deserialize records from binary file reader = new FileReader(binaryFile); writer = new StreamWriter(System.out); Job.run(reader, writer); } }
Code Walkthrough
- A CSVReader is created to read from the local file
credit-balance-01.csv
. - The serialized records are written to a binary file
credit-balance-01.bin
using FileWriter. - This binary file is the read using a FileReader.
- A StreamWriter is used to print the output to the console in human-readable format.