use of org.apache.gobblin.source.jdbc.JdbcCommand.JdbcCommandType in project incubator-gobblin by apache.
the class JdbcExtractor method executeSql.
/**
* Execute query using JDBC simple Statement Set fetch size
*
* @param cmds commands - query, fetch size
* @return JDBC ResultSet
* @throws Exception
*/
private CommandOutput<?, ?> executeSql(List<Command> cmds) {
String query = null;
int fetchSize = 0;
for (Command cmd : cmds) {
if (cmd instanceof JdbcCommand) {
JdbcCommandType type = (JdbcCommandType) cmd.getCommandType();
switch(type) {
case QUERY:
query = cmd.getParams().get(0);
break;
case FETCHSIZE:
fetchSize = Integer.parseInt(cmd.getParams().get(0));
break;
default:
this.log.error("Command " + type.toString() + " not recognized");
break;
}
}
}
this.log.info("Executing query:" + query);
ResultSet resultSet = null;
try {
this.jdbcSource = createJdbcSource();
this.dataConnection = this.jdbcSource.getConnection();
Statement statement = this.dataConnection.createStatement();
if (fetchSize != 0 && this.getExpectedRecordCount() > 2000) {
statement.setFetchSize(fetchSize);
}
final boolean status = statement.execute(query);
if (status == false) {
this.log.error("Failed to execute sql:" + query);
}
resultSet = statement.getResultSet();
} catch (Exception e) {
this.log.error("Failed to execute sql:" + query + " ;error-" + e.getMessage(), e);
}
CommandOutput<JdbcCommand, ResultSet> output = new JdbcCommandOutput();
output.put((JdbcCommand) cmds.get(0), resultSet);
return output;
}
use of org.apache.gobblin.source.jdbc.JdbcCommand.JdbcCommandType in project incubator-gobblin by apache.
the class JdbcExtractor method executePreparedSql.
/**
* Execute query using JDBC PreparedStatement to pass query parameters Set
* fetch size
*
* @param cmds commands - query, fetch size, query parameters
* @return JDBC ResultSet
* @throws Exception
*/
private CommandOutput<?, ?> executePreparedSql(List<Command> cmds) {
String query = null;
List<String> queryParameters = null;
int fetchSize = 0;
for (Command cmd : cmds) {
if (cmd instanceof JdbcCommand) {
JdbcCommandType type = (JdbcCommandType) cmd.getCommandType();
switch(type) {
case QUERY:
query = cmd.getParams().get(0);
break;
case QUERYPARAMS:
queryParameters = cmd.getParams();
break;
case FETCHSIZE:
fetchSize = Integer.parseInt(cmd.getParams().get(0));
break;
default:
this.log.error("Command " + type.toString() + " not recognized");
break;
}
}
}
this.log.info("Executing query:" + query);
ResultSet resultSet = null;
try {
this.jdbcSource = createJdbcSource();
this.dataConnection = this.jdbcSource.getConnection();
PreparedStatement statement = this.dataConnection.prepareStatement(query, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
int parameterPosition = 1;
if (queryParameters != null && queryParameters.size() > 0) {
for (String parameter : queryParameters) {
statement.setString(parameterPosition, parameter);
parameterPosition++;
}
}
if (fetchSize != 0) {
statement.setFetchSize(fetchSize);
}
final boolean status = statement.execute();
if (status == false) {
this.log.error("Failed to execute sql:" + query);
}
resultSet = statement.getResultSet();
} catch (Exception e) {
this.log.error("Failed to execute sql:" + query + " ;error-" + e.getMessage(), e);
}
CommandOutput<JdbcCommand, ResultSet> output = new JdbcCommandOutput();
output.put((JdbcCommand) cmds.get(0), resultSet);
return output;
}
Aggregations