共用方式為


使用 REST API 建立和取得譜系關聯性

在本教學課程中,您將瞭解如何使用 Microsoft Purview REST API 來:

  1. 建立資料資產之間的資產及譜系關係。
  2. 查詢譜系關係/路徑。

本文中參考的 API:

必要條件

使用這些 API 需要 資料策展人和資料讀取者角色。 如需如何取得存取權杖的詳細資訊,請參閱 本教學課程

概念

資產

在 Microsoft Purview 中,有兩種類型的基底資產:DataSet 和 Process。

  • 包含 Azure SQL 資料表、Oracle 資料表等資料的資料集資產應該繼承自 DataSet。
  • 處理資料的流程資產,例如資料管道、查詢、函式等,應繼承自流程。

請參閱 資產和類型 ,以瞭解資料集和程序的類型定義。

關係和血統

在 Microsoft Purview 中,譜系有三種類型的關聯性:

  • dataset_process_inputs:將 DataSet 連接到 Process,這意味著 DataSet 是 Process 的輸入
  • process_dataset_outputs:將 Process 連線至 DataSet,這表示 Process 會產生 DataSet
  • direct_lineage_dataset_dataset:將 DataSet1 連接到 DataSet2,這意味著 DataSet1 是 DataSet2 的上游,儘管我們不知道它們之間到底是哪個進程

範例 1:以下是具有 2 個資料集、一個進程和兩個譜系關係的譜系範例

資料集---> dataset_process_inputs ---> 處理程序---> process_dataset_outputs ---> 資料集

螢幕擷取畫面顯示要處理的 DataSet 至 DataSet 譜系。

範例 2:具有 2 個資料集和一個譜系關係的譜系的另一個範例:

資料集---> direct_lineage_dataset_dataset ---> 資料集

顯示 DataSet 到 DataSet 譜系的螢幕擷取畫面。

建立資料資產之間的資產及譜系關係

在下列各節中,讓我們將 hive_table 作為 DataSet 的範例類型,並將 hive_query_process 作為 Process 的範例類型。 我們將使用這兩種類型創建資產並在它們之間創建沿襲。 您可以使用繼承自 DataSet 或 Process 的任何其他類型來建立譜系。

範例 1

透過 API 建立資產

如果您要建立譜系的資產尚未在 Microsoft Purview 中建立,您可以呼叫下列 API 來建立它們。

API大量建立資產

工作:建立兩個hive_tables做為資料集 - table1 和 table2,每個資料集有 2 個hive_columns - column1 和 column2。

POST {endpoint}/datamap/api/atlas/v2/entity/bulk

與身體:

{
  "entities": [
    {
      "typeName": "hive_table",
      "attributes": {
        "qualifiedName": "test_lineage.table1",
        "name": "table1"
      },
      "relationshipAttributes": {
        "columns": [
          {
            "guid": "-11",
            "typeName": "hive_column"
          },{
            "guid": "-12",
            "typeName": "hive_column"
          }
        ]
      },
      "guid": "-1"
    },
    {
      "typeName": "hive_column",
      "attributes": {
        "qualifiedName": "test_lineage.table1#column1",
        "name": "column1",
        "type": "int"
      },
      "guid": "-11",
      "relationshipAttributes": {
        "table": {
          "guid": "-1",
          "typeName": "hive_table"
        }
      }
    },
    {
      "typeName": "hive_column",
      "attributes": {
        "qualifiedName": "test_lineage.table1#column2",
        "name": "column2",
        "type": "int"
      },
      "guid": "-12",
      "relationshipAttributes": {
        "table": {
          "guid": "-1",
          "typeName": "hive_table"
        }
      }
    },
    {
      "typeName": "hive_table",
      "attributes": {
        "qualifiedName": "test_lineage.table2",
        "name": "table2"
      },
      "relationshipAttributes": {
        "columns": [
          {
            "guid": "-21",
            "typeName": "hive_column"
          },{
            "guid": "-22",
            "typeName": "hive_column"
          }
        ]
      },
      "guid": "-2"
    },
    {
      "typeName": "hive_column",
      "attributes": {
        "qualifiedName": "test_lineage.table2#column1",
        "name": "column1",
        "type": "int"
      },
      "guid": "-21",
      "relationshipAttributes": {
        "table": {
          "guid": "-2",
          "typeName": "hive_table"
        }
      }
    },
    {
      "typeName": "hive_column",
      "attributes": {
        "qualifiedName": "test_lineage.table2#column2",
        "name": "column2",
        "type": "int"
      },
      "guid": "-22",
      "relationshipAttributes": {
        "table": {
          "guid": "-2",
          "typeName": "hive_table"
        }
      }
    }    
  ]
}

