-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MongoDB: Add capability to give type hints and add transformations
- Loading branch information
Showing
12 changed files
with
145 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import logging | ||
import typing as t | ||
from pathlib import Path | ||
|
||
from jsonpointer import JsonPointer | ||
from zyp.model.collection import CollectionAddress, CollectionTransformation | ||
from zyp.model.project import TransformationProject | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class TransformationManager: | ||
def __init__(self, path: Path): | ||
self.path = path | ||
self.active = False | ||
if not self.path: | ||
return | ||
if not self.path.exists(): | ||
raise FileNotFoundError(f"File does not exist: {self.path}") | ||
self.project = TransformationProject.from_yaml(self.path.read_text()) | ||
logger.info("Transformation manager initialized. File: %s", self.path) | ||
self.active = True | ||
|
||
def apply_type_overrides(self, database_name: str, collection_name: str, collection_schema: t.Dict[str, t.Any]): | ||
if not self.active: | ||
return | ||
address = CollectionAddress(database_name, collection_name) | ||
try: | ||
transformation: CollectionTransformation = self.project.get(address) | ||
except KeyError: | ||
return | ||
logger.info(f"Applying type overrides for {database_name}/{collection_name}") | ||
# TODO: Also support addressing nested elements. | ||
# Hint: Implementation already exists on another machine, | ||
# where it has not been added to the repository. Sigh. | ||
for rule in transformation.schema.rules: | ||
pointer = JsonPointer(f"/document{rule.pointer}/types") | ||
type_stats = pointer.resolve(collection_schema) | ||
type_stats[rule.type] = 1e10 | ||
|
||
def apply_transformations(self, database_name: str, collection_name: str, data: t.Dict[str, t.Any]): | ||
if not self.active: | ||
return data | ||
address = CollectionAddress(database_name, collection_name) | ||
try: | ||
transformation: CollectionTransformation = self.project.get(address) | ||
except KeyError: | ||
return data | ||
return transformation.bucket.apply(data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,10 +49,20 @@ ctk shell --command "SELECT * FROM testdrive.demo;" | |
ctk show table "testdrive.demo" | ||
``` | ||
|
||
## Using Zyp transformations | ||
You can use [Zyp transformations] to change the shape of the data while being | ||
transferred. In order to add it to the pipeline, use the `--transformation` | ||
command line option on the `migr8 extract` and `migr8 export` commands. | ||
|
||
You can find an example file at `examples/zyp-transformation.yaml`. | ||
|
||
|
||
:::{todo} | ||
Use `mongoimport`. | ||
```shell | ||
mongoimport --uri 'mongodb+srv://MYUSERNAME:[email protected]/test?retryWrites=true&w=majority' | ||
``` | ||
::: | ||
|
||
|
||
[Zyp transformations]: https://commons-codec.readthedocs.io/zyp/index.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
meta: | ||
type: zyp-project | ||
version: 1 | ||
collections: | ||
- address: | ||
container: testdrive-db | ||
name: foobar-collection | ||
schema: | ||
rules: | ||
- pointer: /some_date | ||
type: DATETIME | ||
- pointer: /another_date | ||
type: DATETIME | ||
bucket: | ||
values: | ||
rules: | ||
- pointer: /some_date | ||
transformer: to_unixtime | ||
- pointer: /another_date | ||
transformer: to_unixtime |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters