-
Notifications
You must be signed in to change notification settings - Fork 206
/
ReadJdbcP.java
144 lines (130 loc) · 5.03 KB
/
ReadJdbcP.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
/*
* Copyright (c) 2008-2018, 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.
*/
package com.hazelcast.jet.impl.connector;
import com.hazelcast.jet.Traverser;
import com.hazelcast.jet.core.AbstractProcessor;
import com.hazelcast.jet.core.ProcessorMetaSupplier;
import com.hazelcast.jet.core.ProcessorSupplier;
import com.hazelcast.jet.core.processor.SourceProcessors;
import com.hazelcast.jet.function.DistributedFunction;
import com.hazelcast.jet.function.DistributedSupplier;
import com.hazelcast.jet.pipeline.ResultSetForPartitionFunction;
import javax.annotation.Nonnull;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import static com.hazelcast.jet.impl.util.Util.uncheckCall;
/**
* Use {@link SourceProcessors#readJdbcP}.
*/
public final class ReadJdbcP<T> extends AbstractProcessor {
private final DistributedSupplier<Connection> connectionSupplier;
private final ResultSetForPartitionFunction resultSetFn;
private final DistributedFunction<ResultSet, T> mapOutputFn;
private Connection connection;
private ResultSet resultSet;
private Traverser traverser;
private int parallelism;
private int index;
private ReadJdbcP(
@Nonnull DistributedSupplier<Connection> connectionSupplier,
@Nonnull ResultSetForPartitionFunction resultSetFn,
@Nonnull DistributedFunction<ResultSet, T> mapOutputFn
) {
this.connectionSupplier = connectionSupplier;
this.resultSetFn = resultSetFn;
this.mapOutputFn = mapOutputFn;
setCooperative(false);
}
/**
* Use {@link SourceProcessors#readJdbcP}.
*/
public static <T> ProcessorMetaSupplier supplier(
@Nonnull DistributedSupplier<Connection> connectionSupplier,
@Nonnull ResultSetForPartitionFunction resultSetFn,
@Nonnull DistributedFunction<ResultSet, T> mapOutputFn
) {
return ProcessorMetaSupplier.preferLocalParallelismOne(() ->
new ReadJdbcP<>(connectionSupplier, resultSetFn, mapOutputFn));
}
public static <T> ProcessorMetaSupplier supplier(
@Nonnull String connectionURL,
@Nonnull String query,
@Nonnull DistributedFunction<ResultSet, T> mapOutputFn
) {
return ProcessorMetaSupplier.forceTotalParallelismOne(ProcessorSupplier.of(() ->
new ReadJdbcP<>(
() -> uncheckCall(() -> DriverManager.getConnection(connectionURL)),
(connection, parallelism, index) -> {
PreparedStatement statement = connection.prepareStatement(query);
try {
return statement.executeQuery();
} catch (SQLException e) {
statement.close();
throw e;
}
},
mapOutputFn)
));
}
@Override
protected void init(@Nonnull Context context) {
this.connection = connectionSupplier.get();
this.parallelism = context.totalParallelism();
this.index = context.globalProcessorIndex();
}
@Override
public boolean complete() {
if (traverser == null) {
resultSet = uncheckCall(() -> resultSetFn.createResultSet(connection, parallelism, index));
traverser = ((Traverser<ResultSet>) () -> uncheckCall(() -> resultSet.next() ? resultSet : null))
.map(mapOutputFn);
}
return emitFromTraverser(traverser);
}
@Override
public void close() throws Exception {
Exception resultSetException = null;
Exception statementException = null;
if (resultSet != null) {
Statement statement = resultSet.getStatement();
resultSetException = close(resultSet);
if (statement != null) {
statementException = close(statement);
}
}
if (connection != null) {
connection.close();
}
if (resultSetException != null) {
throw resultSetException;
}
if (statementException != null) {
throw statementException;
}
}
private static Exception close(AutoCloseable closeable) {
try {
closeable.close();
} catch (Exception e) {
return e;
}
return null;
}
}