Sequence File: Reading & Writing

This lesson demonstrates programmatically reading and writing a sequence file.

We'll cover the following

Sequence File: Reading & Writing

In this lesson, we write and read sequence files. We define a class representing a car, create objects of the car, and then store them in a sequence file. Our class Car is written in Java and implements the interface Writeable. Implementing the interface allows us to serialize an object of the class as a key/value pair into the sequence file. We can choose the key to be the car name and the corresponding value as the car object. The key is represented by the class Text while the value is represented by the class Car. Both implement the Writeable interface. Create the sequence file writer object as follows:

writer = SequenceFile.createWriter(fs, config, path, Text.class, Car.class, SequenceFile.CompressionType.RECORD);

We can also specify the desired compression. The complete code for the sequence file appears in the code widget below. Run the widget and a sequence file is produced.

Get hands-on with 1400+ tech skills courses.