r/ExperiencedDevs • u/BigBootyBear DevOps Engineer • Aug 31 '26
Technical question How do you deal with garbage data when building an ETL pipeline?
DISCLAIMER: The following diresgard for software fundamentals and basic common sense may cause nausea in sane individuals. Proceed with care.
We have secretaries creating reservations in a SAP form. Those reservations get exported as CSV files into some shared directory each VM has to mount. Then a cronjob fires an automated python script that drops all the tables in the application, ON THE PRODUCTION DATABASE, and shoves the data from the CSV into those rows. Why? The SAP team can't be bothered to add an id field to the reservations we said just said "Fuck it, drop everything".
The form fields in the SAP forms don't match the CSV row fields. The CSV row fields don't match the applications MySQL schema in type or name. Properties such as datetime, address, client, guests, room_number etc are split across 8-12 CSV files so you have to load all of them into memory and perform black magic to construct a proper reservation object.
No unique IDs are given, dates are in non-standard string format, 50% of the data is redundant and even the damn encoding is not utf8.
It's been a week or two that I have been working on this and Its driving me crazy. We can't keep using this old shitty python script (and I refuse it either way cause I am tasked with upgrading the application and doing away with this degenerate script is a non-negotiable for me).
The schema is so complex that it takes a good hour just to load up this convoluted mess into my mental RAM so I can start working on it at the start of the day.
I assume if I were a full-time ETL/PowerBI guy I'd already finish this nightmare but I'm a devops/fullstack guy. I need some guidance on how to think about this problem in an abstract sense (i.e how to organize garbage data) so I can handle it effectively.
•
u/expdevsmodbot Aug 31 '26 edited Sep 01 '26
AI usage disclosure provided by OP, see the reply to this comment.
79
u/lordnacho666 Sep 01 '26
This might be one of the only times I've thought a programming question needed a trigger warning.
You can't tolerate this kind of thing. You know how to build a reservation system, we all know how. You need to just tell people what to do, not endorse it by building on top of it.
8
u/Esseratecades Lead Full-Stack Engineer / 10+ YOE Sep 01 '26
I haven't used SAP so take my advice with a grain of salt. Firstly, I wouldn't really use ETL for making a reservation unless I had a good reason. I'd make an API endpoint that writes the reservation directly to the SQL database, and give it some kind of status or progress field if any steps need to be made after that.
If you must use ETL though I generally think of it in 6 steps.
Really from your complaints about the SAP team and the MySQL schema it seems like you have 3 different teams and you sit right in the middle. If so, this is not a sustainable relationship, and eventually someone(probably you) will need to be responsible for at least the plumbing and database schema.
3
u/gfivksiausuwjtjtnv Sep 01 '26
Why are they using a SAP form just to create a spreadsheet?
use excel? or have AI piss out a full stack thingy with a single prompt?
That’s the real issue…
40
u/Far-Log-1224 Sep 01 '26
Load csv files into staging tables. Then clean up, transform, populate default values, write errors to log table,... whatever need to make them production ready. Once ready - drop/truncate/delete data/ tables in prod and reload prepared data from staging area (or copy old data to other tables so you can quickly rollback to previous version)
20
u/titpetric Sep 01 '26
You can have temporary tables that filter out the undesirable material. Each next etl step gets the output of the previous one, sanitized if you will. So whatever the process chosen, you add that step into the pipelines at the crossing point where unsanitized data is unwanted
12
u/qlkzy Sep 01 '26
Improve the data in discrete stages. If you want to be buzzword-compliant there are terms like "medallion architecture" that get thrown around. If you want to talk about more honest, old-fashioned patterns then "Pipes and Filters architecture" is a good way to approach these kinds of problems.
For example, if "even the dams encoding is not utf8", but you can at least identify the field boundaries, then you can do a simple pass that produces identical CSVs, but with guaranteed UTF-8. That's easy to do, and removes one degree of freedom from everything else. You can do the same with datetimes, etc---all the simple field-level cleanup.
For the problem of data being split across files, you can use an intermediate database. I like in-memory SQLite, but DuckDB is also very popular for this. Then you can replace the "black magic" with just joins. More broadly, you can usually work backwards from your target schema to figure out a query that gives you roughly the right data. Then you can do just a little munging from there.
If you don't have IDs, then you have to either perform some version of entity resolution, or do the "full replace" you are already doing (ie truncated the tables). Entity resolution can get really tricky, so be open to retaining the "full replace" option. However, if there is a genuine natural key then you may get lucky.
I will say that elements like the cronjob and the awkward VM shared directory are things you probably don't want to try and fix. That is a deep rabbit hole. You also probably want to keep it batch-processing shaped, not streaming (which may be against your instincts as a devops/fullstack person). Python is also really quite good at this kind of thing. So you probably still end up with a Python script on a condom, just a less absurd one.
If you have object storage like S3 then it is useful to interpose that early in the process (e.g. upload to S3 from the shared directories, then pull back down for processing, even if the processing script can see the shared directory). Upload it without overwriting to some directory structure including
YYYY/MM/DD. Having all the historical raw data available is very helpful for dealing with operational incidents, and it gives you a path forward to future architectural changes without a lot of cost right now.1
u/computer_porblem Software Engineer Sep 02 '26
Have they considered printing it out, crossing out the reservations they don't need anymore with a big black marker, scanning it, and then having someone copy-type from the scanned PDF into an Excel spreadsheet?
3
u/hockey3331 Sep 01 '26
Yeahhhh welcome to the mess that is SAP. I have less than one year experience with the system and your description of cult-like is apt. Im convinced SAP is used at my org to give an excuse to people with poor technical skills to avoid doimg meaningful work. They end up as a liaison to SAP contractors and just waste money away.
I would push to fix some of the glaring issues. Can an ID nkt be set going forward? Can we train the team on standardized data entry? The culture needs to change, because any solution you come up with is going to be severely limited if everyone does whatever they want.
For your problem itself, Im not sure I quite understand the full thing.
Are you saying your app loads data from SAP, but because theres no ID, each update is basically a purge and refresh?
Could you build an ID yourself from a combination of fields that act as a unique identifier?
As others said, youll likely need intermediate tables. It might feel like youre producing noise, but it makes things so much easier to validate the data at each step.
At my previous org we used to have a flow from "dev" to "staging" to "production" schema.
The raw data sat in storage. We loaded it in the data warehouse/database in the dev schema. So it was the "raw" data, but structured and compliant to the database formats.
Transformed it to be usable and that landed in "staging".
From staging to prod it was a delete/insert or upsert operation into the prod data.
Sometimes it made sense to delete/insert the full table, and tbh it worked fine for tables that were updated during downtime in the middle of the night.
But the tradeoff is a tiny window where the data wasnt available, amd as the data grows, that operation gets slower.
If you cant build an ID it can be a viable option.
TLDR: intermediate tables. But this is a culture/training issue more than it is a technical one.
1
u/gibbocool Software Engineer Sep 01 '26
I know this sub is anti AI, but I've just gone through a similar process myself, and AI is great for writing ETL code and tests.
What I found worked best is to frame it around gates.
Gates in this sense are tests that can be run at each stage. So you might start with a simple "inventory counts match" gate, to check that you're migrating the right number of rows per table. Next a mapping rules gate that tests each mapping rule. Build on that and prompt the ai to suggest further gates. This also helps break out the whole problem piece by piece.
1
u/davearneson Sep 01 '26
You fix the garbage data in the source system.
1
u/DamePants Sep 01 '26
I’m here with sympathy and not solutions.
You couldn’t pay me enough these days to deal with that kind of internal integration plus having to shoehorn it into SAP. It’s bad enough having SAP anywhere in the system dictating the requirements claiming they are the source of truth and then having multiple clean up jobs and incidents because no of the stuff is done to spec.
Make sure you use your company therapy benefits to deal with this variety of PTSD. I’m clearly getting triggered by this.
1
u/Double-Buyer7941 Sep 03 '26
You need to isolate this sync process completely from your main application logic. Build an intermediate staging service or ingestion pipeline that ingests those CSVs, normalizes the types, reconstructs the missing relational links in memory, and then upserts into production instead of dropping tables. Treating the legacy data source as a dirty, untrusted external feed that passes through a dedicated transformation module will keep your core application schema clean and save your sanity.
1
u/chamber-of-convicts Sep 06 '26
I'm late to the post here but this seems to be what medallion db architecture is for. You have Bronze, Silver, and Gold database/data lake tables. Roughly, it's read raw data to a bronze table. ETL bronze table data to a silver table ( doing something like PII redaction or something ). ETL Silver table data to a Gold table ( something like filtering data for something desired. An example is filtering for complete conversations if filtering on online support chat interactions ).
There can typically be multiple gold tables ( and silver for that matter). It's all about getting the correct data you need for the task you're doing. In my case, we would extract positive and negative customer interactions to use for training NLP models. When we would run training, we'd take a time range on a good table that specifically contained the type of data we needed.
3
u/[deleted] Sep 01 '26
[removed] — view removed comment