Excel 파일 읽기 및 스트리밍

Azure Databricks는 .xls.xlsx 파일 읽기를 기본적으로 지원하므로 외부 라이브러리나 수동 파일 변환이 필요하지 않습니다. 여러 시트 통합 문서에서 시트를 읽고, 특정 셀 범위를 대상으로 지정하고, 스키마 및 데이터 형식을 자동으로 유추하고, 수식 값을 계산된 결과로 사용할 수 있습니다. Excel 파일은 클라우드 스토리지에서 읽거나 데이터 추가 UI에서 직접 업로드할 수 있으며 자동 로더를 사용하여 일괄 처리 및 스트리밍 워크로드를 모두 지원합니다.

필수 조건

Excel 파일을 읽고 스트리밍하려면 스트리밍 워크로드에 Databricks Runtime 17.1 이상 및 자동 로더가 필요합니다.

옵션

.option().options()DataFrameReader 메서드를 사용하여 Excel 데이터 원본을 구성하세요. 지원되는 옵션의 전체 목록은 Excel 옵션 및 DataFrameReader Excel 옵션을 참조 DataFrameWriter 하세요.

Usage

다음 예제에서는 Spark 일괄 처리(spark.read) 및 스트리밍 API를 사용하여 Excel 파일을 읽는 방법을 보여 줍니다. 기본적으로 파서는 왼쪽 위에서 첫 번째 시트의 비어있지 않은 오른쪽 아래 셀까지 모든 셀을 읽습니다. dataAddress 옵션을 사용하여 특정 시트 또는 셀 범위를 대상으로 지정합니다. 스키마는 자동으로 유추되거나 사용자 고유의 스키마를 지정할 수 있습니다.

UI에서 테이블 만들기 또는 수정

테이블 만들기 또는 수정 UI를 사용하여 Excel 파일에서 테이블을 만들 수 있습니다. 먼저 Excel 파일을 업로드하거나볼륨 또는 외부 위치에서 Excel 파일을 선택하세요. 시트를 선택하고 머리글 행 수를 조정한 다음 선택적으로 셀 범위를 지정합니다. UI는 선택한 파일 및 시트에서 단일 테이블을 만들 수 있습니다.

Excel 파일 읽기

또는 SQL spark.read.excel 의 함수를 사용하여 read_files 클라우드 스토리지(예: S3, ADLS)에서 Excel 파일을 읽을 수 있습니다.

Python

# Read the first sheet from a single Excel file or from multiple Excel files in a directory
df = (spark.read.excel(<path to excel directory or file>))

# Infer schema field name from the header row
df = (spark.read
       .option("headerRows", 1)
       .excel(<path to excel directory or file>))

# Read a specific sheet and range
df = (spark.read
       .option("headerRows", 1)
       .option("dataAddress", "Sheet1!A1:E10")
       .excel(<path to excel directory or file>))

SQL

-- Read an entire Excel file
CREATE TABLE my_table AS
SELECT * FROM read_files(
  "<path to excel directory or file>",
  schemaEvolutionMode => "none"
);

-- Read a specific sheet and range
CREATE TABLE my_sheet_table AS
SELECT * FROM read_files(
  "<path to excel directory or file>",
  format => "excel",
  headerRows => 1,
  dataAddress => "Sheet1!A2:D10",
  schemaEvolutionMode => "none"
);

자동 로더를 사용하여 Excel 파일 스트리밍

cloudFiles.formatexcel 설정하여 자동 로더를 사용하여 Excel 파일을 스트리밍할 수 있습니다. 다음은 그 예입니다.

df = (
  spark
    .readStream
    .format("cloudFiles")
    .option("cloudFiles.format", "excel")
    .option("cloudFiles.inferColumnTypes", True)
    .option("headerRows", 1)
    .option("cloudFiles.schemaLocation", "<path to schema location dir>")
    .option("cloudFiles.schemaEvolutionMode", "none")
    .load(<path to excel directory or file>)
)
df.writeStream
  .format("delta")
  .option("mergeSchema", "true")
  .option("checkpointLocation", "<path to checkpoint location dir>")
  .table(<table name>)

COPY INTO 사용하여 Excel 파일 수집

