Introduction to Reading Data in Batches with Kotlin

In previous lessons, you learned how to handle datasets efficiently stored in compressed formats. Building on that foundation, today's lesson will teach you how to read and process data in batches from multiple CSV files using Kotlin. This is crucial because working with data in smaller chunks, or batches, can make your code more efficient and faster when dealing with large datasets.

Our focus in this lesson will be on a practical scenario where a dataset containing car information is spread across multiple files. You will learn to read, process, and analyze this data to extract meaningful insights, such as determining the car with the lowest price.

Understanding CSV Data Structure

In this lesson, we will work with a set of CSV files containing car data. Here's an example of the CSV format:

model,price,transmission,year,distance_traveled_km,color
Chevrolet Silverado,43725.23,Manual,2013,55504,Silver
BMW X5,5643.78,Semi-Automatic,2014,11902,Red
Honda Accord,42850.79,Manual,2010,102223,Black
BMW Series 3,53359.81,Automatic,2009,237231,Gray
...

A typical record might look like this:

  • Model: Chevrolet Silverado
  • Price: 43725.23
  • Transmission: Manual
  • Year: 2013
  • Distance Traveled (km): 55504
  • Color: Silver

These files are divided into multiple parts to allow batch processing, and understanding their structure is crucial as you learn to read and process them efficiently.

Setting Up for CSV File Batch Reading

To effectively read and process CSV files in batches, we need to set up our environment by defining the necessary classes and data structures. We will also specify the filenames for our CSV data files.

First, we'll define a Car data class to map each row of the CSV file into a Kotlin object. This class includes fields that correspond to the columns in the CSV file.

// Data class to represent a car
data class Car(
    val model: String,
    val price: Double,
    val transmission: String,
    val year: Int,
    val distanceTraveledKm: Int,
    val color: String
)

Next, we declare a list filenames that will hold the names of the CSV files we want to read. Additionally, we create a MutableList<Car> named carData to store all the car data read from these files, enabling us to process and analyze this data collectively.

// Filenames to read
val filenames = listOf("data_part1.csv", "data_part2.csv", "data_part3.csv")

// List to store all car data
val carData = mutableListOf<Car>()

By setting up these structures, we prepare to efficiently read and store data from multiple CSV files, allowing us to handle large datasets by processing information in manageable batches.

Reading Data from Each File

Now, we'll loop through each filename, read the CSV data using Kotlin's capabilities, and append it to our carData structure.

import java.io.File

// Iterate over each filename in the filenames list
for (filename in filenames) {
    // Read each line from the CSV file
    File(filename).useLines { lines ->
        lines.drop(1).forEach { line -> // Skip header
            val tokens = line.split(",")
            val car = Car(
                model = tokens[0],
                price = tokens[1].toDouble(),
                transmission = tokens[2],
                year = tokens[3].toInt(),
                distanceTraveledKm = tokens[4].toInt(),
                color = tokens[5]
            )
            carData.add(car)
        }
    }
}

In this code:

  • We read each CSV file line by line, skipping the header.
  • We split each line by commas and map the values to a Car object.
  • Each car record is added to our carData MutableList.
Finding the Car with the Lowest Price

With all data combined in carData, the next step is identifying the car with the lowest price in Kotlin.

// Check if car data is not empty before finding the lowest cost car
if (carData.isNotEmpty()) {
    // Find the car with the lowest price
    val lowestCostCar = carData.minByOrNull { it.price }

    // Output the model and price of the lowest cost car
    println("Model: ${lowestCostCar?.model}")
    println("Price: $${lowestCostCar?.price}")
} else {
    // If no valid car data is available, output an appropriate message
    println("No valid car data available.")
}

Here:

  • We use minByOrNull to find the car with the minimum price.
  • Finally, we print the model and price of the car with the lowest price.
Summary and Practice Preparation

In this lesson, you have learned how to:

  • Read data in batches from multiple CSV files using Kotlin.
  • Process the data efficiently by defining data classes and mapping CSV records to Kotlin objects.
  • Identify insights, such as the car with the lowest price, using Kotlin's collection functions to evaluate data elements.

These techniques prepare you to handle similar datasets efficiently using Kotlin. Practice these skills with exercises designed to reinforce your understanding, focusing on reactive and efficient data handling techniques with modern Kotlin libraries.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal