-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathJReadWriteParallel.java
220 lines (190 loc) · 7.67 KB
/
JReadWriteParallel.java
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package examples;
import edb.common.ExistingTableException;
import edb.common.UnknownTableException;
import edb.server.DBServer;
import examples.utils.RDDUtils;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.sql.*;
import org.apache.spark.sql.types.DataTypes;
import org.apache.spark.sql.types.StructField;
import org.apache.spark.sql.types.StructType;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import scala.collection.Seq;
import static org.apache.spark.sql.types.DataTypes.LongType;
import static org.apache.spark.sql.types.DataTypes.StringType;
/**
* 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.
*/
public class JReadWriteParallel {
public static void main(String[] args)
throws IOException, InterruptedException,
ExistingTableException, UnknownTableException {
String serverHost = "localhost";
int serverPort = 50199;
DBServer server = new DBServer(serverPort);
server.start();
System.out.println("*** Example database server started");
SparkSession spark =
SparkSession.builder()
.appName("ReadWriteParallel")
.master("local[4]")
.getOrCreate();
//
// Set up the data source
//
String source = "datasources.ParallelRowReadWriteDataSource";
String tableName = "myTable";
List<StructField> fields = new ArrayList<>();
fields.add(DataTypes.createStructField("id", LongType, true));
fields.add(DataTypes.createStructField("count", LongType, true));
fields.add(DataTypes.createStructField("group", StringType, true));
StructType schema = DataTypes.createStructType(fields);
//
// insert some initial contents
//
List<Row> initialRowsToWrite = Arrays.asList(
RowFactory.create(100l, 500l, "A"),
RowFactory.create(200l, 50l, "B"),
RowFactory.create(300l, 160l, "A"),
RowFactory.create(400l, 100l, "B")
);
JavaSparkContext sc = new JavaSparkContext(spark.sparkContext());
JavaRDD<Row> initialRowsToWriteRDD = sc.parallelize(initialRowsToWrite, 6);
Dataset<Row> initialDfToWrite = spark.createDataFrame(initialRowsToWriteRDD, schema);
initialDfToWrite.write()
.format(source)
.option("host", serverHost)
.option("port", serverPort)
.option("table", tableName)
.mode(SaveMode.Append)
.save();
System.out.println("*** Initial contents have been written to data source");
Dataset<Row> df = spark.read()
.format(source)
.option("host", serverHost)
.option("port", serverPort)
.option("table", tableName)
.load();
System.out.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
//
List<Row> rowsToWrite = Arrays.asList(
RowFactory.create(1000l, 500l, "A"),
RowFactory.create(2000l, 150l, "C"),
RowFactory.create(3000l, 160l, "A"),
RowFactory.create(4000l, 1000l, "A")
);
JavaRDD<Row> rowsToWriteRDD = sc.parallelize(rowsToWrite, 4);
Dataset<Row> 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();
System.out.println("*** Write should have failed, but didn't!");
} catch (RuntimeException re) {
System.out.println("*** Threw RuntimeException as expected: " + re.getMessage());
} catch (Exception e) {
System.out.println("*** Threw unexpected exception: " + e.getMessage());
}
Dataset<Row> df1 = spark.read()
.format(source)
.option("host", serverHost)
.option("port", serverPort)
.option("table", tableName)
.load();
System.out.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();
Dataset<Row> df2 = spark.read()
.format(source)
.option("host", serverHost)
.option("port", serverPort)
.option("table", tableName)
.load();
System.out.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();
Dataset<Row> df3 = spark.read()
.format(source)
.option("host", serverHost)
.option("port", serverPort)
.option("table", tableName)
.load();
System.out.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();
Dataset df4 = spark.read()
.format(source)
.option("host", serverHost)
.option("port", serverPort)
.option("table", tableName)
.load();
System.out.println("*** SaveMode.Ignore: no change");
df4.printSchema();
df4.show();
RDDUtils.analyze(df4);
spark.stop();
server.stop();
}
}