Skip to main content

Prefect 3.4 Flow-Paket für MongoDB→Solr Synchronisation

Project description

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_withincommitWithin 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

slubprefect_mongodbsolr-1.0.6.tar.gz (15.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

slubprefect_mongodbsolr-1.0.6-py3-none-any.whl (20.1 kB view details)

Uploaded Python 3

File details

Details for the file slubprefect_mongodbsolr-1.0.6.tar.gz.

File metadata

  • Download URL: slubprefect_mongodbsolr-1.0.6.tar.gz
  • Upload date:
  • Size: 15.6 kB
  • Tags: Source
  • Uploaded using 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}

File hashes

Hashes for slubprefect_mongodbsolr-1.0.6.tar.gz
Algorithm Hash digest
SHA256 b2c9ab9f07a8fe9383731549ba2b064d39e1e51a9a1277f76315781e0792b179
MD5 a48fa48affe0363e7eb6575dd88af976
BLAKE2b-256 f1f28a7c781b26f5250057068190bfd6505c0bf9702053f3137c6627710fcaee

See more details on using hashes here.

File details

Details for the file slubprefect_mongodbsolr-1.0.6-py3-none-any.whl.

File metadata

  • Download URL: slubprefect_mongodbsolr-1.0.6-py3-none-any.whl
  • Upload date:
  • Size: 20.1 kB
  • Tags: Python 3
  • Uploaded using 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}

File hashes

Hashes for slubprefect_mongodbsolr-1.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 2b5a4843c1e6169649652051fc029e8ea90eecdeab7296f18a95b55be6e5775b
MD5 027f7f45e6702f8423c90075f39b3e66
BLAKE2b-256 5c6889882f1ceedfe241a6b29d850572e6940ba18a5fa399153ebf4c99839102

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page