-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathJBasic.java
93 lines (73 loc) · 2.95 KB
/
JBasic.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
package examples;
import edb.client.DBClient;
import edb.common.ExistingTableException;
import edb.common.Schema;
import edb.common.UnknownTableException;
import edb.server.DBServer;
import org.apache.spark.sql.*;
import org.apache.spark.sql.Row;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class JBasic {
public static void main(String[] args)
throws IOException, InterruptedException,
ExistingTableException, UnknownTableException
{
final String serverHost = "localhost";
final int serverPort = 50199;
DBServer server = new DBServer(serverPort);
server.start();
System.out.println("*** Example database server started");
//
// Since this DataSource doesn't support writing, we need to populate
// ExampleDB with some data. Also, since the DataSource only uses a
// fixed schema and reads from a single, fixed table, we need to make
// sure that the data we create conforms to those.
//
Schema schema = new Schema();
schema.addColumn("i", Schema.ColumnType.INT64);
schema.addColumn("j", Schema.ColumnType.INT64);
DBClient client = new DBClient(serverHost, serverPort);
client.createTable("theTable", schema);
List<edb.common.Row> toInsert = new ArrayList<>();
edb.common.Row r1 = new edb.common.Row();
r1.addField(new edb.common.Row.Int64Field("i", 100));
r1.addField(new edb.common.Row.Int64Field("j", 200));
toInsert.add(r1);
edb.common.Row r2 = new edb.common.Row();
r2.addField(new edb.common.Row.Int64Field("i", 300));
r2.addField(new edb.common.Row.Int64Field("j", 400));
toInsert.add(r2);
client.bulkInsert("theTable", toInsert);
System.out.println("*** Example database server populated with data");
String dataSourceName = "datasources.SimpleRowDataSource";
SparkSession spark = SparkSession
.builder()
.appName("JBasic")
.master("local[4]")
.getOrCreate();
//
// This is where we read from our DataSource. Notice how we use the
// fully qualified class name and provide the information needed to connect to
// ExampleDB using options.
//
Dataset<Row> data = spark.read()
.format(dataSourceName)
.option("host", serverHost)
.option("port", serverPort)
.load();
System.out.println("*** Schema: ");
data.printSchema();
System.out.println("*** Data: ");
data.show();
//
// Since this DataSource only supports reading from one executor,
// there will only be a single partition.
//
System.out.println("*** Number of partitions: " +
data.rdd().partitions().length);
spark.stop();
server.stop();
}
}