Skip to content

Commit

Permalink
MongoDB: Add support for UUID types
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed Sep 2, 2024
1 parent dd27c30 commit 349c75d
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

## Unreleased
- MongoDB: Rename columns with leading underscores to use double leading underscores
- MongoDB: Add support for UUID types

## 2024/09/02 v0.0.21
- DynamoDB: Add special decoding for varied lists.
Expand Down
5 changes: 5 additions & 0 deletions cratedb_toolkit/io/mongodb/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
Export the documents from a MongoDB collection as JSON, to be ingested into CrateDB.
"""

import base64
import calendar
import typing as t
from uuid import UUID

import bsonjs
import dateutil.parser as dateparser
Expand Down Expand Up @@ -65,6 +67,9 @@ def extract_value(value, parent_type=None):
"""
if isinstance(value, dict):
if len(value) == 1:
if "$binary" in value and value["$binary"]["subType"] in ["03", "04"]:
decoded = UUID(bytes=base64.b64decode(value["$binary"]["base64"]))
return extract_value(decoded, parent_type)
for k, v in value.items():
if k.startswith("$"):
return extract_value(v, k.lstrip("$"))
Expand Down
4 changes: 4 additions & 0 deletions cratedb_toolkit/io/mongodb/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import typing as t

import bson
from bson import OLD_UUID_SUBTYPE, UUID_SUBTYPE
from pymongo.collection import Collection
from rich import progress
from rich.console import Console
Expand Down Expand Up @@ -197,4 +198,7 @@ def get_type(value):
return "INTEGER"
else:
return "INT64"
if type_ is bson.binary.Binary:
if value.subtype in [OLD_UUID_SUBTYPE, UUID_SUBTYPE]:
return "UUID"
return TYPES_MAP.get(type_, "UNKNOWN")
1 change: 1 addition & 0 deletions cratedb_toolkit/io/mongodb/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

TYPES = {
"OID": "TEXT",
"UUID": "TEXT",
"DATETIME": "TIMESTAMP WITH TIME ZONE",
"TIMESTAMP": "TIMESTAMP WITHOUT TIME ZONE",
"INT64": "BIGINT",
Expand Down

0 comments on commit 349c75d

Please sign in to comment.