Azure Databricks 이진 파일을 읽고 각 파일을 파일의 원시 콘텐츠 및 메타데이터를 포함하는 단일 레코드로 변환하는 이진 파일 데이터 원본을 지원합니다. 일반적으로 다운스트림 처리 또는 ML 유추를 위해 이미지, 오디오 또는 PDF 파일과 같은 구조화되지 않은 데이터를 로드하는 데 사용됩니다. 이진 파일을 읽으려면 데이터 원본 format을 binaryFile로 지정합니다.
필수 조건
Azure Databricks 이진 파일을 사용하기 위해 추가 구성이 필요하지 않습니다.
옵션
바이너리 파일 데이터 소스를 구성하려면 DataFrameReader의 .option() 및 .options() 메서드를 사용합니다. 지원되는 옵션의 전체 목록은 Spark API 옵션 참조를 참조하세요.
출력 스키마
이진 파일 데이터 원본은 다음 열과 추가 파티션 열(있는 경우)로 구성된 DataFrame을 생성합니다.
-
path (StringType): 파일의 경로입니다. -
modificationTime (TimestampType): 파일의 마지막 수정 시간입니다. 일부 Hadoop FileSystem 구현에서는 이 매개 변수를 사용할 수 없으며 값이 기본값으로 설정될 수 있습니다. -
length (LongType): 파일의 길이(바이트)입니다. -
content (BinaryType): 파일의 콘텐츠입니다.
Usage
다음 예제에서는 Spark DataFrame API 및 SQL을 사용하여 이진 파일을 로드하고, 파일 형식별로 필터링하고, 이미지 미리 보기를 표시하고, 읽기 성능을 향상시키기 위해 델타 테이블에 저장하는 방법을 보여 줍니다.
이진 파일 읽기
Apache Spark DataFrame API를 사용하여 변환, 표시 또는 다운스트림 처리를 위해 이진 파일을 DataFrame에 로드합니다.
파이썬
df = spark.read.format("binaryFile").load("/Volumes/<catalog>/<schema>/<volume>/")
display(df)
Scala
val df = spark.read.format("binaryFile").load("/Volumes/<catalog>/<schema>/<volume>/")
df.show()
SQL
SELECT path, length, modificationTime FROM read_files(
'/Volumes/<catalog>/<schema>/<volume>/images/',
format => 'binaryFile'
)
읽기 옵션 구성
파티션 검색의 동작을 유지하면서 지정된 GLOB 패턴과 일치하는 경로가 있는 파일을 로드하려면 pathGlobFilter 옵션을 사용할 수 있습니다. 다음 코드는 파티션 검색을 사용하여 입력 디렉터리에서 모든 JPG 파일을 읽습니다.
파이썬
df = spark.read.format("binaryFile").option("pathGlobFilter", "*.jpg").load("/Volumes/<catalog>/<schema>/<volume>/images/")
Scala
val df = spark.read.format("binaryFile").option("pathGlobFilter", "*.jpg").load("/Volumes/<catalog>/<schema>/<volume>/images/")
SQL
SELECT * FROM read_files(
'/Volumes/<catalog>/<schema>/<volume>/images/',
format => 'binaryFile',
pathGlobFilter => '*.jpg'
)
파티션 검색을 무시하고 입력 디렉터리 아래에서 파일을 재귀적으로 검색하려면 recursiveFileLookup 옵션을 사용합니다. 이 옵션은 이름이 파티션 명명 체계(예: )를 따르지 않더라도 date=2019-07-01를 검색합니다.
다음 코드는 입력 디렉터리에서 모든 JPG 파일을 재귀적으로 읽고 파티션 검색을 무시합니다.
파이썬
df = (spark.read.format("binaryFile")
.option("pathGlobFilter", "*.jpg")
.option("recursiveFileLookup", "true")
.load("/Volumes/<catalog>/<schema>/<volume>/images/"))
Scala
val df = spark.read.format("binaryFile")
.option("pathGlobFilter", "*.jpg")
.option("recursiveFileLookup", "true")
.load("/Volumes/<catalog>/<schema>/<volume>/images/")
SQL
SELECT * FROM read_files(
'/Volumes/<catalog>/<schema>/<volume>/images/',
format => 'binaryFile',
pathGlobFilter => '*.jpg',
recursiveFileLookup => true
)
이미지 로드 및 표시
Databricks는 이진 파일 데이터 원본을 사용하여 이미지 데이터를 로드하는 것을 권장합니다. Databricks display 함수는 이진 데이터 원본을 사용하여 로드된 이미지 데이터 표시를 지원합니다.
로드된 모든 파일에 이미지 확장명이 있는 파일 이름이 있는 경우 이미지 미리 보기가 자동으로 활성화됩니다.
파이썬
df = spark.read.format("binaryFile").load("/Volumes/<catalog>/<schema>/<volume>/images/")
display(df) # image thumbnails are rendered in the "content" column
Scala
val df = spark.read.format("binaryFile").load("/Volumes/<catalog>/<schema>/<volume>/images/")
df.show()
SQL
SELECT * FROM read_files(
'/Volumes/<catalog>/<schema>/<volume>/images/',
format => 'binaryFile'
)
또는 문자열 값 mimeType이(가) 있는 "image/*" 옵션을 사용하여 이진 열에 주석을 추가하여 이미지 미리 보기 기능을 강제 적용할 수 있습니다. 이미지는 이진 콘텐츠의 형식 정보를 기반으로 디코딩됩니다. 지원되는 이미지 유형은 bmp, gif, jpeg 및 png입니다. 지원되지 않는 파일은 손상된 이미지 아이콘으로 표시됩니다.
파이썬
df = spark.read.format("binaryFile").option("mimeType", "image/*").load("/Volumes/<catalog>/<schema>/<volume>/images/")
display(df)
Scala
val df = spark.read.format("binaryFile").option("mimeType", "image/*").load("/Volumes/<catalog>/<schema>/<volume>/images/")
df.show()
SQL
SELECT * FROM read_files(
'/Volumes/<catalog>/<schema>/<volume>/images/',
format => 'binaryFile',
mimeType => 'image/*'
)
이미지 데이터를 처리하기 위한 권장 워크플로는 이미지 애플리케이션용 참조 솔루션을 참조하세요.
Delta 테이블에 저장
데이터를 다시 로드할 때 읽기 성능을 향상시키려면 Azure Databricks 이진 파일에서 델타 테이블로 로드된 데이터를 저장하는 것이 좋습니다.
파이썬
df.write.format("delta").saveAsTable("<catalog>.<schema>.<table>")
Scala
df.write.format("delta").saveAsTable("<catalog>.<schema>.<table>")
추가 리소스
- 이미지 파일 읽기: 워크로드에 원시 바이트가 아닌 높이, 너비 및 채널 데이터와 같은 구조적 이미지 필드가 필요한 경우 이미지 데이터 원본은 디코딩된 스키마를 제공합니다.