/
RowFetcher.java
162 lines (145 loc) · 5.87 KB
/
RowFetcher.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
/*
* Copyright 2017 HugeGraph Authors
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* licenses this file to You 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.baidu.hugegraph.loader.reader.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import com.baidu.hugegraph.loader.constant.Constants;
import com.baidu.hugegraph.loader.exception.LoadException;
import com.baidu.hugegraph.loader.reader.Line;
import com.baidu.hugegraph.loader.source.jdbc.JDBCSource;
import com.baidu.hugegraph.util.E;
import com.baidu.hugegraph.util.Log;
public class RowFetcher {
private static final Logger LOG = Log.logger(RowFetcher.class);
private final JDBCSource source;
private final Connection conn;
private String[] columns;
private String[] primaryKeys;
private Line nextStartRow;
private boolean fullyFetched;
public RowFetcher(JDBCSource source) throws SQLException {
this.source = source;
this.conn = this.connect();
this.columns = null;
this.primaryKeys = null;
this.nextStartRow = null;
this.fullyFetched = false;
}
private Connection connect() throws SQLException {
String url = this.source.vendor().buildUrl(this.source);
LOG.info("Connect to database {}", url);
String driverName = this.source.driver();
String username = this.source.username();
String password = this.source.password();
try {
// Register JDBC driver
Class.forName(driverName);
} catch (ClassNotFoundException e) {
throw new LoadException("Invalid driver class '%s'", e, driverName);
}
return DriverManager.getConnection(url, username, password);
}
public void readHeader() throws SQLException {
String sql = this.source.vendor().buildGetHeaderSql(this.source);
LOG.debug("The sql for reading headers is: {}", sql);
try (Statement stmt = this.conn.createStatement();
ResultSet result = stmt.executeQuery(sql)) {
List<String> columns = new ArrayList<>();
while (result.next()) {
columns.add(result.getString("COLUMN_NAME"));
}
this.columns = columns.toArray(new String[]{});
} catch (SQLException e) {
this.close();
throw e;
}
E.checkArgument(ArrayUtils.isNotEmpty(this.columns),
"The colmuns of the table '%s' shouldn't be empty",
this.source.table());
}
public void readPrimaryKey() throws SQLException {
String sql = this.source.vendor().buildGetPrimaryKeySql(this.source);
LOG.debug("The sql for reading primary keys is: {}", sql);
try (Statement stmt = this.conn.createStatement();
ResultSet result = stmt.executeQuery(sql)) {
List<String> columns = new ArrayList<>();
while (result.next()) {
columns.add(result.getString("COLUMN_NAME"));
}
this.primaryKeys = columns.toArray(new String[]{});
} catch (SQLException e) {
this.close();
throw e;
}
E.checkArgument(ArrayUtils.isNotEmpty(this.primaryKeys),
"The primary keys of the table '%s' shouldn't be empty",
this.source.table());
}
public List<Line> nextBatch() throws SQLException {
if (this.fullyFetched) {
return null;
}
String select = this.source.vendor().buildSelectSql(this.source,
this.nextStartRow);
List<Line> batch = new ArrayList<>(this.source.batchSize() + 1);
try (Statement stmt = this.conn.createStatement();
ResultSet result = stmt.executeQuery(select)) {
while (result.next()) {
Object[] values = new Object[this.columns.length];
for (int i = 1, n = this.columns.length; i <= n; i++) {
Object value = result.getObject(i);
if (value == null) {
value = Constants.NULL_STR;
}
values[i - 1] = value;
}
String rawLine = StringUtils.join(values, Constants.COMMA_STR);
Line line = new Line(rawLine, this.columns, values);
batch.add(line);
}
} catch (SQLException e) {
this.close();
throw e;
}
if (batch.size() != this.source.batchSize() + 1) {
this.fullyFetched = true;
} else {
// Remove the last one
Line lastLine = batch.remove(batch.size() - 1);
lastLine.retainAll(this.primaryKeys);
this.nextStartRow = lastLine;
}
return batch;
}
public void close() {
try {
this.conn.close();
} catch (SQLException e) {
LOG.warn("Failed to close connection", e);
}
}
}