您可以使用 _metadata 資料列取得輸入檔的元數據資訊。
_metadata 資料行是 隱藏的 資料行,而且適用於所有輸入檔案格式。 若要在傳回的 DataFrame 中包含 _metadata 欄,您必須在指定來源的讀取查詢語法中明確選取它。
如果數據源包含名為 _metadata的數據行,則查詢會從數據源傳回數據行,而不是檔案元數據。
警告
未來版本中,新的欄位可能會新增至 [_metadata] 數據行。 若要避免在更新 _metadata 數據行時發生架構演進錯誤,Databricks 建議從查詢中的數據行選取特定欄位。 請參閱 範例。
支援的中繼資料
_metadata 欄位是包含下列欄位的 STRUCT:
| 名稱 | 類型 | 描述 | 範例 | 最低 Databricks 執行環境版本 |
|---|---|---|---|---|
| file_path | STRING |
輸入檔的檔案路徑。 | file:/tmp/f0.csv |
10.5 |
| file_name | STRING |
輸入檔的名稱及其擴展名。 | f0.csv |
10.5 |
| file_size | LONG |
輸入檔的長度,以位元組為單位。 | 628 | 10.5 |
| 檔案修改時間 | TIMESTAMP |
輸入檔的上次修改時間戳。 | 2021-12-20 20:05:21 |
10.5 |
| 文件區塊開始 | LONG |
正在讀取之區塊的開始位移,以位元組為單位。 | 0 | 13.0 |
| 文件區塊長度 | LONG |
要讀取的區塊長度,以位元組為單位。 | 628 | 13.0 |
例子
在基本檔案型數據源讀取器中使用
Python
df = spark.read \
.format("csv") \
.schema(schema) \
.load("dbfs:/tmp/*") \
.select("*", "_metadata")
display(df)
'''
Result:
+---------+-----+----------------------------------------------------+
| name | age | _metadata |
+=========+=====+====================================================+
| | | { |
| | | "file_path": "dbfs:/tmp/f0.csv", |
| Debbie | 18 | "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": "dbfs:/tmp/f1.csv", |
| Frank | 24 | "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
val df = spark.read
.format("csv")
.schema(schema)
.load("dbfs:/tmp/*")
.select("*", "_metadata")
display(df_population)
/* Result:
+---------+-----+----------------------------------------------------+
| name | age | _metadata |
+=========+=====+====================================================+
| | | { |
| | | "file_path": "dbfs:/tmp/f0.csv", |
| Debbie | 18 | "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": "dbfs:/tmp/f1.csv", |
| Frank | 24 | "file_name": "f1.csv", |
| | | "file_size": 10, |
| | | "file_block_start": 0, |
| | | "file_block_length": 12, |
| | | "file_modification_time": "2021-12-20 02:06:21" |
| | | } |
+---------+-----+----------------------------------------------------+
*/
選取特定欄位
Python
spark.read \
.format("csv") \
.schema(schema) \
.load("dbfs:/tmp/*") \
.select("_metadata.file_name", "_metadata.file_size")
程式語言 Scala
spark.read
.format("csv")
.schema(schema)
.load("dbfs:/tmp/*")
.select("_metadata.file_name", "_metadata.file_size")
在篩選中使用
Python
spark.read \
.format("csv") \
.schema(schema) \
.load("dbfs:/tmp/*") \
.select("*") \
.filter(col("_metadata.file_name") == lit("test.csv"))
程式語言 Scala
spark.read
.format("csv")
.schema(schema)
.load("dbfs:/tmp/*")
.select("*")
.filter(col("_metadata.file_name") === lit("test.csv"))
在 COPY INTO(舊版)中使用
COPY INTO my_delta_table
FROM (
SELECT *, _metadata FROM 'abfss://my-container-name@storage-account-name.dfs.core.windows.net/csvData'
)
FILEFORMAT = CSV
在自動載入器中使用
如果您的來源資料包含名為 _metadata的資料列,請將它重新命名為 source_metadata。 如果您未將它重新命名,就無法存取目標數據表中的檔案元數據數據行;查詢會改為傳回源數據行。
Python
spark.readStream \
.format("cloudFiles") \
.option("cloudFiles.format", "csv") \
.schema(schema) \
.load("abfss://my-container-name@storage-account-name.dfs.core.windows.net/csvData") \
.selectExpr("*", "_metadata as source_metadata") \
.writeStream \
.option("checkpointLocation", checkpointLocation) \
.start(targetTable)
程式語言 Scala
spark.readStream
.format("cloudFiles")
.option("cloudFiles.format", "csv")
.schema(schema)
.load("abfss://my-container-name@storage-account-name.dfs.core.windows.net/csvData")
.selectExpr("*", "_metadata as source_metadata")
.writeStream
.option("checkpointLocation", checkpointLocation)
.start(targetTable)
如果您使用 foreachBatch ,而且想要在串流 DataFrame 中包含檔案元數據欄,那麼必須在串流讀取 DataFrame 中於函式 foreachBatch 之前參考該欄。 如果您僅在函式中參考檔案的元數據欄,則該欄不會被包含。
Python
spark.readStream \
.format("cloudFiles") \
.option("cloudFiles.format", "csv") \
.load("abfss://my-container-name@storage-account-name.dfs.core.windows.net/csvData") \
.select("*", "metadata") \
.writeStream \
.foreachBatch(...)
程式語言 Scala
spark.readStream
.format("cloudFiles")
.option("cloudFiles.format", "csv")
.load("abfss://my-container-name@storage-account-name.dfs.core.windows.net/csvData")
.select("*", "metadata")
.writeStream
.foreachBatch(...)