工作:建立程序資產 'hive_view_query'

POST {endpoint}/datamap/api/atlas/v2/entity/bulk

與身體:

{
  "entities": [
    {
      "typeName": "hive_view_query",
      "attributes": {
        "qualifiedName": "test_lineage.HiveQuery1",
        "name": "HiveQuery1",
        "columnMapping": "[{\"DatasetMapping\":{\"Source\":\"test_lineage.table1\",\"Sink\":\"test_lineage.table2\"},\"ColumnMapping\":[{\"Source\":\"column1\",\"Sink\":\"column1\"},{\"Source\":\"column2\",\"Sink\":\"column2\"}]}]"
      },
      "guid": "-1"
    }
  ]
}

上述 API 呼叫會產生兩個hive_tables (資料集) 和一個hive_view_query (處理) 。

在資料集與程序之間建立譜系關係

API建立關係

工作:從 table1 -> HiveQuery1 (即資料集 -> 處理程序) 建立譜系

POST {endpoint}/datamap/api/atlas/v2/relationship

與身體:

{
        "typeName": "dataset_process_inputs",
        "guid": "-1",
        "end1": {
            "typeName": "hive_table",
            "uniqueAttributes": {
                "qualifiedName": "test_lineage.table1"
            }
        },
        "end2": {
            "typeName": "Process",
            "uniqueAttributes": {
                "qualifiedName": "test_lineage.HiveQuery1"
            }
        }
}

工作:從 HiveQuery1 -> table2 建立譜系 (,即處理 -> 資料集)

POST {endpoint}/datamap/api/atlas/v2/relationship

與身體:

{
        "typeName": "process_dataset_outputs",
        "guid": "-2",
        "end1": {
            "typeName": "Process",
            "uniqueAttributes": {
                "qualifiedName": "test_lineage.HiveQuery1"
            }
        },
        "end2": {
            "typeName": "hive_table",
            "uniqueAttributes": {
                "qualifiedName": "test_lineage.table2"
            }
        }
}

檢視譜系

建立資產和譜系關聯性之後,您可以在 Microsoft Purview 中檢查譜系圖表:

DataSet-Process-DataSet 譜系。

範例 2

建立具有兩欄的 Hive 表格 table3

API大量建立資產

工作:建立 table3,其中包含 2 個hive_columns、column1 和 column2

POST {endpoint}/datamap/api/atlas/v2/entity/bulk

與身體:

{
"entities": [
    {
      "typeName": "hive_table",
      "attributes": {
        "qualifiedName": "test_lineage.table3",
        "name": "table3"
      },
      "relationshipAttributes": {
        "columns": [
          {
            "guid": "-31",
            "typeName": "hive_column"
          },{
            "guid": "-32",
            "typeName": "hive_column"
          }
        ]
      },
      "guid": "-3"
    },
    {
      "typeName": "hive_column",
      "attributes": {
        "qualifiedName": "test_lineage.table3#column1",
        "name": "column1",
        "type": "int"
      },
      "guid": "-31",
      "relationshipAttributes": {
        "table": {
          "guid": "-3",
          "typeName": "hive_table"
        }
      }
    },
    {
      "typeName": "hive_column",
      "attributes": {
        "qualifiedName": "test_lineage.table3#column2",
        "name": "column2",
        "type": "int"
      },
      "guid": "-32",
      "relationshipAttributes": {
        "table": {
          "guid": "-3",
          "typeName": "hive_table"
        }
      }
    }
   ]
}

