Wednesday, February 3, 2021

How to use pyarrow to view the metadata information inside a Parquet file

Goal:

This article explains how to use PyArrow to view the metadata information inside a Parquet file.

Env:

CentOS 7

Solution:

1. Create a Python 3 virtual environment

This step is because the default python version is 2.x on CentOS/Redhat 7 and it is too old to install pyArrow latest version. 

Using Python 3 and its pip3 is the way to go.

However if we just use "alternatives" to config the python to use python3, it may break some other tools such as "yum" which depends on python2.

Using virtual environment is the easiest way to keep both python2 and python3 on CentOS 7.

python3 -m venv .venv
. .venv/bin/activate

 2. Install PyArrow and its dependencies

pip install --upgrade pip setuptools
pip install Cython
pip install pyarrow

3.  Read the metadata inside a Parquet file

>>> import pyarrow.parquet as pq
>>> parquet_file = pq.ParquetFile('/.../part-00000-67861019-20bb-4396-96f8-146141351ff2-c000.snappy.parquet')

>>> parquet_file.metadata
<pyarrow._parquet.FileMetaData object at 0x7f8014250bf8>
created_by: parquet-mr version 1.10.1 (build a89df8f9932b6ef6633d06069e50c9b7970bebd1)
num_columns: 10
num_rows: 546097
num_row_groups: 1
format_version: 1.0
serialized_size: 1886

>>> parquet_file.metadata.row_group(0)
<pyarrow._parquet.RowGroupMetaData object at 0x7f808aaf4f98>
num_columns: 10
num_rows: 546097
total_byte_size: 17515040

>>> parquet_file.metadata.row_group(0).column(3)
<pyarrow._parquet.ColumnChunkMetaData object at 0x7f801356cf48>
file_offset: 6588315
file_path:
physical_type: INT64
num_values: 546097
path_in_schema: Index
is_stats_set: True
statistics:
<pyarrow._parquet.Statistics object at 0x7f8013fd2ea8>
has_min_max: True
min: 0
max: 396316
null_count: 0
distinct_count: 0
num_values: 546097
physical_type: INT64
logical_type: None
converted_type (legacy): NONE
compression: SNAPPY
encodings: ('BIT_PACKED', 'RLE', 'PLAIN')
has_dictionary_page: False
dictionary_page_offset: None
data_page_offset: 6588315
total_compressed_size: 2277936
total_uncompressed_size: 4369155

>>> parquet_file.metadata.row_group(0).column(3).statistics
<pyarrow._parquet.Statistics object at 0x7f801356cef8>
has_min_max: True
min: 0
max: 396316
null_count: 0
distinct_count: 0
num_values: 546097
physical_type: INT64
logical_type: None
converted_type (legacy): NONE

From above information, we can tell that:

  • The parquet file version is 1.10.1.
  • It has only 1 row group inside.
  • It has 10 columns and 546097 rows.
  • The 4th column(.column(3)) named “Index” is a INT64 type with min=0 and max=396316.


7 comments:

  1. Replies
    1. This article provides a practical introduction to inspecting Parquet file metadata with PyArrow, covering useful details such as row groups, column chunks, compression, encodings, and file-level statistics. The step-by-step approach makes it easier to understand what information is stored inside a Parquet file.

      The focus on Parquet structure and metadata makes Data Engineering Training particularly relevant, especially for understanding how columnar data formats and storage characteristics fit into data engineering workflows.

      Delete
  2. The Python-based implementation also makes Python Programming Training a useful related resource, as the article demonstrates creating a virtual environment, installing PyArrow, and using Python code to inspect the file metadata.

    ReplyDelete
  3. Because Parquet is commonly used for handling large-scale datasets, the discussion of row groups, compression, column sizes, and storage metadata connects naturally with Big Data Projects and practical big-data processing scenarios.

    ReplyDelete
  4. The metadata also exposes useful column statistics such as minimum and maximum values, null counts, and the number of values. These details provide a helpful connection to Data Analysis Training, particularly when examining the characteristics of structured datasets before further processing.

    ReplyDelete