|
1 |
| -# Apache Atlas Python Client |
| 1 | +# Atlan Python Client |
2 | 2 |
|
3 |
| -Python library for Apache Atlas. |
| 3 | +Python library for Atlan. |
4 | 4 |
|
5 | 5 | ## Installation
|
6 | 6 |
|
7 |
| -Use the package manager [pip](https://pip.pypa.io/en/stable/) to install Python client for Apache Atlas. |
| 7 | +Use the package manager [pip](https://pip.pypa.io/en/stable/) to install Python client for Atlan |
8 | 8 |
|
9 | 9 | ```bash
|
10 |
| -> pip install apache-atlas |
| 10 | +> pip install pyatlan |
11 | 11 | ```
|
12 | 12 |
|
13 |
| -Verify if apache-atlas client is installed: |
| 13 | +Verify if atlan client is installed: |
14 | 14 | ```bash
|
15 | 15 | > pip list
|
16 | 16 |
|
17 | 17 | Package Version
|
18 | 18 | ------------ ---------
|
19 |
| -apache-atlas 0.0.11 |
| 19 | +pyatlan 0.0.1 |
20 | 20 | ```
|
21 | 21 |
|
22 | 22 | ## Usage
|
23 |
| - |
24 |
| -```python atlas_example.py``` |
25 |
| -```python |
26 |
| -# atlas_example.py |
27 |
| - |
28 |
| -import time |
29 |
| - |
30 |
| -from apache_atlas.client.base_client import AtlasClient |
31 |
| -from apache_atlas.model.instance import AtlasEntity, AtlasEntityWithExtInfo, AtlasEntitiesWithExtInfo, AtlasRelatedObjectId |
32 |
| -from apache_atlas.model.enums import EntityOperation |
33 |
| - |
34 |
| - |
35 |
| -## Step 1: create a client to connect to Apache Atlas server |
36 |
| -client = AtlasClient('http://localhost:21000', ('admin', 'atlasR0cks!')) |
37 |
| - |
38 |
| -# For Kerberos authentication, use HTTPKerberosAuth as shown below |
39 |
| -# |
40 |
| -# from requests_kerberos import HTTPKerberosAuth |
41 |
| -# |
42 |
| -# client = AtlasClient('http://localhost:21000', HTTPKerberosAuth()) |
43 |
| - |
44 |
| -# to disable SSL certificate validation (not recommended for production use!) |
45 |
| -# |
46 |
| -# client.session.verify = False |
47 |
| - |
48 |
| - |
49 |
| -## Step 2: Let's create a database entity |
50 |
| -test_db = AtlasEntity({ 'typeName': 'hive_db' }) |
51 |
| -test_db.attributes = { 'name': 'test_db', 'clusterName': 'prod', 'qualifiedName': 'test_db@prod' } |
52 |
| - |
53 |
| -entity_info = AtlasEntityWithExtInfo() |
54 |
| -entity_info.entity = test_db |
55 |
| - |
56 |
| -print('Creating test_db') |
57 |
| - |
58 |
| -resp = client.entity.create_entity(entity_info) |
59 |
| - |
60 |
| -guid_db = resp.get_assigned_guid(test_db.guid) |
61 |
| - |
62 |
| -print(' created test_db: guid=' + guid_db) |
63 |
| - |
64 |
| - |
65 |
| -## Step 3: Let's create a table entity, and two column entities - in one call |
66 |
| -test_tbl = AtlasEntity({ 'typeName': 'hive_table' }) |
67 |
| -test_tbl.attributes = { 'name': 'test_tbl', 'qualifiedName': 'test_db.test_tbl@prod' } |
68 |
| -test_tbl.relationshipAttributes = { 'db': AtlasRelatedObjectId({ 'guid': guid_db }) } |
69 |
| - |
70 |
| -test_col1 = AtlasEntity({ 'typeName': 'hive_column' }) |
71 |
| -test_col1.attributes = { 'name': 'test_col1', 'type': 'string', 'qualifiedName': 'test_db.test_tbl.test_col1@prod' } |
72 |
| -test_col1.relationshipAttributes = { 'table': AtlasRelatedObjectId({ 'guid': test_tbl.guid }) } |
73 |
| - |
74 |
| -test_col2 = AtlasEntity({ 'typeName': 'hive_column' }) |
75 |
| -test_col2.attributes = { 'name': 'test_col2', 'type': 'string', 'qualifiedName': 'test_db.test_tbl.test_col2@prod' } |
76 |
| -test_col2.relationshipAttributes = { 'table': AtlasRelatedObjectId({ 'guid': test_tbl.guid }) } |
77 |
| - |
78 |
| -entities_info = AtlasEntitiesWithExtInfo() |
79 |
| -entities_info.entities = [ test_tbl, test_col1, test_col2 ] |
80 |
| - |
81 |
| -print('Creating test_tbl') |
82 |
| - |
83 |
| -resp = client.entity.create_entities(entities_info) |
84 |
| - |
85 |
| -guid_tbl = resp.get_assigned_guid(test_tbl.guid) |
86 |
| -guid_col1 = resp.get_assigned_guid(test_col1.guid) |
87 |
| -guid_col2 = resp.get_assigned_guid(test_col2.guid) |
88 |
| - |
89 |
| -print(' created test_tbl: guid=' + guid_tbl) |
90 |
| -print(' created test_tbl.test_col1: guid=' + guid_col1) |
91 |
| -print(' created test_tbl.test_col2: guid=' + guid_col2) |
92 |
| - |
93 |
| - |
94 |
| -## Step 4: Let's create a view entity that feeds from the table created earlier |
95 |
| -# Also create a lineage between the table and the view, and lineages between their columns as well |
96 |
| -test_view = AtlasEntity({ 'typeName': 'hive_table' }) |
97 |
| -test_view.attributes = { 'name': 'test_view', 'qualifiedName': 'test_db.test_view@prod' } |
98 |
| -test_view.relationshipAttributes = { 'db': AtlasRelatedObjectId({ 'guid': guid_db }) } |
99 |
| - |
100 |
| -test_view_col1 = AtlasEntity({ 'typeName': 'hive_column' }) |
101 |
| -test_view_col1.attributes = { 'name': 'test_col1', 'type': 'string', 'qualifiedName': 'test_db.test_view.test_col1@prod' } |
102 |
| -test_view_col1.relationshipAttributes = { 'table': AtlasRelatedObjectId({ 'guid': test_view.guid }) } |
103 |
| - |
104 |
| -test_view_col2 = AtlasEntity({ 'typeName': 'hive_column' }) |
105 |
| -test_view_col2.attributes = { 'name': 'test_col2', 'type': 'string', 'qualifiedName': 'test_db.test_view.test_col2@prod' } |
106 |
| -test_view_col2.relationshipAttributes = { 'table': AtlasRelatedObjectId({ 'guid': test_view.guid }) } |
107 |
| - |
108 |
| -test_process = AtlasEntity({ 'typeName': 'hive_process' }) |
109 |
| -test_process.attributes = { 'name': 'create_test_view', 'userName': 'admin', 'operationType': 'CREATE', 'qualifiedName': 'create_test_view@prod' } |
110 |
| -test_process.attributes['queryText'] = 'create view test_view as select * from test_tbl' |
111 |
| -test_process.attributes['queryPlan'] = '<queryPlan>' |
112 |
| -test_process.attributes['queryId'] = '<queryId>' |
113 |
| -test_process.attributes['startTime'] = int(time.time() * 1000) |
114 |
| -test_process.attributes['endTime'] = int(time.time() * 1000) |
115 |
| -test_process.relationshipAttributes = { 'inputs': [ AtlasRelatedObjectId({ 'guid': guid_tbl }) ], 'outputs': [ AtlasRelatedObjectId({ 'guid': test_view.guid }) ] } |
116 |
| - |
117 |
| -test_col1_lineage = AtlasEntity({ 'typeName': 'hive_column_lineage' }) |
118 |
| -test_col1_lineage.attributes = { 'name': 'test_view.test_col1 lineage', 'depenendencyType': 'read', 'qualifiedName': 'test_db.test_view.test_col1@prod' } |
119 |
| -test_col1_lineage.attributes['query'] = { 'guid': test_process.guid } |
120 |
| -test_col1_lineage.relationshipAttributes = { 'inputs': [ AtlasRelatedObjectId({ 'guid': guid_col1 }) ], 'outputs': [ AtlasRelatedObjectId({ 'guid': test_view_col1.guid }) ] } |
121 |
| - |
122 |
| -test_col2_lineage = AtlasEntity({ 'typeName': 'hive_column_lineage' }) |
123 |
| -test_col2_lineage.attributes = { 'name': 'test_view.test_col2 lineage', 'depenendencyType': 'read', 'qualifiedName': 'test_db.test_view.test_col2@prod' } |
124 |
| -test_col2_lineage.attributes['query'] = { 'guid': test_process.guid } |
125 |
| -test_col2_lineage.relationshipAttributes = { 'inputs': [ AtlasRelatedObjectId({ 'guid': guid_col2 }) ], 'outputs': [ AtlasRelatedObjectId({ 'guid': test_view_col2.guid }) ] } |
126 |
| - |
127 |
| -entities_info = AtlasEntitiesWithExtInfo() |
128 |
| -entities_info.entities = [ test_process, test_col1_lineage, test_col2_lineage ] |
129 |
| - |
130 |
| -entities_info.add_referenced_entity(test_view) |
131 |
| -entities_info.add_referenced_entity(test_view_col1) |
132 |
| -entities_info.add_referenced_entity(test_view_col2) |
133 |
| - |
134 |
| -print('Creating test_view') |
135 |
| - |
136 |
| -resp = client.entity.create_entities(entities_info) |
137 |
| - |
138 |
| -guid_view = resp.get_assigned_guid(test_view.guid) |
139 |
| -guid_view_col1 = resp.get_assigned_guid(test_view_col1.guid) |
140 |
| -guid_view_col2 = resp.get_assigned_guid(test_view_col2.guid) |
141 |
| -guid_process = resp.get_assigned_guid(test_process.guid) |
142 |
| -guid_col1_lineage = resp.get_assigned_guid(test_col1_lineage.guid) |
143 |
| -guid_col2_lineage = resp.get_assigned_guid(test_col2_lineage.guid) |
144 |
| - |
145 |
| -print(' created test_view: guid=' + guid_view) |
146 |
| -print(' created test_view.test_col1: guid=' + guid_view_col1) |
147 |
| -print(' created test_view.test_col2: guid=' + guid_view_col1) |
148 |
| -print(' created test_view lineage: guid=' + guid_process) |
149 |
| -print(' created test_col1 lineage: guid=' + guid_col1_lineage) |
150 |
| -print(' created test_col2 lineage: guid=' + guid_col2_lineage) |
151 |
| - |
152 |
| - |
153 |
| -## Step 5: Finally, cleanup by deleting entities created above |
154 |
| -print('Deleting entities') |
155 |
| - |
156 |
| -resp = client.entity.delete_entities_by_guids([ guid_col1_lineage, guid_col2_lineage, guid_process, guid_view, guid_tbl, guid_db ]) |
157 |
| - |
158 |
| -deleted_count = len(resp.mutatedEntities[EntityOperation.DELETE.name]) if resp and resp.mutatedEntities and EntityOperation.DELETE.name in resp.mutatedEntities else 0 |
159 |
| - |
160 |
| -print(' ' + str(deleted_count) + ' entities deleted') |
161 |
| -``` |
162 |
| -For more examples, checkout `sample-app` python project in [atlas-examples](https://github.com/apache/atlas/blob/master/atlas-examples/sample-app/src/main/python/sample_client.py) module. |
0 commit comments