snowflake.snowpark.lineage.Lineage.trace

Lineage.trace(object_name: str, object_domain: str, *, object_version: Optional[str] = None, direction: Union[str, LineageDirection] = LineageDirection.BOTH, distance: int = 2) DataFrame[source] (https://github.com/snowflakedb/snowpark-python/blob/v1.16.0/src/snowflake/snowpark/lineage.py#L548-L626)

Traces the lineage of an object within Snowflake and returns it as a DataFrame.

Parameters:
  • object_name (str) – The fully qualified name of the Snowflake object to start trace, formatted as below: Non-Case-sensitive: “database.schema.object” Case-sensitive: “”database”.”schema”.”object””

  • object_domain (str) – The domain of the Snowflake object to start trace. e.g., “table”, “view”.

  • object_version (Optional[str]) – Version of the versioned Snowflake object (e.g., model or dataset) to begin tracing. Defaults to None.

  • direction (LineageDirection) – The direction to trace (UPSTREAM, DOWNSTREAM, BOTH), defaults to BOTH.

  • distance (int) – Trace distance, defaults to 2, with a maximum of 10.

Returns:

A DataFrame representing the traced lineage with the following schema:
  • source (str): The source of the lineage.

  • target (str): The target of the lineage.

  • direction (str): The direction of the lineage (‘FORWARD’, ‘BACKWARD’, or ‘BOTH’).

  • distance (int): The distance of the lineage tracing from given object.

Example

>>> db = session.get_current_database().replace('"', "")
>>> schema = session.get_current_schema().replace('"', "")
>>> _ = session.sql(f"CREATE OR REPLACE TABLE {db}.{schema}.T1(C1 INT)").collect()
>>> _ = session.sql(
...     f"CREATE OR REPLACE VIEW {db}.{schema}.V1 AS SELECT * FROM {db}.{schema}.T1"
... ).collect()
>>> _ = session.sql(
...     f"CREATE OR REPLACE VIEW {db}.{schema}.V2 AS SELECT * FROM {db}.{schema}.V1"
... ).collect()
>>> df = session.lineage.trace(
...     f"{db}.{schema}.T1",
...     "table",
...     direction="downstream"
... )
>>> df.show() 
-------------------------------------------------------------------------------------------------------------------------------------------------
| "SOURCE_OBJECT"                                         | "TARGET_OBJECT"                                        | "DIRECTION"   | "DISTANCE" |
-------------------------------------------------------------------------------------------------------------------------------------------------
| {"createdOn": "2023-11-15T12:30:23Z", "domain": "TABLE",| {"createdOn": "2023-11-15T12:30:23Z", "domain": "VIEW",| "Downstream"  | 1          |
|  "name": "YOUR_DATABASE.YOUR_SCHEMA.T1", "status":      |  "name": "YOUR_DATABASE.YOUR_SCHEMA.V1", "status":     |               |            |
|  "ACTIVE"}                                              |  "ACTIVE"}                                             |               |            |
| {"createdOn": "2023-11-15T12:30:23Z", "domain": "VIEW", | {"createdOn": "2023-11-15T12:30:23Z", "domain": "VIEW",| "Downstream"  | 2          |
|  "name": "YOUR_DATABASE.YOUR_SCHEMA.V1", "status":      |  "name": "YOUR_DATABASE.YOUR_SCHEMA.V2", "status":     |               |            |
|  "ACTIVE"}                                              |  "ACTIVE"}                                             |               |            |
-------------------------------------------------------------------------------------------------------------------------------------------------
Copy

Return type:

snowflake.snowpark.DataFrame

This function or method is in private preview since 1.16.0.

语言: 中文