-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathJReadNamedTable.java
More file actions
95 lines (75 loc) · 3.09 KB
/
JReadNamedTable.java
File metadata and controls
95 lines (75 loc) · 3.09 KB
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
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.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class JReadNamedTable {
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. We'll use the same schema as before, but
// this time it's not baked into the data source -- the latter will infer it.
//
Schema schema = new Schema();
schema.addColumn("u", Schema.ColumnType.INT64);
schema.addColumn("v", Schema.ColumnType.DOUBLE);
DBClient client = new DBClient(serverHost, serverPort);
client.createTable("myTable", schema);
List<edb.common.Row> toInsert = new ArrayList<>();
edb.common.Row r1 = new edb.common.Row();
r1.addField(new edb.common.Row.Int64Field("u", 100));
r1.addField(new edb.common.Row.DoubleField("v", 200.2));
toInsert.add(r1);
edb.common.Row r2 = new edb.common.Row();
r2.addField(new edb.common.Row.Int64Field("u", 300));
r2.addField(new edb.common.Row.DoubleField("v", 400.4));
toInsert.add(r2);
client.bulkInsert("myTable", toInsert);
System.out.println("*** Example database server populated with data");
String dataSourceName = "datasources.FlexibleRowDataSource";
SparkSession spark = SparkSession
.builder()
.appName("JReadNamedTable")
.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. Also, notice we specify the name of the table
// as an option.
//
Dataset<Row> data = spark.read()
.format(dataSourceName)
.option("host", serverHost)
.option("port", serverPort)
.option("table", "myTable")
.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();
}
}