Skip to main content

MongoDB → Solr Sync

Prefect-3-basiertes Projekt, das Daten aus MongoDB nach Solr synchronisiert. Sämtlicher Code liegt im Python-Paket mongodbsolr unter src/mongodbsolr.

Das Paket wird als Python-Distribution unter dem Namen slubprefect-mongodbsolr veröffentlicht und kann bei Bedarf über PyPI installiert werden.

🎯 Zielgruppe & Einsatzszenario

  • Primär: internes Developer-Team der SLUB Dresden (Datenmanagement / DMG)
  • Typischer Einsatz: periodische oder ad-hoc Synchronisation von Dokumenten aus MongoDB in einen Solr-Core
  • Infrastruktur: Prefect 3, MongoDB-Cluster, Solr-Instanz und Prefect-Server

🚀 Schnellstart

1. Umgebung einrichten

uv sync
source .venv/bin/activate

2. Secrets & Konfiguration

export MONGODB_URI="mongodb://localhost:27017"
export SOLR_URL="http://localhost:8983/solr"
python scripts/create_mongodbsolr_config.py create-secrets

python scripts/create_mongodbsolr_config.py create-variable \
  --mongodb-db my_database \
  --mongodb-collection my_collection \
  --mongodb-query '{"status": "published"}' \
  --solr-core my_core \
  --mongodb-id-field record_id \
  --solr-id-field id

Struktur der Prefect-Variable mongodbsolr-config

Die Variable mongodbsolr-config enthält ein JSON-Objekt mit mehreren Blöcken:

{
  "mongodb": {
    "uri_block": "mongodbsolr-mongodb-uri",
    "database": "...",
    "collection": "...",
    "query": "...",
    "batch_size": 100
  },
  "solr": {
    "url_block": "mongodbsolr-solr-url",
    "core": "my_core",
    "commit_within": 5000,
    "max_concurrent_upserts": 2,
    "finalcommit": false
  },
  "mapping": {
    "mongodb_id_field": "record_id",
    "solr_id_field": "id",
    "exclude_fields": ["_id", "_rev", "_attachments"],
    "required_fields": []
  },
  "mode": "upsert",
  "delete_query": "status:published"
}

Felder im Überblick:

  • mongodb.batch_size – Anzahl Dokumente pro gelesenem Batch aus MongoDB (Standard: 100).
  • solr.commit_within – commitWithin in Millisekunden, das an Solr übergeben wird (Standard: 5000).
  • solr.max_concurrent_upserts (optional) – maximale Anzahl paralleler Solr-Upsert-Tasks im Flow. Wenn nicht gesetzt, wird 2 verwendet.
  • solr.finalcommit (optional) – steuert, ob am Ende des Flows ein expliziter commit gegen Solr ausgeführt wird:
    • false (Standard): nur commitWithin wird verwendet, Solr entscheidet selbst, wann Commits passieren.
    • true: nach Abschluss aller Upserts/Deletes wird zusätzlich ein finaler Commit ausgelöst.

Hinweis: mode und delete_query können wie bisher verwendet werden (upsert, delete-before-import, delete-only).

3. Flow ausführen

python -m mongodbsolr.flows.cli sync --config mongodbsolr-config

Optionale Flags:

Flag Wirkung
--mode Überschreibt den Modus (upsert, delete-before-import, delete-only)
--explain-indexes Führt explain('executionStats') aus und prüft Indexnutzung

📦 Installation als Paket

Das Paket wird auf PyPI unter dem Namen slubprefect-mongodbsolr veröffentlicht.

pip install slubprefect-mongodbsolr

Anschließend kann das CLI wie oben beschrieben mit python -m mongodbsolr.flows.cli genutzt werden.

🧱 Struktur

src/
└── mongodbsolr/
    ├── flows/
    │   ├── cli.py             # CLI-Entry für Prefect Flows
    │   └── sync_flow.py       # Haupt-Flow mongodb_to_solr_sync
    ├── tasks/
    │   ├── mongodb.py         # MongoDB-spezifische Tasks
    │   └── solr.py            # Solr-spezifische Tasks
    └── utils/
        ├── config.py          # Prefect Variables & Secret Blocks laden
        └── validation.py      # Pflichtfeldprüfung & Feldfilterung