在表格 2 與表格 3 之間建立直接譜系,並使用直欄對映

API建立關係

作業:從 table2 -> table3 (建立譜系,即資料集 -> 具有直欄對應的資料集)

POST {endpoint}/datamap/api/atlas/v2/relationship

與身體:

{
    "typeName": "direct_lineage_dataset_dataset",
    "guid": "-1",
    "end1": {
        "typeName": "hive_table",
        "uniqueAttributes": {
            "qualifiedName": "test_lineage.table2"
        }
    },
    "end2": {
        "typeName": " hive_table ",
        "uniqueAttributes": {
            "qualifiedName": "test_lineage.table3"
        }
    },
    "attributes": {
      "columnMapping": "[{\"Source\":\"column1\",\"Sink\":\"column1\"},{\"Source\":\"column2\",\"Sink\":\"column2\"}]"
    }
}

檢視譜系

現在,從上述範例 1 & 範例 2 (譜系圖) 變成:

DataSet 到 DataSet 譜系。

請注意,table2 會直接連結至 table3,它們之間沒有 HiveQuery。

查詢譜系關係/路徑

API依 GUID 取得譜系

工作:透過 REST API 取得 table2 的譜系

GET {{endpoint}}/api/atlas/v2/lineage/{{guid_of_table2}}?direction=BOTH

您可以在下方使用JSON回應承載:

{
    "baseEntityGuid": "2a12b3ff-5816-4222-833a-035bf82e06e0",
    "lineageDirection": "BOTH",
    "lineageDepth": 3,
    "lineageWidth": -1,
    "childrenCount": -1,
    "guidEntityMap": {
        "16b93b78-8683-4f88-9651-24c4a9d797b0": {
            "typeName": "hive_table",
            "attributes": {
                "temporary": false,
                "lastAccessTime": 0,
                "createTime": 0,
                "qualifiedName": "test_lineage.table3",
                "name": "table3",
                "retention": 0
            },
            "lastModifiedTS": "1",
            "guid": "16b93b78-8683-4f88-9651-24c4a9d797b0",
            "status": "ACTIVE",
            "displayText": "table3",
            "classificationNames": [],
            "meaningNames": [],
            "meanings": [],
            "isIncomplete": false,
            "labels": [],
            "isIndexed": true
        },
        "cb22ba23-47a2-4149-ade6-e3d9642fe592": {
            "typeName": "hive_table",
            "attributes": {
                "temporary": false,
                "lastAccessTime": 0,
                "createTime": 0,
                "qualifiedName": "test_lineage.table1",
                "name": "table1",
                "retention": 0
            },
            "lastModifiedTS": "1",
            "guid": "cb22ba23-47a2-4149-ade6-e3d9642fe592",
            "status": "ACTIVE",
            "displayText": "table1",
            "classificationNames": [],
            "meaningNames": [],
            "meanings": [],
            "isIncomplete": false,
            "labels": [],
            "isIndexed": true
        },
        "bbeacce6-5bde-46f7-8fe4-689cbb36ba51": {
            "typeName": "hive_view_query",
            "attributes": {
                "qualifiedName": "test_lineage.HiveQuery1",
                "name": "HiveQuery1",
                "columnMapping": "[{\"DatasetMapping\":{\"Source\":\"test_lineage.table1\",\"Sink\":\"test_lineage.table2\"},\"ColumnMapping\":[{\"Source\":\"column1\",\"Sink\":\"column1\"},{\"Source\":\"column2\",\"Sink\":\"column2\"}]}]"
            },
            "lastModifiedTS": "1",
            "guid": "bbeacce6-5bde-46f7-8fe4-689cbb36ba51",
            "status": "ACTIVE",
            "displayText": "HiveQuery1",
            "classificationNames": [],
            "meaningNames": [],
            "meanings": [],
            "isIncomplete": false,
            "labels": [],
            "isIndexed": true
        },
        "2a12b3ff-5816-4222-833a-035bf82e06e0": {
            "typeName": "hive_table",
            "attributes": {
                "temporary": false,
                "lastAccessTime": 0,
                "createTime": 0,
                "qualifiedName": "test_lineage.table2",
                "name": "table2",
                "retention": 0
            },
            "lastModifiedTS": "1",
            "guid": "2a12b3ff-5816-4222-833a-035bf82e06e0",
            "status": "ACTIVE",
            "displayText": "table2",
            "classificationNames": [],
            "meaningNames": [],
            "meanings": [],
            "isIncomplete": false,
            "labels": [],
            "isIndexed": true
        }
    },
    "includeParent": false,
    "relations": [
        {
            "fromEntityId": "2a12b3ff-5816-4222-833a-035bf82e06e0",
            "toEntityId": "16b93b78-8683-4f88-9651-24c4a9d797b0",
            "relationshipId": "23df8e3e-b066-40b2-be29-9fd90693c51b",
            "columnMapping": "[{\"Source\":\"column1\",\"Sink\":\"column1\"},{\"Source\":\"column2\",\"Sink\":\"column2\"}]"
        },
        {
            "fromEntityId": "bbeacce6-5bde-46f7-8fe4-689cbb36ba51",
            "toEntityId": "2a12b3ff-5816-4222-833a-035bf82e06e0",
            "relationshipId": "5fe8d378-39cd-4c6b-8ced-91b0152d3014"
        },
        {
            "fromEntityId": "cb22ba23-47a2-4149-ade6-e3d9642fe592",
            "toEntityId": "bbeacce6-5bde-46f7-8fe4-689cbb36ba51",
            "relationshipId": "73e084bf-98a3-45fb-a1e4-c56cc40661b8"
        }
    ],
    "parentRelations": [],
    "widthCounts": {
        "INPUT": {
            "cb22ba23-47a2-4149-ade6-e3d9642fe592": 0,
            "bbeacce6-5bde-46f7-8fe4-689cbb36ba51": 1,
            "2a12b3ff-5816-4222-833a-035bf82e06e0": 1
        },
        "OUTPUT": {
            "16b93b78-8683-4f88-9651-24c4a9d797b0": 0,
            "2a12b3ff-5816-4222-833a-035bf82e06e0": 1
        }
    }
}

