forked from hazelcast/hazelcast-jet-code-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JdbcSource.java
104 lines (89 loc) · 3.42 KB
/
JdbcSource.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
/*
* Copyright (c) 2008-2019, Hazelcast, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import com.hazelcast.jet.Jet;
import com.hazelcast.jet.JetInstance;
import com.hazelcast.jet.Util;
import com.hazelcast.jet.pipeline.Pipeline;
import com.hazelcast.jet.pipeline.Sinks;
import com.hazelcast.jet.pipeline.Sources;
import org.h2.tools.DeleteDbFiles;
import java.nio.file.Files;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
/**
* Demonstrates dumping values from a table in a relational database to an IMap
* using the JDBC connector.
*/
public class JdbcSource {
private static final String MAP_NAME = "userMap";
private static final String TABLE_NAME = "USER_TABLE";
private JetInstance jet;
private String dbDirectory;
private static Pipeline buildPipeline(String connectionUrl) {
Pipeline p = Pipeline.create();
p.drawFrom(Sources.jdbc(connectionUrl,
"SELECT * FROM " + TABLE_NAME,
resultSet -> new User(resultSet.getInt(1), resultSet.getString(2))))
.map(user -> Util.entry(user.getId(), user))
.drainTo(Sinks.map(MAP_NAME));
return p;
}
public static void main(String[] args) throws Exception {
System.setProperty("hazelcast.logging.type", "log4j");
new JdbcSource().go();
}
private void go() throws Exception {
try {
setup();
Pipeline p = buildPipeline(connectionUrl());
jet.newJob(p).join();
jet.getMap(MAP_NAME).values().forEach(System.out::println);
} finally {
cleanup();
}
}
private void setup() throws Exception {
dbDirectory = Files.createTempDirectory(JdbcSource.class.getName()).toString();
createAndFillTable();
jet = Jet.newJetInstance();
Jet.newJetInstance();
}
private void cleanup() {
Jet.shutdownAll();
DeleteDbFiles.execute(dbDirectory, JdbcSource.class.getSimpleName(), true);
}
private void createAndFillTable() throws SQLException {
try (Connection connection = DriverManager.getConnection(connectionUrl());
Statement statement = connection.createStatement()) {
statement.execute("CREATE TABLE " + TABLE_NAME + "(id int primary key, name varchar(255))");
try (PreparedStatement stmt =
connection.prepareStatement("INSERT INTO " + TABLE_NAME + "(id, name) VALUES(?, ?)")) {
for (int i = 0; i < 100; i++) {
stmt.setInt(1, i);
stmt.setString(2, "name-" + i);
stmt.addBatch();
}
stmt.executeBatch();
}
}
}
private String connectionUrl() {
return "jdbc:h2:" + dbDirectory + "/" + JdbcSource.class.getSimpleName();
}
}