File size: 8,378 Bytes
0510038
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
"""
Collibra Exporter - Export to Collibra Data Intelligence format.

Collibra is an enterprise data governance and catalog platform.
https://www.collibra.com/
"""

from typing import Dict, Any, List
from datetime import datetime
import uuid
from .base import LineageExporter, LineageGraph, LineageNode, LineageEdge


class CollibraExporter(LineageExporter):
    """Export lineage to Collibra import format."""

    def __init__(self, graph: LineageGraph, community_name: str = "Data Lineage",
                 domain_name: str = "Physical Data Dictionary"):
        super().__init__(graph)
        self.community_name = community_name
        self.domain_name = domain_name

    @property
    def format_name(self) -> str:
        return "Collibra"

    @property
    def file_extension(self) -> str:
        return ".json"

    def _node_type_to_collibra_type(self, node_type: str) -> str:
        """Map internal node types to Collibra asset types."""
        type_mapping = {
            "table": "Table",
            "view": "View",
            "model": "Data Set",
            "source": "Data Source",
            "destination": "Data Target",
            "column": "Column",
            "database": "Database",
            "schema": "Schema",
            "report": "Report",
            "dimension": "Dimension Table",
            "fact": "Fact Table",
            "feature_set": "Data Set",
            "semantic_model": "Business Intelligence Report",
            "external_api": "Data Source",
            "extract": "Data Set"
        }
        return type_mapping.get(node_type.lower(), "Data Set")

    def _edge_type_to_collibra_relation(self, edge_type: str) -> str:
        """Map internal edge types to Collibra relation types."""
        relation_mapping = {
            "transform": "is source of",
            "reference": "references",
            "ingest": "is source of",
            "export": "is target of",
            "join": "is source of",
            "aggregate": "is source of",
            "model": "is source of",
            "publish": "is target of",
            "reverse_etl": "is target of"
        }
        return relation_mapping.get(edge_type.lower(), "is source of")

    def _create_asset(self, node: LineageNode) -> Dict[str, Any]:
        """Create a Collibra asset from a node."""
        asset = {
            "resourceType": "Asset",
            "identifier": {
                "name": node.name,
                "domain": {
                    "name": self.domain_name,
                    "community": {
                        "name": self.community_name
                    }
                }
            },
            "type": {
                "name": self._node_type_to_collibra_type(node.type)
            },
            "displayName": node.name,
            "attributes": {}
        }

        # Add description
        if node.description:
            asset["attributes"]["Description"] = [{"value": node.description}]

        # Add database and schema
        if node.database:
            asset["attributes"]["Technical Data Type"] = [{"value": node.database}]
        if node.schema:
            asset["attributes"]["Schema Name"] = [{"value": node.schema}]

        # Add owner
        if node.owner:
            asset["attributes"]["Data Owner"] = [{"value": node.owner}]

        # Add tags as business terms
        if node.tags:
            asset["attributes"]["Tags"] = [{"value": ", ".join(node.tags)}]

        # Add category
        if node.category:
            asset["attributes"]["Category"] = [{"value": node.category}]

        return asset

    def _create_relation(self, edge: LineageEdge) -> Dict[str, Any]:
        """Create a Collibra relation from an edge."""
        source_node = self.graph.get_node(edge.source)
        target_node = self.graph.get_node(edge.target)

        relation = {
            "resourceType": "Relation",
            "source": {
                "name": source_node.name if source_node else edge.source,
                "domain": {
                    "name": self.domain_name,
                    "community": {
                        "name": self.community_name
                    }
                }
            },
            "target": {
                "name": target_node.name if target_node else edge.target,
                "domain": {
                    "name": self.domain_name,
                    "community": {
                        "name": self.community_name
                    }
                }
            },
            "type": {
                "role": self._edge_type_to_collibra_relation(edge.type),
                "coRole": "has source",
                "sourceType": {
                    "name": self._node_type_to_collibra_type(
                        source_node.type if source_node else "table"
                    )
                },
                "targetType": {
                    "name": self._node_type_to_collibra_type(
                        target_node.type if target_node else "table"
                    )
                }
            }
        }

        return relation

    def _create_column_assets(self, node: LineageNode) -> List[Dict[str, Any]]:
        """Create Collibra column assets from a node's columns."""
        if not node.columns:
            return []

        column_assets = []
        for col in node.columns:
            column_asset = {
                "resourceType": "Asset",
                "identifier": {
                    "name": f"{node.name}.{col.get('name')}",
                    "domain": {
                        "name": self.domain_name,
                        "community": {
                            "name": self.community_name
                        }
                    }
                },
                "type": {
                    "name": "Column"
                },
                "displayName": col.get("name"),
                "attributes": {
                    "Technical Data Type": [{"value": col.get("type") or col.get("data_type", "string")}]
                },
                "relations": {
                    "Column is part of Table": [{
                        "name": node.name,
                        "domain": {
                            "name": self.domain_name,
                            "community": {
                                "name": self.community_name
                            }
                        }
                    }]
                }
            }

            if col.get("description"):
                column_asset["attributes"]["Description"] = [{"value": col.get("description")}]

            column_assets.append(column_asset)

        return column_assets

    def export(self) -> str:
        """Export to Collibra JSON import format."""
        return self.to_json(indent=2)

    def _to_dict(self) -> Dict[str, Any]:
        """Convert to Collibra import dictionary."""
        # Collect all assets (nodes)
        assets = []
        for node in self.graph.nodes:
            assets.append(self._create_asset(node))
            # Add column assets if present
            assets.extend(self._create_column_assets(node))

        # Collect all relations (edges)
        relations = [self._create_relation(edge) for edge in self.graph.edges]

        return {
            "exportInfo": {
                "producer": "Lineage Graph Accelerator",
                "exportedAt": self.graph.generated_at,
                "sourceLineageName": self.graph.name,
                "format": "Collibra Import API",
                "version": "2.0"
            },
            "community": {
                "name": self.community_name,
                "description": f"Data lineage imported from {self.graph.name}"
            },
            "domain": {
                "name": self.domain_name,
                "type": "Physical Data Dictionary",
                "community": {
                    "name": self.community_name
                }
            },
            "assets": assets,
            "relations": relations,
            "summary": {
                "totalAssets": len(assets),
                "totalRelations": len(relations),
                "assetTypes": list(set(
                    self._node_type_to_collibra_type(n.type) for n in self.graph.nodes
                ))
            }
        }