其他資源

類型定義

所有資產/實體和關係都在類型系統中定義。

呼叫 清單所有類型定義 API,以取得 Microsoft Purview 執行個體中的目前類型定義。 如果您不確定要在所述的 API 呼叫中使用哪個 typeName,您可以檢查類型定義 API 以尋找適當的資產/實體類型。

以下是此 API 的範例回應:

TypeDef API 回應 1.

可以看到,在上述回應的entityDefs中,定義了資產類型 (如oracle_table、oracle_view等 ) 。 進一步查看此範例中資產 (的定義,oracle_view) 顯示資產是繼承的 DataSet 類型。

TypeDef API 回應 2.

同樣地,您可以發現一個進程 (例如,Oracle_function) 繼承自「進程」類型,如下所示:

TypeDef API 回應 3.

建立新的自訂類型

如果您想要建立自訂資產或程序,可以使用下列 API。

API建立 Typedef API

工作:建立自訂程序類型

POST {endpoint}/datamap/api/atlas/v2/types/typedefs

與身體:

{
  "enumDefs": [],
  "structDefs": [],
  "classificationDefs": [],
  "entityDefs": [
    {
      "name": "MyCustomServiceProcess",
      "superTypes": [
        "Process"
      ],
      "typeVersion": "1.0",
      "attributeDefs": []
    }
  ],
  "relationshipDefs": []
}

工作:建立自訂資料集類型

POST {endpoint}/datamap/api/atlas/v2/types/typedefs

與身體:

{
  "enumDefs": [],
  "structDefs": [],
  "classificationDefs": [],
  "entityDefs": [
    {
      "name": "MyCustomDataSet",
      "superTypes": [
        "DataSet"
      ],
      "typeVersion": "1.0",
      "attributeDefs": []
    }
  ],
  "relationshipDefs": []
}