---
title: Read binary files
description: Learn how to read data from binary files using Databricks.
canonical_url: https://docs.databricks.com/aws/en/query/formats/binary
---

# Read binary files

Databricks supports the [binary file](https://spark.apache.org/docs/latest/sql-data-sources-binaryFile.html) data source, which reads binary files and converts each file into a single record containing the file's raw content and metadata. It is commonly used to load unstructured data such as images, audio, or PDF files for downstream processing or ML inference. To read binary files, specify the data source `format` as `binaryFile`.

## Prerequisites

Databricks does not require additional configuration to use binary files.

## Options

Use the `.option()` and `.options()` methods of `DataFrameReader` to configure the binary file data source. For a complete list of supported options, see [Spark API options reference](https://docs.databricks.com/aws/en/spark/api-options.md).

## Output schema

The binary file data source produces a DataFrame with the following columns, plus any partition columns:

* `path (StringType)`: The path of the file.
* `modificationTime (TimestampType)`: The modification time of the file. In some Hadoop FileSystem implementations, this parameter might be unavailable and the value would be set to a default value.
* `length (LongType)`: The length of the file in bytes.
* `content (BinaryType)`: The contents of the file.

## Usage

The following examples demonstrate loading binary files using the Spark DataFrame API and SQL, filtering by file type, displaying image previews, and saving to a Delta table for improved read performance.

### Read binary files

Use the Apache Spark DataFrame API to load binary files into a DataFrame for transformation, display, or downstream processing.

#### Python

```python
df = spark.read.format("binaryFile").load("/Volumes/<catalog>/<schema>/<volume>/")
display(df)
```

#### Scala

```scala
val df = spark.read.format("binaryFile").load("/Volumes/<catalog>/<schema>/<volume>/")
df.show()
```

#### SQL

```sql
SELECT path, length, modificationTime FROM read_files(
  '/Volumes/<catalog>/<schema>/<volume>/images/',
  format => 'binaryFile'
)
```

### Configure read options

To load files with paths matching a given glob pattern while keeping the behavior of partition discovery,
you can use the `pathGlobFilter` option. The following code reads all JPG files from the
input directory with partition discovery:

#### Python

```python
df = spark.read.format("binaryFile").option("pathGlobFilter", "*.jpg").load("/Volumes/<catalog>/<schema>/<volume>/images/")
```

#### Scala

```scala
val df = spark.read.format("binaryFile").option("pathGlobFilter", "*.jpg").load("/Volumes/<catalog>/<schema>/<volume>/images/")
```

#### SQL

```sql
SELECT * FROM read_files(
  '/Volumes/<catalog>/<schema>/<volume>/images/',
  format => 'binaryFile',
  pathGlobFilter => '*.jpg'
)
```

If you want to ignore partition discovery and recursively search files under the input directory,
use the `recursiveFileLookup` option. This option searches through nested directories
even if their names *do not* follow a partition naming scheme like `date=2019-07-01`.
The following code reads all JPG files recursively from the input directory and ignores partition discovery:

#### Python

```python
df = (spark.read.format("binaryFile")
  .option("pathGlobFilter", "*.jpg")
  .option("recursiveFileLookup", "true")
  .load("/Volumes/<catalog>/<schema>/<volume>/images/"))
```

#### Scala

```scala
val df = spark.read.format("binaryFile")
  .option("pathGlobFilter", "*.jpg")
  .option("recursiveFileLookup", "true")
  .load("/Volumes/<catalog>/<schema>/<volume>/images/")
```

#### SQL

```sql
SELECT * FROM read_files(
  '/Volumes/<catalog>/<schema>/<volume>/images/',
  format => 'binaryFile',
  pathGlobFilter => '*.jpg',
  recursiveFileLookup => true
)
```

### Load and display images

Databricks recommends that you use the binary file data source to load image data. The Databricks `display` function supports displaying image data loaded using the binary data source.

If all the loaded files have a file name with an [image extension](https://github.com/AdoptOpenJDK/openjdk-jdk11/blob/19fb8f93c59dfd791f62d41f332db9e306bc1422/src/java.base/windows/classes/sun/net/www/content-types.properties#L182-L251), image preview is automatically enabled:

#### Python

```python
df = spark.read.format("binaryFile").load("/Volumes/<catalog>/<schema>/<volume>/images/")
display(df)    # image thumbnails are rendered in the "content" column
```

#### Scala

```scala
val df = spark.read.format("binaryFile").load("/Volumes/<catalog>/<schema>/<volume>/images/")
df.show()
```

#### SQL

```sql
SELECT * FROM read_files(
  '/Volumes/<catalog>/<schema>/<volume>/images/',
  format => 'binaryFile'
)
```

![image preview](https://docs.databricks.com/aws/en/images/data-sources/binary-file-image-preview1.png)

Alternatively, you can force the image preview functionality by using the `mimeType` option with a string value `"image/*"` to annotate the binary column. Images are decoded based on their format information in the binary content. Supported image types are `bmp`, `gif`, `jpeg`, and `png`. Unsupported files appear as a broken image icon.

#### Python

```python
df = spark.read.format("binaryFile").option("mimeType", "image/*").load("/Volumes/<catalog>/<schema>/<volume>/images/")
display(df)
```

#### Scala

```scala
val df = spark.read.format("binaryFile").option("mimeType", "image/*").load("/Volumes/<catalog>/<schema>/<volume>/images/")
df.show()
```

#### SQL

```sql
SELECT * FROM read_files(
  '/Volumes/<catalog>/<schema>/<volume>/images/',
  format => 'binaryFile',
  mimeType => 'image/*'
)
```

![image preview with unsupported file type](https://docs.databricks.com/aws/en/images/data-sources/binary-file-image-preview2.png)

See [Reference solution for image applications](https://docs.databricks.com/aws/en/machine-learning/reference-solutions/images-etl-inference.md) for the recommended workflow to handle image data.

### Save to Delta table

To improve read performance when you load data back, Databricks recommends saving data loaded from binary files to a Delta table.

#### Python

```python
df.write.format("delta").saveAsTable("<catalog>.<schema>.<table>")
```

#### Scala

```scala
df.write.format("delta").saveAsTable("<catalog>.<schema>.<table>")
```

## Additional resources

* [Read image files](https://docs.databricks.com/aws/en/query/formats/image.md): If your workload requires structured image fields such as height, width, and channel data rather than raw bytes, the image data source provides a decoded schema.
