table_changes 資料表值函式

適用于:核取標示為是 Databricks SQL 檢查標示為是 Databricks Runtime

傳回已啟用變更資料摘要之 Delta Lake 資料表的變更記錄。

若要叫用此函式,您必須至少有下列其中一項:

  • SELECT 指定資料表的許可權
  • 成為資料表的擁有者
  • 具有系統管理許可權

語法

table_changes ( table_str, start [, end ] )

參數

  • table_str:STRING 常值,表示資料表的選擇性限定名稱。
  • start:BIGINT 或 TIMESTAMP 常值,代表要傳回之變更的第一個版本或時間戳記。
  • end:選擇性的 BIGINT 或 TIMESTAMP 常值,代表要傳回之變更的最後一個版本或時間戳記。 如果未指定從 start 目前變更到目前變更的所有變更,則會傳回 。

返回

資料表,包括 中所 table_str 識別資料表的所有資料行,以及下列資料行:

  • _change_type STRING NOT NULL

    指定變更: deleteinsertupdate_preimage 、 或 update_postimage

  • _commit_version BIGINT NOT NULL

    指定與變更相關聯的資料表認可版本。

  • _commit_timestamp TIMESTAMP NOT NULL

    指定與變更相關聯的認可時間戳記。

如果 table_str 不代表限定的資料表名稱,則名稱會以 的值 current_schema 限定。 如果資料表名稱包含空格或點,請使用字串內的回引號來加上該部分名稱的引號。

例子

-- Create a Delta table with Change Data Feed;
> CREATE TABLE myschema.t(c1 INT, c2 STRING) TBLPROPERTIES(delta.enableChangeDataFeed=true);

-- Modify the table
> INSERT INTO myschema.t VALUES (1, 'Hello'), (2, 'World');
> INSERT INTO myschema.t VALUES (3, '!');
> UPDATE myschema.t SET c2 = upper(c2) WHERE c1 < 3;
> DELETE FROM myschema.t WHERE c1 = 3;

-- Show the history of table change events
> DESCRIBE HISTORY myschema.t;
 version timestamp                    userId           userName      operation    operationParameters                                            ...
       4 2022-09-01T18:32:35.000+0000 6167625779053302 alf@melmak.et DELETE       {"predicate":"[\"(spark_catalog.myschema.t.c1 = 3)\"]"}
       3 2022-09-01T18:32:32.000+0000 6167625779053302 alf@melmak.et UPDATE       {"predicate":"(c1#3195878 < 3)"}
       2 2022-09-01T18:32:28.000+0000 6167625779053302 alf@melmak.et WRITE        {"mode":"Append","partitionBy":"[]"}
       1 2022-09-01T18:32:26.000+0000 6167625779053302 alf@melmak.et WRITE        {"mode":"Append","partitionBy":"[]"}
       0 2022-09-01T18:32:23.000+0000 6167625779053302 alf@melmak.et CREATE TABLE {"isManaged":"true","description":null,"partitionBy":"[]","properties":"{\"delta.enableChangeDataFeed\":\"true\"}"}

-- Show the change table feed using a the commit timestamp retrieved from the history.
> SELECT * FROM table_changes('`myschema`.`t`', 2);
 c1 c2     _change_type    _commit_version _commit_timestamp
  3 !      insert                        2 2022-09-01T18:32:28.000+0000
  2 WORLD  update_postimage              3 2022-09-01T18:32:32.000+0000
  2 World  update_preimage               3 2022-09-01T18:32:32.000+0000
  1 Hello  update_preimage               3 2022-09-01T18:32:32.000+0000
  1 HELLO  update_postimage              3 2022-09-01T18:32:32.000+0000
  3 !      delete                        4 2022-09-01T18:32:35.000+0000

-- Show the ame change table feed using a point in time.
> SELECT * FROM table_changes('`myschema`.`t`', '2022-09-01T18:32:27.000+0000') ORDER BY _commit_version;
 c1 c2     _change_type    _commit_version _commit_timestamp
  3 !      insert                        2 2022-09-01T18:32:28.000+0000
  2 WORLD  update_postimage              3 2022-09-01T18:32:32.000+0000
  2 World  update_preimage               3 2022-09-01T18:32:32.000+0000
  1 Hello  update_preimage               3 2022-09-01T18:32:32.000+0000
  1 HELLO  update_postimage              3 2022-09-01T18:32:32.000+0000
  3 !      delete                        4 2022-09-01T18:32:35.000+0000