Skip to content
Son's blog
All writing

Spark and CRC

Introduction

The question we want to answer in this blog is WHY and HOW does Apache Spark use CRC?

Why

Ensuring Data Integrity

many ways data can be corrupted while uploading or downloading between nodes, systems

CRC32C is recommended over MD5 for data integrity check.

Implementation

How

Why Spark generates extra CRC files while Parquet already has checksum in data file metadata?

Example

val data = Seq(
  (1, "apple", 2.99),
  (2, "banana", 1.99),
  (3, "orange", 0.99)
)
val df = sparkSession.createDataFrame(data).toDF("id", "name", "price")
// comment out coalesce(1) to write to 3 partitions
df
  .coalesce(1)
  .write
  .option("compression", "snappy")
  .parquet("parquet-one-partition")

CRC files created by Spark are separate one per parquet file. CRC checksum of parquet is in footer of e.g. part-00000-68d027c3-f55c-4aef-8854-37442413837b-c000.snappy.parquet

parquet-one-partition
├── ._SUCCESS.crc
├── .part-00000-68d027c3-f55c-4aef-8854-37442413837b-c000.snappy.parquet.crc
├── _SUCCESS
└── part-00000-68d027c3-f55c-4aef-8854-37442413837b-c000.snappy.parquet

❯ hexdump .part-00000-f4cdf865-dd87-4edf-838e-98b752039c43-c000.snappy.parquet.crc
0000000 63 72 63 00 00 00 02 00 1b 8a c6 7a b5 a1 39 52
0000010


parquet-3-partition
├── ._SUCCESS.crc
├── .part-00000-f4cdf865-dd87-4edf-838e-98b752039c43-c000.snappy.parquet.crc
├── .part-00001-f4cdf865-dd87-4edf-838e-98b752039c43-c000.snappy.parquet.crc
├── .part-00002-f4cdf865-dd87-4edf-838e-98b752039c43-c000.snappy.parquet.crc
├── _SUCCESS
├── part-00000-f4cdf865-dd87-4edf-838e-98b752039c43-c000.snappy.parquet
├── part-00001-f4cdf865-dd87-4edf-838e-98b752039c43-c000.snappy.parquet
└── part-00002-f4cdf865-dd87-4edf-838e-98b752039c43-c000.snappy.parquet

❯ hexdump .part-00000-68d027c3-f55c-4aef-8854-37442413837b-c000.snappy.parquet.crc
0000000 63 72 63 00 00 00 02 00 42 73 a8 0c 96 28 2f 65
0000010

63 72 63 -> crc (hexadecimal representation of ASCII code)
00 00 00 02 00 -> beginning of crc32???
42 73 a8 0c 96 28 2f 65 -> 4-byte crc checksum value?? here it's 8 Byte

Reference

Appendix

Concepts

Apache Parquet

How does Parquet support checksum?

Source: Parquet 1.x Footer Content

Parquet File Format parquet-file-format.png

Parquet Footer Structure parquet_footer_structure.png