forked from chrisperks/cdmc-atlas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
populate_catalog.py
196 lines (160 loc) · 7.06 KB
/
populate_catalog.py
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
import json
from apache_atlas.client.base_client import AtlasClient
from apache_atlas.model.instance import AtlasEntityWithExtInfo
from apache_atlas.model.glossary import (
AtlasGlossary,
AtlasGlossaryHeader,
AtlasGlossaryTerm,
)
from apache_atlas.model.typedef import AtlasTypesDef, AtlasAttributeDef
from apache_atlas.utils import type_coerce
client = AtlasClient("http://localhost:21000", ("admin", "admin"))
def load_type_defs_from_json(json_path, skip_existing=True):
"""
Load defs from JSON
"""
with open(json_path, encoding="utf-8") as f:
type_def = type_coerce(json.load(f), AtlasTypesDef)
types_to_create = AtlasTypesDef()
types_to_create.enumDefs = []
types_to_create.structDefs = []
types_to_create.classificationDefs = []
types_to_create.entityDefs = []
types_to_create.relationshipDefs = []
types_to_create.businessMetadataDefs = []
for enum_def in type_def.enumDefs:
if skip_existing and client.typedef.type_with_name_exists(enum_def.name):
print("Type with name %s already exists. Skipping.", enum_def.name)
else:
types_to_create.enumDefs.append(enum_def)
for struct_def in type_def.structDefs:
if skip_existing and client.typedef.type_with_name_exists(struct_def.name):
print("Type with name %s already exists. Skipping.", struct_def.name)
else:
types_to_create.structDefs.append(struct_def)
for classification_def in type_def.classificationDefs:
if skip_existing and client.typedef.type_with_name_exists(
classification_def.name
):
print(
"Type with name %s already exists. Skipping.",
classification_def.name,
)
else:
types_to_create.classificationDefs.append(classification_def)
for entity_def in type_def.entityDefs:
entity_def.attributeDefs.append(AtlasAttributeDef({}))
if skip_existing and client.typedef.type_with_name_exists(entity_def.name):
print("Type with name %s already exists. Skipping.", entity_def.name)
else:
types_to_create.entityDefs.append(entity_def)
for relationship_def in type_def.relationshipDefs:
if skip_existing and client.typedef.type_with_name_exists(
relationship_def.name
):
print(
"Type with name %s already exists. Skipping.", relationship_def.name
)
else:
types_to_create.relationshipDefs.append(relationship_def)
for business_metadata_def in type_def.businessMetadataDefs:
if skip_existing and client.typedef.type_with_name_exists(
business_metadata_def.name
):
print(
"Type with name %s already exists. Skipping.",
business_metadata_def.name,
)
else:
types_to_create.businessMetadataDefs.append(business_metadata_def)
return types_to_create
def create_type_defs(json_path):
"""
Create the typedefs
"""
types_to_create = load_type_defs_from_json(json_path)
return client.typedef.create_atlas_typedefs(types_to_create)
def delete_type_defs(json_path):
"""
Delete the typedefs
"""
type_defs = load_type_defs_from_json(json_path, False)
names_to_delete = []
names_to_delete.extend(
[type_def["name"] for type_def in type_defs.relationshipDefs]
)
names_to_delete.extend([type_def["name"] for type_def in type_defs.enumDefs])
names_to_delete.extend([type_def["name"] for type_def in type_defs.structDefs])
names_to_delete.extend(
[type_def["name"] for type_def in type_defs.classificationDefs]
)
names_to_delete.extend(
[type_def["name"] for type_def in type_defs.businessMetadataDefs]
)
names_to_delete.extend([type_def["name"] for type_def in type_defs.entityDefs])
for typedef_name in names_to_delete:
print("deleting " + typedef_name)
client.typedef.delete_type_by_name(typedef_name)
def create_entities(json_path):
"""
Create entities
"""
with open(json_path, encoding="utf-8") as f:
entity = type_coerce(json.load(f), AtlasEntityWithExtInfo)
print("Creating or updating " + entity.entity.attributes["name"])
return client.entity.create_entity(entity)
def create_glossary(json_path):
"""
Create glossary
"""
with open(json_path, encoding="utf-8") as f:
raw_json = json.load(f)
glossary = type_coerce(raw_json, AtlasGlossary)
terms = [type_coerce(x, AtlasGlossaryTerm) for x in raw_json["terms"]]
for existing_glossary in client.glossary.get_all_glossaries():
if existing_glossary["name"] == glossary.name:
client.glossary.delete_glossary_by_guid(existing_glossary["guid"])
print("Creating or updating " + glossary.name)
new_glossary = client.glossary.create_glossary(glossary)
shared_glossary_header = AtlasGlossaryHeader()
shared_glossary_header.glossaryGuid = new_glossary.guid
shared_glossary_header.displayText = glossary.name
for term in terms:
term.anchor = shared_glossary_header
client.glossary.create_glossary_terms(terms)
json_type_defs = [
"models/2000_SQLDB_typedefs.json",
"models/3000_AWS_S3_typedefs.json",
"models/3500_Azure_ADLS_typedefs.json",
]
json_entity_defs = [
"models/[email protected]",
"models/[email protected]",
"models/[email protected]",
"models/[email protected]",
"models/[email protected]",
"models/[email protected]",
"models/[email protected]",
"models/[email protected]",
"models/[email protected]",
"models/[email protected]",
"models/[email protected]",
"models/[email protected]",
"models/[email protected]",
"models/[email protected]",
"models/[email protected]",
"models/[email protected]",
"models/[email protected]",
"models/[email protected]",
"models/[email protected]",
]
json_glossary_defs = ["models/4000_Glossary_EDM_DCAM.json"]
# Create Typedefs
for path in json_type_defs:
create_type_defs(path)
# Create Entities
for path in json_entity_defs:
create_entities(path)
# Create Glossaries
# for path in json_glossary_defs:
# create_glossary(path)