Last month we removed the Apache Sqoop library from our main data codebase. For years it was how MySQL tables got into our Hadoop cluster: a scheduled job per table, with Sqoop splitting each table into ranges and copying them in parallel into Hive. This post is about what replaced it. There turned out to be no single replacement, and the most unusual part started a year before the rest.
Why move
Sqoop was retired by the Apache Software Foundation in 2021, so no more releases or fixes were coming. That would have become a blocker for upgrading to Hadoop 3. We had also started hitting problems it wouldn’t fix: its relaxed-isolation option didn’t behave as we expected, and one import running with several mappers caused locking problems on a production database over a weekend.
We replaced it case by case, with three different approaches.
1. When you can’t connect to the database: parse the dump
The first push came in early 2022. Some of the databases we imported from moved to a Kubernetes-based platform where direct connections from outside weren’t possible anymore. What we could get, through an API the platform team built, were backups: binary backups, and SQL dumps produced by mysqldump. Sqoop needs a live connection, so it was out for those tables.
We considered four ways to get from a dump to Hive tables:
- Restore each backup into a temporary MySQL instance and run Sqoop against it as before. Lowest risk in one sense, since the output would be exactly what Sqoop always produced, but a lot of moving parts for every import.
- Run a permanent MySQL instance just for staging imports. Simpler jobs, but another production database to operate, back up and tune, which the people who run our databases were rightly wary of.
- Ask for delimited text exports instead of SQL dumps, which Spark could read directly. Not possible: the dumps were cached and had to correspond one to one with the binary backups, so they couldn’t be per-table or in a different format.
- Parse the SQL dump directly. No MySQL involved at all, at the cost of handling the dump format ourselves.
We went with the fourth. A mysqldump file is mostly CREATE TABLE statements followed by long INSERT INTO ... VALUES (...), (...) statements. The importer reads the dump in Spark, keeps only the INSERT statements for the tables we need, parses the value tuples, and converts each field using the table schemas we already maintained for the Sqoop imports. One of our database people suggested dropping the tables we didn’t need before parsing anything, which makes it faster and cuts down the edge cases. The dumps were also smaller than you might expect: for one database, the compressed SQL dump was 4.3 GB against 11.7 GB for the binary backup.
The first four tables were importing this way about two weeks after we started on it. Parsing a format you don’t control sounds fragile, but mysqldump output is regular, and having the schemas already written down did most of the work.
2. When the import itself is the problem: change data capture
The table whose import caused the weekend locking was a large table of user attributes that changes constantly. Running Sqoop with a single mapper was a stopgap. Instead, we changed the application to emit a small change event every time it writes to that table, with the table, the operation, a timestamp and the changed fields, and added a job that applies those events to the copy in Hive. That’s under 20 million events a day, nothing like a full table scan, and the Hive copy went from being refreshed weekly to daily.
This needs a change in the application, so it’s not a general replacement. For a handful of large, busy tables, it’s much better than copying them again and again.
3. Everything else: Spark over JDBC
For the remaining tables, we built a generic import in Spark that reads over JDBC using the same table definitions. Spark’s JDBC source can split a read into ranges the way Sqoop did, with partitionColumn, lowerBound, upperBound and numPartitions. Most tables moved over without trouble. The interesting problems were at the edges:
- The largest table had more than two billion rows. The first attempts ran out of memory, a problem Sqoop never had because it streams rows straight to HDFS. Getting the range splitting and the executor limits right fixed it. The import then took about 50 minutes with six parallel readers, against three hours forty minutes with Sqoop, though the two didn’t use the same parallelism.
- Not every table has a usable split column. Large tables without a primary key needed a way to name a different column to split on, and one very large table needed a way to override the default lower bound for splitting.
- Parallelism is also load on the source. Each parallel reader is another query against a production database. We kept the same parallelism Sqoop had used and only changed it where a job was too slow.
- Formats leak. Some datetime columns had been stored in Hive as strings. Sqoop and Spark format datetimes differently, one with milliseconds and one without, and downstream jobs that parsed those strings with a hard-coded format broke. The fix we went for was to store those columns as timestamps, which meant updating the jobs that read them.
Looking back
Sqoop did one job well enough that we had stopped thinking about the decisions it made for us: how to split a table, how much load to put on the source, how to format each type. Replacing it meant making each of those decisions explicitly, and choosing a different tool depending on what we could connect to and how often the data changes. The schemas we had kept for every imported table were what made all three approaches possible. Exports from Hadoop back to MySQL are the last piece, and the next project.
Leave a Reply