정리 조금/Basics

parquet

Turtle0105 2024. 4. 22. 20:28

Intro

Parquet은 일반적으로 herringbone 스타일 패턴(V shape)으로 짜여진 바닥 나무 블록이다.

컴퓨터, 특히 data science에서 parquet은 종종 코드를 보다보면 데이터 포맷으로 등장하는데, 하둡에서 많이 사용되는 column-oriented 방식으로 압축 및 저장하는 형태이다. 이 방식은 기존의 방식보다 데이터를 효율적으로 처리 가능하다.

 

Google research에서 2010년에 발표된 방법론 , " Dremel: Interactive Analysis of Web-Scale Datasets ", 에 영감을 받았다고 한다.

Parquet

Row-oriented & Column-oriented

Parquet는 column-oriented 방법으로 row-oriented의 압축 방식을 따르고 있는 SQL과 같은 기존의 서비스보다 압축률이 높은 데이터 포맷이다. 그림으로 보자면 아래와 같은 차이가 있다.

 

 

Parquet가 압축률이 높은 이유는, 묶여 저장되어지는 자료의 형태가 유사하여 최적화가 잘 되어있기 때문이다. 또한 데이터 분석에 있어서 row-oriented 보다 column-oriented 저장 방식이 더 직관적일 수 있다. 가령, sample이 20억개가 있는 경우, income에 대한 데이터 분석 시 row-oriented database에서는 전체 20억개 자료의 모든 열를 다 읽어야 하는 반면, column-oriented에서는 필요한 열만 읽으면 되는 경우가 있을 수 있다. 여러모로 time & space complexity에 있어 장점이 많은 방식이다.

 

하지만, data를 추가할 때에는 기존의 방식인  row-oriented 방식이 더 좋다고 할 수 있다. 기존의 방식에서 database의 가장 끝부분에 새로운 data를 추가하면 되는 간단한 일이지만, column-oriented에서 이 작업은 조금 더 복잡해진다. 또한, data loading시 추가된 기간으로 trigger를 주거나 한다면 마찬가지로 한번 더 작업이 진행되어야 한다.

 

때문에, row-oriented 방식은 쓰는일이 잦은 OLTP (online transaction processing) system에 적합하며, column-oriented 방식은 읽기가 잦은 OLAP (online analytical processing) 또는 WORM (Write Once, Ready Many)system에 더 적합하다.

 

참고로 Hybid 방식도 존재한다고 한다.

Structure

Parquet은 위와같이 header + block(s) + footer 구조로 이루어져 있다. 

 

Header의 경우에는 parquet indicator 역할을 하는 magic number "PAR1"이 적혀있다.

 

Block의 경우는 여러개가 있어도 되는데, 각각의 block은 여러 column chunck를 담고 있다.

Column chunk는 각 column의 data를 담고있는 덩어리로 여러 page로 이루어져 있다.

Page는 type별로 data 정보를 담는 parquet에서 가장 작은 단위조각이다.

여기서 row group은 total data의 row-wise subset으로 block 세트 전체가 원본 dataset이라 이해하면 된다.

 

Footer는여러 종류의 metadata를 담고있는 부분이다.

 

Example Code

Python에서 사용되는 경우, 일반적인 data frame을 parquet으로 바꾸어 저장할때 간단히 아래와 같이 하면된다.

import pandas as pd

data_example = pd.DataFrame({'ID':['A00001', 'A00002', 'A00003'],
				   'Name':['Alice', 'Bob', 'Charlie'],
				   'Income':[50000, 100000, 73000],
                   'BMI':[25, 19.5, 17],
                   'AFP':[22, 10, 13]})

data_example.to_parquet('data_example.parquet')

그리고 실무적인 단점으로는 고차원 자료형으로는 사용이 불가하다는 것이있다. 딱 list로만 사용 가능! 때문에 고차원 배열을 사용하고싶다면, hdf5를 사용해야한다.

Outro

사실 더 깊게 들어가면, encryption, indexing ... 여러 방법들이 더 소개된다. 2번째 reference에 자세히 나와있으니 참고!

 

또한 on disk data format을 포함한 여러 형태의 data format, csv, hdfs ... 을 비교한 결과도 있다. 그중에 나에게는 이 방법이 가장 좋더라는...! 가장 마지막 reference에 자세히 나와있다!

References

https://research.google/pubs/dremel-interactive-analysis-of-web-scale-datasets-2/

 

Dremel: Interactive Analysis of Web-Scale Datasets

Distributed Systems and Parallel Computing

research.google

https://parquet.apache.org/docs/file-format/

 

File Format

Documentation about the Parquet File Format.

parquet.apache.org

https://karthiksharma1227.medium.com/understanding-parquet-and-its-optimization-opportunities-c7265a360391

 

Understanding Parquet and its Optimization opportunities

Introduction to Parquet

karthiksharma1227.medium.com

https://dkharazi.github.io/blog/parquet

 

Data Science

Personal Site

dkharazi.github.io

https://uiandwe.tistory.com/1322

 

parquet

데이터를 쉽게 접근할 수 있도록 csv로 저장해서 사용한다. 하지만 csv는 메타데이터를 저장할 수 없어 칼럼 별로 dtype을 다시 지정해줘야 하는 일이 생기며, 읽고 쓸 때 시간이 많이 걸린다는 단

uiandwe.tistory.com

https://towardsdatascience.com/the-best-format-to-save-pandas-data-414dca023e0d

 

The Best Format to Save Pandas Data

A small comparison of various ways to serialize a pandas data frame to the persistent storage

towardsdatascience.com

 

'정리 조금 > Basics' 카테고리의 다른 글

Molecular Descriptor  (0) 2024.05.02
Tanimoto Coefficient  (0) 2024.05.02
Deep Copy & Shallow Copy  (0) 2024.03.11
Macro & Micro Averaging  (3) 2024.02.29
Inner & Outer Product  (0) 2024.02.15