Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Recopie MS-SQL vers PostgreSQL

APERTO-NOTA

Principe

Une route Camel interroge périodiquement MS-SQL via JDBC, transforme chaque ligne en Map, puis insère les données dans PostgreSQL.

Timer → SQL SELECT (MS-SQL) → Split → SQL INSERT (PostgreSQL)

Configuration des datasources

Créer le fichier camel/application.properties :

# Datasource MS-SQL
camel.datasource.mssql.url=jdbc:sqlserver://mssql:1433;databaseName=sourcedb;encrypt=false
camel.datasource.mssql.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
camel.datasource.mssql.username=sa
camel.datasource.mssql.password=Str0ngPass!

# Datasource PostgreSQL
camel.datasource.postgres.url=jdbc:postgresql://postgres:5432/targetdb
camel.datasource.postgres.driver-class-name=org.postgresql.Driver
camel.datasource.postgres.username=camel
camel.datasource.postgres.password=camel

Route Camel (YAML)

Créer le fichier camel/routes/mssql-to-pg.camel.yaml :

- route:
    id: mssql-to-postgres
    description: Recopie de la table produits de MS-SQL vers PostgreSQL
    from:
      uri: timer:sync
      parameters:
        period: 60000       # toutes les 60 secondes
        repeatCount: 1      # exécution unique (retirer pour répétition)
    steps:
      - log:
          message: "Démarrage de la synchronisation MS-SQL → PostgreSQL"

      # Lecture source MS-SQL
      - to:
          uri: jdbc:mssql
          parameters:
            useHeadersAsParameters: true
          body: "SELECT id, nom, prix, actif FROM dbo.produits"

      # Éclatement ligne par ligne
      - split:
          expression:
            simple: "${body}"
          steps:
            # Transformation des types (BIT → BOOLEAN)
            - setBody:
                expression:
                  groovy: |
                    def row = request.body
                    row['actif'] = row['actif'] == 1
                    return row

            # Insertion dans PostgreSQL (upsert)
            - to:
                uri: jdbc:postgres
                body: >
                  INSERT INTO produits (id, nom, prix, actif)
                  VALUES (:#id, :#nom, :#prix, :#actif)
                  ON CONFLICT (id) DO UPDATE
                  SET nom = EXCLUDED.nom,
                      prix = EXCLUDED.prix,
                      actif = EXCLUDED.actif

      - log:
          message: "Synchronisation terminée"

Exécution et vérification

Lancer la route depuis Karavan

  1. Ouvrir http://localhost:8080

  2. Naviguer vers camel/routes/mssql-to-pg.camel.yaml

  3. Cliquer Run

Vérifier le résultat dans PostgreSQL

docker exec -it camel-postgres psql -U camel -d targetdb \
  -c "SELECT * FROM produits;"

Résultat attendu :

 id |   nom    | prix  | actif
----+----------+-------+-------
  1 | Article A| 10.50 | t
  2 | Article B| 25.00 | t
  3 | Article C |  7.99 | t

Conversions de types MS-SQL → PostgreSQL

MS-SQLPostgreSQLRemarque
NVARCHARTEXTPas de limite à déclarer
DATETIMETIMESTAMPFormat ISO 8601 automatique
BITBOOLEANConversion explicite nécessaire
DECIMALNUMERICPrécision identique
INT IDENTITYINTEGERPas d’auto-increment côté cible
UNIQUEIDENTIFIERUUIDCast via ::uuid si besoin

Gestion de la volumétrie

Pour les tables de grande taille, ajouter la pagination côté MS-SQL :

body: >
  SELECT id, nom, prix, actif
  FROM dbo.produits
  ORDER BY id
  OFFSET :#offset ROWS FETCH NEXT 1000 ROWS ONLY

Piloter :#offset via une propriété Camel incrémentée à chaque itération.


Points d’attention