Keyboard shortcuts

Press ← or → to navigate between chapters

Press ? to show this help

Press Esc to hide this help

DuckDB SQL로 운석 필터 만들기

단일 노드에서 SQL로 Valid 및 1950년 이상 조건을 표현할 수 있습니다. 운석 데이터를 가져오고 DuckDB를 사용할 수 있는 변환 환경을 준비합니다.

다음은 src/myproject/datasets/meteor_analysis.py의 필터 변환입니다. ROOT는 실제 프로젝트 폴더로 바꿉니다.

from transforms.api import Input, Output, transform

ROOT = "/Example Project/Meteorites"


@transform.using(
    output=Output(f"{ROOT}/meteorite_landings_cleaned"),
    meteorite_landings=Input(f"{ROOT}/meteorite_landings"),
)
def clean(ctx, output, meteorite_landings):
    connection = ctx.duckdb().conn
    filtered = connection.sql("""
        SELECT *
        FROM meteorite_landings
        WHERE nametype = 'Valid' AND year >= 1950
    """)
    output.write_table(filtered)

meteorite_landings는 decorator에서 선언한 입력 이름으로 연결에 자동 등록됩니다. connection.sql()의 relation을 바로 출력으로 쓰므로 단순 필터를 위해 Pandas로 변환할 필요가 없습니다.

이 조건은 1950년을 포함합니다. 입력 year는 수치형이어야 하며, SQL의 WHERE 조건이 true가 아닌 행은 결과에 포함되지 않습니다.

등록한 변환을 commit하고 빌드하여 출력 데이터셋을 만듭니다. 평균, 조인과 class별 여러 출력의 흐름은 튜토리얼의 계산 정의를 따릅니다.