-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathSReadWriteParallel.scala
200 lines (179 loc) · 5.52 KB
/
SReadWriteParallel.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package examples
import edb.server.DBServer
import examples.utils.RDDUtils
import org.apache.spark.sql.types._
import org.apache.spark.sql.{Row, SaveMode, SparkSession}
/**
* This illustrates updates using the simplest update-capable data source example,
* the ParallelRowReadWriteDataSource.
*
* First a dataframe is created that is used to populate a table for the first time. At that
* point the newly created table's database schema is calculated from the dataframe schema.
* Notice that even though we create a dataframe with 6 partitions, later when we read
* from the table we always obtain dataframes with 4 partitions. This is because all tables
* in ExampleDB advertise 4 partitions by default, and we would have to override that default
* when reading to obtain different partitioning. However, the partitioning of the dataframe
* DOES impact update parallelism -- notice from the log output that six tasks write to six temporary tables --
* and these would have run in parallel had we not specified only 4 executors as we do in all these examples.
*
* We then put all four settings of SaveMode through their paces and see their impact.
*/
object SReadWriteParallel {
def main(args: Array[String]) {
val serverHost: String = "localhost"
val serverPort: Int = 50199
val server: DBServer = new DBServer(serverPort)
server.start()
System.out.println("*** Example database server started")
val spark =
SparkSession.builder()
.appName("SReadWriteParallel")
.master("local[4]")
.getOrCreate()
//
// Set up the data source
//
val source = "datasources.ParallelRowReadWriteDataSource"
val tableName = "myTable"
val schema = StructType(
Seq(
StructField("id", LongType, true),
StructField("count", LongType, true),
StructField("group", StringType, true)
)
)
//
// insert some initial contents
//
val initialRowsToWrite = Seq(
Row(100l, 500l, "A"),
Row(200l, 50l, "B"),
Row(300l, 160l, "A"),
Row(400l, 100l, "B")
)
val initialRowsToWriteRDD = spark.sparkContext.parallelize(initialRowsToWrite, 6)
val initialDfToWrite = spark.createDataFrame(initialRowsToWriteRDD, schema)
initialDfToWrite.write
.format(source)
.option("host", serverHost)
.option("port", serverPort)
.option("table", tableName)
.mode(SaveMode.Append)
.save()
println("*** Initial contents have been written to data source")
val df = spark.read
.format(source)
.option("host", serverHost)
.option("port", serverPort)
.option("table", tableName)
.load()
println("*** Initial contents of data source")
df.printSchema()
df.show()
RDDUtils.analyze(df)
//
// Set up another data frame to write to the above data source in
// various values of SaveMode
//
val rowsToWrite = Seq(
Row(1000l, 500l, "A"),
Row(2000l, 150l, "C"),
Row(3000l, 160l, "A"),
Row(4000l, 1000l, "A")
)
val rowsToWriteRDD = spark.sparkContext.parallelize(rowsToWrite, 4)
val dfToWrite = spark.createDataFrame(rowsToWriteRDD, df.schema)
//
// SaveMode.ErrorIfExists
//
try {
dfToWrite.write
.format(source)
.option("host", serverHost)
.option("port", serverPort)
.option("table", tableName)
.mode(SaveMode.ErrorIfExists)
.save()
println("*** Write should have failed, but didn't!")
} catch {
case re: RuntimeException => {
println(s"*** Threw RuntimeException as expected: ${re.getMessage}")
}
case e: Exception => {
println(s"*** Threw unexpected exception: ${e.getMessage}")
}
}
val df1 = spark.read
.format(source)
.option("host", serverHost)
.option("port", serverPort)
.option("table", tableName)
.load()
println("*** SaveMode.ErrorIfExists: exception and no change")
df1.printSchema()
df1.show()
RDDUtils.analyze(df1)
//
// SaveMode.Append
//
dfToWrite.write
.format(source)
.option("host", serverHost)
.option("port", serverPort)
.option("table", tableName)
.mode(SaveMode.Append)
.save()
val df2 = spark.read
.format(source)
.option("host", serverHost)
.option("port", serverPort)
.option("table", tableName)
.load()
println("*** SaveMode.Append: rows are added")
df2.printSchema()
df2.show()
RDDUtils.analyze(df2)
//
// SaveMode.Overwrite
//
dfToWrite.write
.format(source)
.option("host", serverHost)
.option("port", serverPort)
.option("table", tableName)
.mode(SaveMode.Overwrite)
.save()
val df3 = spark.read
.format(source)
.option("host", serverHost)
.option("port", serverPort)
.option("table", tableName)
.load()
println("*** SaveMode.Overwrite: old rows are replaced")
df3.printSchema()
df3.show()
RDDUtils.analyze(df3)
//
// SaveMode.Ignore
//
dfToWrite.write
.format(source)
.option("host", serverHost)
.option("port", serverPort)
.option("table", tableName)
.mode(SaveMode.Ignore)
.save()
val df4 = spark.read
.format(source)
.option("host", serverHost)
.option("port", serverPort)
.option("table", tableName)
.load()
println("*** SaveMode.Ignore: no change")
df4.printSchema()
df4.show()
RDDUtils.analyze(df4)
spark.stop()
server.stop()
}
}