scripts/
├── create_mongodbsolr_config.py   # Secrets/Variable anlegen
├── deploy_mongodbsolr_eth.py      # Beispiel-Deployment
└── inspect_prefect_variable.py    # Debugging von Prefect-Variablen

doc/plan.md                        # Architektur- und Roadmap-Dokument
prefect.yaml                       # Deployments

⚙️ Flow & Tasks

  • flows/sync_flow.py: Implementiert den Flow mongodb_to_solr_sync, erzeugt Prefect-Artifacts und koordiniert Modi upsert, delete-before-import, delete-only.
  • tasks/mongodb.py: Optionaler Explain-Check, Cursor-Fetching, Batch-Verarbeitung, ID-Extraktion.
  • tasks/solr.py: Delete-by-Query, Delete-by-IDs, Upsert-Batches und Commit.
  • utils/config.py: Lädt Prefect Variable, validiert Inhalte und löst Secret Blocks auf.
  • utils/validation.py: Pflichtfeld-Validierung, ID-Mapping, Feldfilterung.

🧪 Tests & Linting

ruff format .
ruff check .
python -m mongodbsolr.flows.cli test-config --config mongodbsolr-config

📦 Deployments

Standardflow: mongodbsolr.flows.sync_flow:mongodb_to_solr_sync (siehe prefect.yaml).

Beispiel-Deployment erstellen:

python scripts/deploy_mongodbsolr_eth.py

oder direkt via Prefect CLI:

prefect deploy --name mongodbsolr-sync

Spezielles Deploy-Skript für mongodbsolr

python deploy.py --dockerfile Dockerfile.yml --image-repo registry.git.slub-dresden.de/metadata/mongodbsolr

Parameter:

Flag Beschreibung
--tag Optionaler Image-Tag (Standard: Zeitstempel UTC)
--no-push Überspringt Push ins Registry
--deployment-name Prefect-Deployment-Name (Standard: mongodbsolr-sync)
--log-level Log-Level für das Skript

📚 Weitere Quellen

📝 Lizenz

Siehe LICENSE im Repository.

Metadata

Release files for slubprefect-mongodbsolr 1.0.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for slubprefect-mongodbsolr 1.0.6
File Size Uploaded
slubprefect_mongodbsolr-1.0.6.tar.gz 15.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for slubprefect-mongodbsolr 1.0.6
File Interpreter ABI Platform
slubprefect_mongodbsolr-1.0.6-py3-none-any.whl Python 3 none any Details

Total release size: 35.7 kB

Release files / slubprefect_mongodbsolr-1.0.6.tar.gz

Download URL slubprefect_mongodbsolr-1.0.6.tar.gz
Size 15.6 kB
Tags Source
SHA-256 checksum
How to use checksums
b2c9ab9f07a8fe9383731549ba2b064d39e1e51a9a1277f76315781e0792b179
BLAKE2b-256 checksum
How to use checksums
f1f28a7c781b26f5250057068190bfd6505c0bf9702053f3137c6627710fcaee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.10.6 {"installer":{"name":"uv","version":"0.10.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"KDE neon","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / slubprefect_mongodbsolr-1.0.6-py3-none-any.whl

Download URL slubprefect_mongodbsolr-1.0.6-py3-none-any.whl
Size 20.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2b5a4843c1e6169649652051fc029e8ea90eecdeab7296f18a95b55be6e5775b
BLAKE2b-256 checksum
How to use checksums
5c6889882f1ceedfe241a6b29d850572e6940ba18a5fa399153ebf4c99839102
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.10.6 {"installer":{"name":"uv","version":"0.10.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"KDE neon","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release history Release notifications | RSS feed

This release

1.0.6 This release

2 release files

1.0.5

2 release files

1.0.3

2 release files

1.0.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page