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.3.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.3-py3-none-any.whl (20.1 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for slubprefect_mongodbsolr-1.0.3.tar.gz
Algorithm Hash digest
SHA256 0de3616775f2184527ed0563d89107f491a8e3442b011b181794721836abf652
MD5 9477f2ba6c1110f3730e66f3baeefe36
BLAKE2b-256 bf5992bd74829f6348fae58ffe4208fbf918812ed6a45d48b16ceb0cab2c65a8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for slubprefect_mongodbsolr-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 c7aaf3b29ddf734356b4fec9c5b5c712691ce25a9c8aaa70ea8b7e5e1f8734c6
MD5 0b187a7407a541e55b9507d8ac1afc80
BLAKE2b-256 42a639e3230a7213af340299fa6ee7db9c8e4e2ae9ffa7a2e209be145d96cd91

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