Data serialization (Avro, Parquet, ORC) in Hadoop - Time & Space Complexity
When working with big data, we often save data in special formats like Avro, Parquet, or ORC. Understanding how long it takes to read or write these formats helps us plan and speed up data processing.
We want to know: how does the time to serialize or deserialize data grow as the data size grows?
Analyze the time complexity of the following Hadoop code snippet that writes data using Parquet format.
Job job = Job.getInstance(conf);
job.setOutputFormatClass(ParquetOutputFormat.class);
ParquetOutputFormat.setWriteSupportClass(job, AvroWriteSupport.class);
ParquetOutputFormat.setCompression(job, CompressionCodecName.SNAPPY);
// Mapper writes records to Parquet files
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
// Convert input to Avro record
// Write record to ParquetOutputFormat
context.write(null, avroRecord);
}
This code writes many records into Parquet files using Avro schema inside Hadoop MapReduce.
Look for repeated actions that take most time.
- Primary operation: Writing each record to the Parquet file inside the map function.
- How many times: Once for every input record, so the number of records (n).
As the number of records grows, the time to write grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 writes |
| 100 | 100 writes |
| 1000 | 1000 writes |
Pattern observation: The time grows roughly in direct proportion to the number of records.
Time Complexity: O(n)
This means the time to serialize data grows linearly with the number of records.
[X] Wrong: "Serialization time stays the same no matter how much data we write."
[OK] Correct: Each record must be processed and written, so more data means more work and more time.
Knowing how serialization time grows helps you explain data pipeline speed and design better systems. It shows you understand how data size affects processing time.
"What if we changed from writing data record-by-record to writing in batches? How would the time complexity change?"
Practice
Solution
Step 1: Understand Avro's storage method
Avro stores data row-wise, meaning it saves one record at a time along with its schema.Step 2: Compare with other formats
Parquet and ORC store data column-wise, optimizing for query speed, while CSV has no schema.Final Answer:
Avro -> Option AQuick Check:
Row-wise with schema = Avro [OK]
- Confusing Parquet or ORC as row-wise
- Thinking CSV has schema
- Mixing storage orientation concepts
Solution
Step 1: Identify the output format class for Parquet
Parquet files require setting the output format to ParquetOutputFormat in Hadoop jobs.Step 2: Eliminate other options
TextOutputFormat is for text files, AvroOutputFormat for Avro files, and OrcOutputFormat for ORC files.Final Answer:
job.setOutputFormatClass(ParquetOutputFormat.class); -> Option DQuick Check:
Parquet output format = ParquetOutputFormat [OK]
- Using TextOutputFormat for Parquet files
- Confusing AvroOutputFormat with ParquetOutputFormat
- Mixing ORC and Parquet output formats
df.write.format('orc').save('/data/output')Solution
Step 1: Identify the format used in the code
The code uses df.write.format('orc'), which means data is saved in ORC format.Step 2: Understand ORC properties
ORC stores data column-wise and preserves schema for efficient queries.Final Answer:
Data saved in ORC format with schema preserved -> Option AQuick Check:
ORC format with schema = Data saved in ORC format with schema preserved [OK]
- Assuming default is CSV
- Confusing ORC with Parquet or Avro
- Ignoring schema preservation in ORC
spark.read.format('parquet').load('/data/file.orc')What is the main issue here?
Solution
Step 1: Check the file format and extension
The code tries to read a file with .orc extension using Parquet format.Step 2: Understand format compatibility
Parquet reader cannot read ORC files because they have different internal structures.Final Answer:
Parquet format cannot read ORC files -> Option BQuick Check:
Parquet reader ≠ ORC file [OK]
- Ignoring file extension mismatch
- Assuming Spark auto-detects format
- Thinking Parquet and ORC are interchangeable
Solution
Step 1: Understand the dataset and query needs
Many columns exist but only a few are needed, so reading only those columns is important.Step 2: Compare serialization formats for columnar access
Parquet stores data column-wise, enabling fast queries on selected columns and saving storage.Step 3: Eliminate other options
Avro is row-wise, so it reads all columns; CSV and JSON lack efficient columnar storage and schema.Final Answer:
Parquet, because it stores data column-wise allowing fast column queries -> Option CQuick Check:
Column-wise storage = Parquet best for selective queries [OK]
- Choosing Avro for column queries
- Picking CSV or JSON for big data efficiency
- Ignoring storage and query speed trade-offs
Ready to prove it?
Pass the test and this pattern moves unknown
