-
Notifications
You must be signed in to change notification settings - Fork 3
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
9 changed files
with
106 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
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) | ||
transformation: CollectionTransformation = self.project.get(address) | ||
# 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) | ||
transformation: CollectionTransformation = self.project.get(address) | ||
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 |
---|---|---|
|
@@ -56,3 +56,8 @@ Use `mongoimport`. | |
mongoimport --uri 'mongodb+srv://MYUSERNAME:[email protected]/test?retryWrites=true&w=majority' | ||
``` | ||
::: | ||
|
||
|
||
docker run -it --rm --network=host --volume ~/Downloads/applications_snp.quotes.json:/data/applications_snp.quotes.json mongo:7 mongoimport --db=testdrive --collection=carrier-quotes /data/applications_snp.quotes.json --jsonArray | ||
export CRATEDB_SQLALCHEMY_URL=crate://crate@localhost:4200/testdrive/carrier-quotes | ||
ctk load table mongodb://localhost:27017/testdrive/carrier-quotes |
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