forked from amundsen-io/amundsendatabuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watermark.py
130 lines (114 loc) · 4.47 KB
/
watermark.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
# Copyright Contributors to the Amundsen project.
# SPDX-License-Identifier: Apache-2.0
from typing import (
Iterator, List, Tuple, Union,
)
from amundsen_rds.models import RDSModel
from amundsen_rds.models.table import TableWatermark as RDSTableWatermark
from databuilder.models.graph_node import GraphNode
from databuilder.models.graph_relationship import GraphRelationship
from databuilder.models.graph_serializable import GraphSerializable
from databuilder.models.table_serializable import TableSerializable
class Watermark(GraphSerializable, TableSerializable):
"""
Table watermark result model.
Each instance represents one row of table watermark result.
"""
LABEL = 'Watermark'
KEY_FORMAT = '{database}://{cluster}.{schema}' \
'/{table}/{part_type}/'
WATERMARK_TABLE_RELATION_TYPE = 'BELONG_TO_TABLE'
TABLE_WATERMARK_RELATION_TYPE = 'WATERMARK'
def __init__(self,
create_time: str,
database: str,
schema: str,
table_name: str,
part_name: str,
part_type: str = 'high_watermark',
cluster: str = 'gold',
) -> None:
self.create_time = create_time
self.database = database
self.schema = schema
self.table = table_name
self.parts: List[Tuple[str, str]] = []
if '=' not in part_name:
raise Exception('Only partition table has high watermark')
# currently we don't consider nested partitions
idx = part_name.find('=')
name, value = part_name[:idx], part_name[idx + 1:]
self.parts = [(name, value)]
self.part_type = part_type
self.cluster = cluster
self._node_iter = self._create_node_iterator()
self._relation_iter = self._create_relation_iterator()
self._record_iter = self._create_next_record()
def create_next_node(self) -> Union[GraphNode, None]:
# return the string representation of the data
try:
return next(self._node_iter)
except StopIteration:
return None
def create_next_relation(self) -> Union[GraphRelationship, None]:
try:
return next(self._relation_iter)
except StopIteration:
return None
def create_next_record(self) -> Union[RDSModel, None]:
try:
return next(self._record_iter)
except StopIteration:
return None
def get_watermark_model_key(self) -> str:
return Watermark.KEY_FORMAT.format(database=self.database,
cluster=self.cluster,
schema=self.schema,
table=self.table,
part_type=self.part_type)
def get_metadata_model_key(self) -> str:
return f'{self.database}://{self.cluster}.{self.schema}/{self.table}'
def _create_node_iterator(self) -> Iterator[GraphNode]:
"""
Create watermark nodes
:return:
"""
for part in self.parts:
part_node = GraphNode(
key=self.get_watermark_model_key(),
label=Watermark.LABEL,
attributes={
'partition_key': part[0],
'partition_value': part[1],
'create_time': self.create_time
}
)
yield part_node
def _create_relation_iterator(self) -> Iterator[GraphRelationship]:
"""
Create relation map between watermark record with original table
:return:
"""
relation = GraphRelationship(
start_key=self.get_watermark_model_key(),
start_label=Watermark.LABEL,
end_key=self.get_metadata_model_key(),
end_label='Table',
type=Watermark.WATERMARK_TABLE_RELATION_TYPE,
reverse_type=Watermark.TABLE_WATERMARK_RELATION_TYPE,
attributes={}
)
yield relation
def _create_next_record(self) -> Iterator[RDSModel]:
"""
Create watermark records
"""
for part in self.parts:
part_record = RDSTableWatermark(
rk=self.get_watermark_model_key(),
partition_key=part[0],
partition_value=part[1],
create_time=self.create_time,
table_rk=self.get_metadata_model_key()
)
yield part_record