COPY INTO를 사용하여 클라우드 스토리지의 Excel 파일을 Delta 테이블에 멱등 방식으로 로드합니다.

CREATE TABLE IF NOT EXISTS excel_demo_table;

COPY INTO excel_demo_table
FROM "<path to excel directory or file>"
FILEFORMAT = EXCEL
FORMAT_OPTIONS ('mergeSchema' = 'true')
COPY_OPTIONS ('mergeSchema' = 'true');

시트 목록

listSheets 작업을 사용하여 Excel 파일의 시트를 나열할 수 있습니다. 반환된 스키마는 다음 필드가 있는 스키마입니다 struct .

  • sheetIndex:길게
  • sheetName: 문자열

다음은 그 예입니다.

Python

# List the name of the Sheets in an Excel file
df = (spark.read.format("excel")
       .option("operation", "listSheets")
       .load(<path to excel directory or file>))

SQL

SELECT * FROM read_files("<path to excel directory or file>",
  schemaEvolutionMode => "none",
  operation => "listSheets"
)

복잡하게 구조화되지 않은 Excel 시트를 분석합니다.

복잡한 비구조적 Excel 시트(예: 시트당 여러 테이블, 데이터 아일랜드)의 경우 Databricks는 dataAddress 옵션을 사용하여 Spark DataFrames를 만드는 데 필요한 셀 범위를 추출하는 것이 좋습니다.

df = (spark.read.format("excel")
       .option("headerRows", 1)
       .option("dataAddress", "Sheet1!A1:E10")
       .load(<path to excel directory or file>))

제한점

  • 암호로 보호된 파일은 지원되지 않습니다.
  • 하나의 헤더 행만 지원됩니다.
  • 병합된 셀 값은 왼쪽 위 셀만 채웁다. 나머지 자식 셀은 NULL로 설정됩니다.
  • 자동 로더를 사용하는 스트리밍 Excel 파일은 지원되지만 스키마 진화는 지원되지 않습니다. schemaEvolutionMode="None"를 명시적으로 설정해야 합니다.
  • "Strict Open XML 스프레드시트(Strict OOXML)"는 지원되지 않습니다.
  • 파일의 .xlsm 매크로 실행은 지원되지 않습니다.
  • ignoreCorruptFiles 옵션이 지원되지 않습니다.

자주 묻는 질문(FAQ)

Lakeflow Connect의 Excel 커넥터에 대한 질문과 대답을 찾습니다.

모든 시트를 한 번에 읽을 수 있나요?

파서는 Excel 파일에서 한 번에 하나의 시트만 읽습니다. 기본적으로 첫 번째 시트를 읽습니다. 옵션을 사용하여 다른 시트를 dataAddress 지정할 수 있습니다. 여러 시트를 처리하려면 먼저 옵션을 operation설정 listSheets 하여 시트 목록을 검색한 다음 시트 이름을 반복하고 옵션에 dataAddress 해당 이름을 제공하여 각 시트를 읽습니다.

복잡한 레이아웃 또는 시트당 여러 테이블이 있는 Excel 파일을 수집할 수 있나요?

기본적으로 파서는 왼쪽 위 셀에서 비어있지 않은 오른쪽 아래 셀까지 모든 Excel 셀을 읽습니다. 옵션을 사용하여 dataAddress 다른 셀 범위를 지정할 수 있습니다.

수식과 병합된 셀은 어떻게 처리합니까?

수식은 계산된 값으로 처리됩니다. 병합된 셀의 경우 왼쪽 위 값만 유지됩니다(자식 셀은 NULL).

자동 로더 및 스트리밍 작업에서 Excel 수집을 사용할 수 있나요?

예, cloudFiles.format = "excel" 사용하여 Excel 파일을 스트리밍할 수 있습니다. 그러나 스키마 진화는 지원되지 않으므로 "schemaEvolutionMode""None"로 설정해야 합니다.

암호로 보호된 Excel 지원되는가요?

아니요. 이 기능이 워크플로에 중요한 경우 Databricks 계정 담당자에게 문의하세요.

추가 리소스

  • CSV 파일 읽기 및 쓰기: 데이터 원본을 CSV로 내보낼 수 있는 경우 CSV는 더 광범위한 도구 지원과 전용 파서에 대한 종속성이 없는 간단한 형식입니다.