6
6
- databricks
7
7
- spark
8
8
- deltalake
9
+ - featured
9
10
team : Data Engineering
10
11
---
11
12
12
- OLTP databases are a common data source for Data Lake based warehouses which use Big Data tools to run
13
- batch analytics pipelines. Classic hadoop toolset comes with
14
- [ Apache Sqoop] ( https://sqoop.apache.org/ ) - a tool for bulk import/export
15
- of data between HDFS and relational data stores. Our pipelines were using this tool as well, primarily
16
- to import MySQL data into HDFS. When Platform Engineering team at Scribd took on a effort
17
- to migrate our on-premise Hadoop workloads to [ Databricks Lakehouse Platform] ( https://databricks.com/product/data-lakehouse )
18
- on AWS we had to write our own tool to import data from MySQL directly into S3 backed [ Delta Lake] ( https://delta.io/ ) .
19
- In this post I will share the details about ` sql-delta-import ` - an open-source spark utility to import data from any
20
- JDBC compatible database into Delta Lake. This utility is being open sourced under
21
- [ Delta Lake Connectors] ( https://github.com/delta-io/connectors/pull/80 ) project
13
+ OLTP databases are a common data source for Data Lake based warehouses which use Big Data tools to run
14
+ batch analytics pipelines. The classic Apache Hadoop toolchain includes
15
+ [ Apache Sqoop] ( https://sqoop.apache.org/ ) - a tool for bulk import/export
16
+ of data between HDFS and relational data stores. Our pipelines were using this tool as well, primarily
17
+ to import MySQL data into HDFS. When the Platform Engineering team took on the migration of
18
+ our on-premise Hadoop workloads to the [ Databricks Lakehouse Platform] ( https://databricks.com/product/data-lakehouse )
19
+ on AWS, we had to write our own tool to import data from MySQL directly into S3-backed [ Delta Lake] ( https://delta.io/ ) .
20
+ In this post I will share the details about ` sql-delta-import ` - an open source utility we have proposed for inclusion in the
21
+ [ Delta Lake
22
+ Connectors] ( https://github.com/delta-io/connectors/pull/80 ) project, we're
23
+ looking forward to working with others to improve and accelerate importing data
24
+ into Delta Lake!
22
25
23
26
### Sample import
24
27
25
- Importing data into a Delta Lake table is as easy as
28
+ Importing data into a Delta Lake table is as easy as
26
29
27
- ``` shell script
30
+ ``` sh
28
31
spark-submit /
29
32
--class " io.delta.connectors.spark.JDBC.ImportRunner" sql-delta-import_2.12-0.2.1-SNAPSHOT.jar /
30
33
--jdbc-url jdbc:mysql://hostName:port/database /
@@ -35,17 +38,17 @@ spark-submit /
35
38
36
39
### This looks a lot like ` sqoop ` ... why didn't you just use that?
37
40
38
- We considered using ` sqoop ` at first but quickly dismissed that option for multiple reasons
41
+ We considered using ` sqoop ` at first but quickly dismissed that option for multiple reasons:
39
42
40
43
#### 1. Databricks Lakehouse Platform does not come with ` sqoop `
41
44
Yes we could have ran our sqoop jobs on EMR clusters but we wanted to run everything in Databricks and
42
- avoid additional technology footprint. But even if we drop that restriction...
43
-
45
+ avoid additional technology footprint and overhead . But even if we drop that restriction...
46
+
44
47
#### 2. ` sqoop ` does not support writing data directly to Delta Lake
45
- ` sqoop ` can only import data as text or parquet. Writing to delta directly allows us to
48
+ ` sqoop ` can only import data as text or parquet. Writing to delta directly allows us to
46
49
optimize data storage for best performance on reads by just adding a couple of configuration options
47
50
48
- ``` shell script
51
+ ``` sh
49
52
spark-submit /
50
53
--conf spark.databricks.delta.optimizeWrite.enabled=true /
51
54
--conf spark.databricks.delta.autoCompact.enabled=true /
@@ -57,18 +60,18 @@ spark-submit /
57
60
```
58
61
59
62
#### 3. ` --num-mappers ` just not good enough to control parallelism when working with a database
60
- ` sqoop ` uses map-reduce under the hood. We can specify ` --num-mappers ` parameter that controls how many
61
- mappers will be used to import data. Small number of mappers can result in large volume
62
- of data per import and long running transactions. Large number of mappers will result in many connections
63
+ ` sqoop ` uses map-reduce under the hood. We can specify ` --num-mappers ` parameter that controls how many
64
+ mappers will be used to import data. Small number of mappers can result in large volume
65
+ of data per import and long running transactions. Large number of mappers will result in many connections
63
66
to database potentially overloading it especially when there are a lot of ` sqoop ` jobs running in parallel.
64
- Additionally since there are no reduce stages in ` sqoop ` jobs large number of mappers will result in large
67
+ Additionally since there are no reduce stages in ` sqoop ` jobs large number of mappers will result in large
65
68
number of output files and potentially introducing a small files problem.
66
69
67
- ` sql delta import ` uses ` --chunks ` parameter to control number of... well... chunks to split the source table
70
+ ` sql delta import ` uses ` --chunks ` parameter to control number of... well... chunks to split the source table
68
71
into and standard spark parameters like ` --num-executors ` and ` --executor-cores ` to control data import
69
72
concurrency thus allowing you to tune those parameters independently
70
73
71
- ``` shell script
74
+ ``` sh
72
75
spark-submit --num-executors 15 --executor-cores 4 /
73
76
--conf spark.databricks.delta.optimizeWrite.enabled=true /
74
77
--conf spark.databricks.delta.autoCompact.enabled=true /
@@ -81,39 +84,39 @@ spark-submit --num-executors 15 --executor-cores 4 /
81
84
```
82
85
83
86
in the example above source table will be split into 500 chunks resulting in quick transactions and released connections
84
- but no more than 60 concurrent connections will be used for import since max degree of parallelism is 60 (15 executors x 4 cores).
87
+ but no more than 60 concurrent connections will be used for import since max degree of parallelism is 60 (15 executors x 4 cores).
85
88
` delta.optimizeWrite ` and ` delta.autoCompact ` configuration will yield optimal file size output for the destination table
86
89
87
90
#### 3.1 ` --num-mappers ` and data skew just don't play nicely together
88
-
89
- When ` sqoop ` imports data, source table will be split into ranges based on ` --split-by ` column and each mapper
90
- would import its corresponding range. This works good when ` --split-by ` column has a near uniform distribution
91
+
92
+ When ` sqoop ` imports data, source table will be split into ranges based on ` --split-by ` column and each mapper
93
+ would import its corresponding range. This works well when ` --split-by ` column has a near uniform distribution
91
94
of data, but that's not always the case with source tables... As tables age we tend to add additional columns to them to
92
- take on new business requirements so over time data in latest rows has a higher fill rate than earlier rows.
95
+ take on new business requirements so over time data in latest rows has a higher fill rate than earlier rows.
93
96
94
97
![ row density increase over time] ( /post-images/2021-03-sql-delta-import/row_density_increase.png )
95
98
96
- Our source tables here at Scribd definitely have these characteristics. We also have some tables that have entire
99
+ Our source tables here at Scribd definitely have these characteristics. We also have some tables that have entire
97
100
ranges of data missing due to data cleanup. At some point large chunks of data were just deleted from these tables.
98
101
99
102
![ missing rows] ( /post-images/2021-03-sql-delta-import/missing_rows.png )
100
103
101
- This type of data skew will result in processing time skew and output file size skew when you can only control number of
102
- mappers. Yes we can introduce additional computed synthetic column in the source table as our ` split-by ` column but now
103
- there is an additional column that does not add business value, app developers need to be aware of it, computing and
104
- storing it takes up database resources and if we plan to use it for imports it's better be indexed, thus even more
104
+ This type of data skew will result in processing time skew and output file size skew when you can only control number of
105
+ mappers. Yes we can introduce additional computed synthetic column in the source table as our ` split-by ` column but now
106
+ there is an additional column that does not add business value, app developers need to be aware of it, computing and
107
+ storing it takes up database resources and if we plan to use it for imports it's better be indexed, thus even more
105
108
compute and storage resources.
106
109
107
- With ` sql-delta-import ` we still split source tables into ranges based on ` --split-by ` column but if there is data
110
+ With ` sql-delta-import ` we still split source tables into ranges based on ` --split-by ` column but if there is data
108
111
distribution skew we can "solve" this problem by making number of chunks much larger than max degree of parallelism.
109
- This way large chunks with high data density are broken up into smaller pieces that a single executor can handle.
110
- Executors that get chunks with little or no data can just quickly process them and move on to do some real work.
112
+ This way large chunks with high data density are broken up into smaller pieces that a single executor can handle.
113
+ Executors that get chunks with little or no data can just quickly process them and move on to do some real work.
111
114
112
115
113
116
### Advanced use cases
114
117
115
- For advanced use cases you don't have to use provided spark application directly. ` sql-delta-import `
116
- libraries can be imported into your own project. You can specify custom data transformations or JDBC dialect to gain a
118
+ For advanced use cases you don't have to use provided spark application directly. ` sql-delta-import `
119
+ libraries can be imported into your own project. You can specify custom data transformations or JDBC dialect to gain a
117
120
more precised control of data type handling
118
121
119
122
``` scala
@@ -122,7 +125,7 @@ import org.apache.spark.sql.functions._
122
125
import org .apache .spark .sql .types ._
123
126
124
127
import io .delta .connectors .spark .JDBC ._
125
-
128
+
126
129
implicit val spark : SparkSession = SparkSession .builder().master(" local" ).getOrCreate()
127
130
128
131
@@ -149,7 +152,10 @@ val importer = new JDBCImport(jdbcUrl = jdbcUrl, importConfig = config, dataTran
149
152
importer.run()
150
153
```
151
154
152
- ---
153
- Prior to migrating to Databricks Lakehouse Platform we had roughly 300 ` sqoop ` jobs. We were able to
154
- successfully port all of them to ` sql-delta-import ` . Today they happily coexist in production with other spark
155
+ Prior to migrating to Databricks Lakehouse Platform we had roughly 300 ` sqoop ` jobs. We were able to
156
+ successfully port all of them to ` sql-delta-import ` . Today they happily coexist in production with other spark
155
157
jobs allowing us to use uniform set of tools for orchestrating, scheduling, monitoring and logging for all of our jobs.
158
+
159
+ If you're interested in working with Delta Lake, the Databricks platform, or
160
+ enabling really interesting machine learning use-cases, check out our [ careers
161
+ page] ( /careers/#open-positions ) !
0 commit comments