top of page
Search

"Seamless ELT to MongoDB!"

Updated: Oct 13, 2025


Overview

This example demonstrates a simple and efficient way to move HR master data from SAP into MongoDB using ABAP and Python. The goal is to show how SAP can drive a full Extract–Load–Transform (ELT) process without external middleware.

ABAP handles the extraction and JSON conversion, while Python takes care of the load and transformation logic inside MongoDB. This approach provides a lightweight integration that can easily be extended to other SAP modules.


ABAP Program – Extract and Trigger Python

REPORT zsap_hr_data_to_mongo.

DATA: lt_hr_data  TYPE TABLE OF pa0002,
      lv_json     TYPE string,
      lv_filename TYPE string VALUE '/usr/sap/tmp/sap_hr_data.json',
      lv_script   TYPE string VALUE '/usr/sap/scripts/load_transform_mongodb.py',
      lv_status   TYPE sxpgcolist,
      lv_exitcode TYPE i,
      lt_protocol TYPE TABLE OF btcxpm,
      ls_protocol TYPE btcxpm.

" Step 1: Extract HR data from PA0002
SELECT * FROM pa0002 INTO TABLE lt_hr_data WHERE pernr = '00000001'.
IF sy-subrc <> 0.
  WRITE: / 'No HR data found for the selected personnel number.'.
  EXIT.
ENDIF.

" Step 2: Convert internal table to JSON
TRY.
    lv_json = /ui2/cl_json=>serialize(
      data        = lt_hr_data
      pretty_name = /ui2/cl_json=>pretty_mode-low_case ).
  CATCH cx_root INTO DATA(lx_json).
    WRITE: / 'Error during JSON conversion:', lx_json->get_text( ).
    EXIT.
ENDTRY.

" Step 3: Write JSON to application server file
OPEN DATASET lv_filename FOR OUTPUT IN TEXT MODE ENCODING UTF-8.
IF sy-subrc <> 0.
  WRITE: / 'Unable to open file:', lv_filename.
  EXIT.
ENDIF.
TRANSFER lv_json TO lv_filename.
CLOSE DATASET lv_filename.
WRITE: / 'HR data saved to:', lv_filename.

" Step 4: Execute Python script through external command (SM69)
CALL FUNCTION 'SXPG_COMMAND_EXECUTE'
  EXPORTING
    commandname           = 'Z_RUN_PYTHON'
    additional_parameters = lv_script && ' ' && lv_filename
  IMPORTING
    status                = lv_status
    exitcode              = lv_exitcode
  TABLES
    exec_protocol         = lt_protocol
  EXCEPTIONS
    no_permission         = 1
    command_not_found     = 2
    program_start_error   = 3
    program_termination_error = 4
    OTHERS                = 5.

IF sy-subrc = 0 AND lv_exitcode = 0.
  WRITE: / 'Python script executed successfully.'.
ELSE.
  WRITE: / 'Python execution failed. Exit code:', lv_exitcode.
  LOOP AT lt_protocol INTO ls_protocol.
    WRITE: / ls_protocol-message.
  ENDLOOP.
ENDIF.

SAP Configuration Notes

  1. External Command Setup (SM69)

    • Define an external OS command named Z_RUN_PYTHON.

    • Command: /usr/bin/python3 or your actual Python path (C:\Python312\python.exe).

    • Allow Additional Parameters.

    • The SAP application server user must have OS permission to execute Python.

  2. File Paths

    • Use directories accessible by the SAP application server.

    • For Linux, use /usr/sap/tmp/; for Windows, adjust paths accordingly.

  3. JSON Conversion

    • This version uses the standard class /UI2/CL_JSON available in NetWeaver 7.40 and higher.

    • It replaces the older, non-standard JSON conversion routine.


Python Script – Load and Transform Data

File: /usr/sap/scripts/load_transform_mongodb.py

import json
import sys
from pymongo import MongoClient

if len(sys.argv) < 2:
    print("Usage: python load_transform_mongodb.py <json_file>")
    sys.exit(1)

json_path = sys.argv[1]
print(f"Loading SAP HR data from {json_path}...")

# Step 1: Connect to MongoDB
client = MongoClient("mongodb://localhost:27017/")
db = client["sap_hr_database"]
collection = db["employee_data"]

# Step 2: Load JSON data
with open(json_path, 'r', encoding='utf-8') as file:
    hr_data = json.load(file)

# Step 3: Insert into MongoDB
if isinstance(hr_data, list):
    collection.insert_many(hr_data)
else:
    collection.insert_one(hr_data)
print("SAP HR data successfully loaded into MongoDB.")

# Step 4: Transform data - add full_name field
for employee in collection.find():
    first = employee.get('vornam', '')
    last = employee.get('nachn', '')
    full_name = f"{first.strip()} {last.strip()}".strip()
    collection.update_one(
        {"_id": employee["_id"]},
        {"$set": {"full_name": full_name}}
    )

print("Data transformation completed. Field 'full_name' added.")

Process Summary

Phase

Description

Component

Extract

ABAP pulls data from table PA0002

SAP

Convert to JSON

/UI2/CL_JSON serializes internal table

ABAP

Write to File

Data saved to application server

ABAP

Load

Python script reads JSON and loads into MongoDB

Python

Transform

Adds computed field full_name

Python

Why This Matters

This method provides a simple, direct bridge between SAP and a NoSQL environment. It avoids heavy middleware and demonstrates how Python can complement ABAP for lightweight data engineering.For analytics teams, it’s an easy way to make SAP data available for dashboards or AI models without altering core SAP logic.



 
 
 

Comments


Featured Blog Post

bottom of page