---
title: File metadata column
description: Access metadata about source data files by querying the hidden \_metadata column.
canonical_url: https://docs.databricks.com/aws/en/ingestion/file-metadata-column
---

# File metadata column

You can get metadata information for input files with the `_metadata` column. The `_metadata` column is a *hidden* column, and is available for all input file formats. To include the `_metadata` column in the returned DataFrame, you must explicitly select it in the read query where you specify the source.

If the data source contains a column named `_metadata`, queries return the column from the data source, and not the file metadata.

> [!WARNING]
>
> New fields might be added to the `_metadata` column in future releases. To prevent schema evolution errors if the `_metadata` column is updated, Databricks recommends selecting specific fields from the column in your queries. See [examples](https://docs.databricks.com/aws/en/ingestion/file-metadata-column.md#metadata-examples).

## Supported metadata

The `_metadata` column is a `STRUCT` containing the following fields:

| Name                         | Type        | Description                                      | Example               | Minimum Databricks Runtime release |
| ---------------------------- | ----------- | ------------------------------------------------ | --------------------- | ---------------------------------- |
| **file\_path**               | `STRING`    | File path of the input file.                     | `file:/tmp/f0.csv`    | 10.5                               |
| **file\_name**               | `STRING`    | Name of the input file along with its extension. | `f0.csv`              | 10.5                               |
| **file\_size**               | `LONG`      | Length of the input file, in bytes.              | 628                   | 10.5                               |
| **file\_modification\_time** | `TIMESTAMP` | Last modification timestamp of the input file.   | `2021-12-20 20:05:21` | 10.5                               |
| **file\_block\_start**       | `LONG`      | Start offset of the block being read, in bytes.  | 0                     | 13.0                               |
| **file\_block\_length**      | `LONG`      | Length of the block being read, in bytes.        | 628                   | 13.0                               |

For fetching additional cloud object-level properties, see [Object metadata column](https://docs.databricks.com/aws/en/ingestion/object-metadata-column.md).

## <a id="metadata-examples" />Examples

### Use in a basic file-based data source reader

#### Python

```python
df = spark.read \
  .format("csv") \
  .schema(schema) \
  .load("/Volumes/catalog_name/schema_name/volume_name/data/*") \
  .select("*", "_metadata")

display(df)

'''
Result:
+---------+-----+----------------------------------------------------+
|   name  | age |                 _metadata                          |
+=========+=====+====================================================+
|         |     | {                                                  |
|         |     |    "file_path": "/Volumes/catalog_name/            |
| Debbie  | 18  |      schema_name/volume_name/data/f0.csv",         |
|         |     |    "file_name": "f0.csv",                          |
|         |     |    "file_size": 12,                                |
|         |     |    "file_block_start": 0,                          |
|         |     |    "file_block_length": 12,                        |
|         |     |    "file_modification_time": "2021-07-02 01:05:21" |
|         |     | }                                                  |
+---------+-----+----------------------------------------------------+
|         |     | {                                                  |
|         |     |    "file_path": "/Volumes/catalog_name/            |
| Frank   | 24  |      schema_name/volume_name/data/f1.csv",         |
|         |     |    "file_name": "f1.csv",                          |
|         |     |    "file_size": 12,                                |
|         |     |    "file_block_start": 0,                          |
|         |     |    "file_block_length": 12,                        |
|         |     |    "file_modification_time": "2021-12-20 02:06:21" |
|         |     | }                                                  |
+---------+-----+----------------------------------------------------+
'''
```

#### Scala

```scala
val df = spark.read
  .format("csv")
  .schema(schema)
  .load("/Volumes/catalog_name/schema_name/volume_name/data/*")
  .select("*", "_metadata")

display(df)

/* Result:
+---------+-----+----------------------------------------------------+
|   name  | age |                 _metadata                          |
+=========+=====+====================================================+
|         |     | {                                                  |
|         |     |    "file_path": "/Volumes/catalog_name/            |
| Debbie  | 18  |      schema_name/volume_name/data/f0.csv",         |
|         |     |    "file_name": "f0.csv",                          |
|         |     |    "file_size": 12,                                |
|         |     |    "file_block_start": 0,                          |
|         |     |    "file_block_length": 12,                        |
|         |     |    "file_modification_time": "2021-07-02 01:05:21" |
|         |     | }                                                  |
+---------+-----+----------------------------------------------------+
|         |     | {                                                  |
|         |     |    "file_path": "/Volumes/catalog_name/            |
| Frank   | 24  |      schema_name/volume_name/data/f1.csv",         |
|         |     |    "file_name": "f1.csv",                          |
|         |     |    "file_size": 10,                                |
|         |     |    "file_block_start": 0,                          |
|         |     |    "file_block_length": 12,                        |
|         |     |    "file_modification_time": "2021-12-20 02:06:21" |
|         |     | }                                                  |
+---------+-----+----------------------------------------------------+
*/
```

### Select specific fields

#### Python

```python
spark.read \
  .format("csv") \
  .schema(schema) \
  .load("/Volumes/catalog_name/schema_name/volume_name/data/*") \
  .select("_metadata.file_name", "_metadata.file_size")
```

#### Scala

```scala
spark.read
  .format("csv")
  .schema(schema)
  .load("/Volumes/catalog_name/schema_name/volume_name/data/*")
  .select("_metadata.file_name", "_metadata.file_size")
```

### Use in filters

#### Python

```python
spark.read \
  .format("csv") \
  .schema(schema) \
  .load("/Volumes/catalog_name/schema_name/volume_name/data/*") \
  .select("*") \
  .filter(col("_metadata.file_name") == lit("test.csv"))
```

#### Scala

```scala
spark.read
  .format("csv")
  .schema(schema)
  .load("/Volumes/catalog_name/schema_name/volume_name/data/*")
  .select("*")
  .filter(col("_metadata.file_name") === lit("test.csv"))
```

### Use in COPY INTO (legacy)

```sql
COPY INTO my_delta_table
FROM (
  SELECT *, _metadata FROM 's3://my-bucket/csvData'
)
FILEFORMAT = CSV
```

### Use in Auto Loader

If your source data contains a column named `_metadata`, rename it to `source_metadata`. If you don't rename it, you can't access the file metadata column in the target table; queries will return the source column instead.

#### Python

```python
spark.readStream \
  .format("cloudFiles") \
  .option("cloudFiles.format", "csv") \
  .schema(schema) \
  .load("s3://my-bucket/csvData") \
  .selectExpr("*", "_metadata as source_metadata") \
  .writeStream \
  .option("checkpointLocation", checkpointLocation) \
  .start(targetTable)
```

#### Scala

```scala
spark.readStream
  .format("cloudFiles")
  .option("cloudFiles.format", "csv")
  .schema(schema)
  .load("s3://my-bucket/csvData")
  .selectExpr("*", "_metadata as source_metadata")
  .writeStream
  .option("checkpointLocation", checkpointLocation)
  .start(targetTable)
```

If you use [foreachBatch](https://docs.databricks.com/aws/en/structured-streaming/foreach.md) and want to include the file metadata column in the streaming DataFrame, you must reference it in the streaming read DataFrame before the `foreachBatch` function. If you only reference the file metadata column inside the `foreachBatch` function, the column is not included.

#### Python

```python
spark.readStream \
  .format("cloudFiles") \
  .option("cloudFiles.format", "csv") \
  .load("s3://my-bucket/csvData") \
  .select("*", "metadata") \
  .writeStream \
  .foreachBatch(...)
```

#### Scala

```scala
spark.readStream
  .format("cloudFiles")
  .option("cloudFiles.format", "csv")
  .load("s3://my-bucket/csvData")
  .select("*", "metadata")
  .writeStream
  .foreachBatch(...)
```

## <a id="additional-resources" /> Additional resources

* [Auto Loader](https://docs.databricks.com/aws/en/ingestion/cloud-object-storage/auto-loader/index.md)
* [Structured Streaming](https://docs.databricks.com/aws/en/structured-streaming/concepts.md)
* [Object metadata column](https://docs.databricks.com/aws/en/ingestion/object-metadata-column.md)
* [COPY INTO (legacy)](https://docs.databricks.com/aws/en/sql/language-manual/delta-copy-into.md)
