Bases: BaseHWMStore
YAML local store for HWM values. Used as default HWM store. [
][DBR-onetl-hooks]
Parameters
path
Folder name there HWM value files will be stored.
Default:
* `~/.local/share/onETL/yml_hwm_store` on Linux
* `C:\Documents and Settings\<User>\Application Data\oneTools\onETL\yml_hwm_store` on Windows
* `~/Library/Application Support/onETL/yml_hwm_store` on MacOS
encoding
Encoding of files with HWM value
keep_history
Keep history of old HWM values in YAML file
Examples
Default parameters
from onetl.connection import Hive, Postgres
from onetl.db import DBReader, DBWriter
from onetl.strategy import IncrementalStrategy
from onetl.hwm.store import YAMLHWMStore
postgres = Postgres(...)
hive = Hive(...)
reader = DBReader(
connection=postgres,
source="public.mydata",
columns=["id", "data"],
hwm=DBReader.AutoDetectHWM(name="some_unique_name", expression="id"),
)
writer = DBWriter(connection=hive, target="db.newtable")
with YAMLHWMStore():
with IncrementalStrategy():
df = reader.run()
writer.run(df)
# will create file
# "~/.local/share/onETL/id__public.mydata__postgres_postgres.domain.com_5432__myprocess__myhostname.yml"
# with encoding="utf-8" and save a serialized HWM values to this file
With all options
with YAMLHWMStore(path="/my/store", encoding="utf-8", keep_history=True):
with IncrementalStrategy():
df = reader.run()
writer.run(df)
# will create file
# "/my/store/id__public.mydata__postgres_postgres.domain.com_5432__myprocess__myhostname.yml"
# with encoding="utf-8" and save a serialized HWM values to this file
File content example:
- column:
name: id
partition: {}
modified_time: '2023-02-11T17:10:49.659019'
process:
dag: ''
host: myhostname
name: myprocess
task: ''
source:
db: public
instance: postgres://postgres.domain.com:5432/target_database
name: mydata
type: int
value: '1500'
- column:
name: id
partition: {}
modified_time: '2023-02-11T16:00:31.962150'
process:
dag: ''
host: myhostname
name: myprocess
task: ''
source:
db: public
instance: postgres://postgres.domain.com:5432/target_database
name: mydata
type: int
value: '1000'
Source code in onetl/hwm/store/yaml_hwm_store.py
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 | @default_hwm_store_class
@register_hwm_store_class("yaml")
@support_hooks
class YAMLHWMStore(BaseHWMStore):
r"""YAML **local store** for HWM values. Used as default HWM store. [][DBR-onetl-hooks]
Parameters
----------
path
Folder name there HWM value files will be stored.
Default:
* `~/.local/share/onETL/yml_hwm_store` on Linux
* `C:\Documents and Settings\<User>\Application Data\oneTools\onETL\yml_hwm_store` on Windows
* `~/Library/Application Support/onETL/yml_hwm_store` on MacOS
encoding
Encoding of files with HWM value
keep_history
Keep history of old HWM values in YAML file
Examples
--------
Default parameters
```python
from onetl.connection import Hive, Postgres
from onetl.db import DBReader, DBWriter
from onetl.strategy import IncrementalStrategy
from onetl.hwm.store import YAMLHWMStore
postgres = Postgres(...)
hive = Hive(...)
reader = DBReader(
connection=postgres,
source="public.mydata",
columns=["id", "data"],
hwm=DBReader.AutoDetectHWM(name="some_unique_name", expression="id"),
)
writer = DBWriter(connection=hive, target="db.newtable")
with YAMLHWMStore():
with IncrementalStrategy():
df = reader.run()
writer.run(df)
# will create file
# "~/.local/share/onETL/id__public.mydata__postgres_postgres.domain.com_5432__myprocess__myhostname.yml"
# with encoding="utf-8" and save a serialized HWM values to this file
```
With all options
```python
with YAMLHWMStore(path="/my/store", encoding="utf-8", keep_history=True):
with IncrementalStrategy():
df = reader.run()
writer.run(df)
# will create file
# "/my/store/id__public.mydata__postgres_postgres.domain.com_5432__myprocess__myhostname.yml"
# with encoding="utf-8" and save a serialized HWM values to this file
```
File content example:
```yaml
- column:
name: id
partition: {}
modified_time: '2023-02-11T17:10:49.659019'
process:
dag: ''
host: myhostname
name: myprocess
task: ''
source:
db: public
instance: postgres://postgres.domain.com:5432/target_database
name: mydata
type: int
value: '1500'
- column:
name: id
partition: {}
modified_time: '2023-02-11T16:00:31.962150'
process:
dag: ''
host: myhostname
name: myprocess
task: ''
source:
db: public
instance: postgres://postgres.domain.com:5432/target_database
name: mydata
type: int
value: '1000'
```
"""
path: LocalPath = DATA_PATH / "yml_hwm_store"
encoding: str = "utf-8"
keep_history: bool = True
ITEMS_DELIMITER_PATTERN: ClassVar[re.Pattern] = re.compile("[#@|]+")
PROHIBITED_SYMBOLS_PATTERN: ClassVar[re.Pattern] = re.compile(r"[=:/\\]+")
if hasattr(BaseHWMStore, "model_config"):
# pydantic v2
model_config: ClassVar = {"frozen": True, "extra": "forbid"}
else:
class Config:
# pydantic v1
frozen = True
extra = "forbid"
def __init__(
self,
*,
path: os.PathLike | str = DATA_PATH / "yml_hwm_store",
encoding="utf-8",
keep_history: bool = True,
):
path = LocalPath(path).expanduser().resolve()
path.mkdir(parents=True, exist_ok=True)
super().__init__(
path=path,
encoding=encoding,
keep_history=keep_history,
)
@slot
def get_hwm(self, name: str) -> HWM | None:
"""Get HWM value by name."""
data = self._load(name)
if not data:
return None
latest = sorted(data, key=operator.itemgetter("modified_time"))[-1] # noqa: FURB192
return HWMTypeRegistry.parse(latest)
@slot
def set_hwm(self, hwm: HWM) -> LocalPath:
"""Save HWM value. Returns path to the YAML file."""
data = self._load(hwm.name)
to_save: list[dict] = [hwm.serialize()]
if self.keep_history:
to_save.extend(data)
self._dump(hwm.name, to_save)
return self.get_file_path(hwm.name)
@classmethod
def cleanup_file_name(cls, name: str) -> str:
# id|partition=value#db.table@proto://instance#process@host
# ->
# id__partition__value__db.table__proto_instance__process__host
result = cls.ITEMS_DELIMITER_PATTERN.sub("__", name)
result = cls.PROHIBITED_SYMBOLS_PATTERN.sub("_", result)
return re.sub("_{2,}", "__", result)
def get_file_path(self, name: str) -> LocalPath:
file_name = self.cleanup_file_name(name)
return self.path / f"{file_name}.yml"
def _load(self, name: str) -> list[dict]:
path = self.get_file_path(name)
if not path.exists():
return []
with path.open("r", encoding=self.encoding) as file:
return yaml.safe_load(file)
def _dump(self, name: str, data: list[dict]) -> None:
path = self.get_file_path(name)
with path.open("w", encoding=self.encoding) as file:
yaml.dump(data, file)
|
__init__(*, path=DATA_PATH / 'yml_hwm_store', encoding='utf-8', keep_history=True)
Source code in onetl/hwm/store/yaml_hwm_store.py
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184 | def __init__(
self,
*,
path: os.PathLike | str = DATA_PATH / "yml_hwm_store",
encoding="utf-8",
keep_history: bool = True,
):
path = LocalPath(path).expanduser().resolve()
path.mkdir(parents=True, exist_ok=True)
super().__init__(
path=path,
encoding=encoding,
keep_history=keep_history,
)
|
get_hwm(name)
Get HWM value by name.
Source code in onetl/hwm/store/yaml_hwm_store.py
186
187
188
189
190
191
192
193
194
195 | @slot
def get_hwm(self, name: str) -> HWM | None:
"""Get HWM value by name."""
data = self._load(name)
if not data:
return None
latest = sorted(data, key=operator.itemgetter("modified_time"))[-1] # noqa: FURB192
return HWMTypeRegistry.parse(latest)
|
set_hwm(hwm)
Save HWM value. Returns path to the YAML file.
Source code in onetl/hwm/store/yaml_hwm_store.py
197
198
199
200
201
202
203
204
205 | @slot
def set_hwm(self, hwm: HWM) -> LocalPath:
"""Save HWM value. Returns path to the YAML file."""
data = self._load(hwm.name)
to_save: list[dict] = [hwm.serialize()]
if self.keep_history:
to_save.extend(data)
self._dump(hwm.name, to_save)
return self.get_file_path(hwm.name)
|