Search in sources :

Example 1 with JdbcCommandType

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;
}
Also used : JdbcCommandType(org.apache.gobblin.source.jdbc.JdbcCommand.JdbcCommandType) Command(org.apache.gobblin.source.extractor.extract.Command) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) ParseException(java.text.ParseException) RecordCountException(org.apache.gobblin.source.extractor.exception.RecordCountException) DataRecordException(org.apache.gobblin.source.extractor.DataRecordException) SqlParseException(org.apache.calcite.sql.parser.SqlParseException) SchemaException(org.apache.gobblin.source.extractor.exception.SchemaException) SQLException(java.sql.SQLException) IOException(java.io.IOException) HighWatermarkException(org.apache.gobblin.source.extractor.exception.HighWatermarkException)

Example 2 with JdbcCommandType

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;
}
Also used : JdbcCommandType(org.apache.gobblin.source.jdbc.JdbcCommand.JdbcCommandType) Command(org.apache.gobblin.source.extractor.extract.Command) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) ParseException(java.text.ParseException) RecordCountException(org.apache.gobblin.source.extractor.exception.RecordCountException) DataRecordException(org.apache.gobblin.source.extractor.DataRecordException) SqlParseException(org.apache.calcite.sql.parser.SqlParseException) SchemaException(org.apache.gobblin.source.extractor.exception.SchemaException) SQLException(java.sql.SQLException) IOException(java.io.IOException) HighWatermarkException(org.apache.gobblin.source.extractor.exception.HighWatermarkException)

Aggregations

IOException (java.io.IOException)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 ParseException (java.text.ParseException)2 SqlParseException (org.apache.calcite.sql.parser.SqlParseException)2 DataRecordException (org.apache.gobblin.source.extractor.DataRecordException)2 HighWatermarkException (org.apache.gobblin.source.extractor.exception.HighWatermarkException)2 RecordCountException (org.apache.gobblin.source.extractor.exception.RecordCountException)2 SchemaException (org.apache.gobblin.source.extractor.exception.SchemaException)2 Command (org.apache.gobblin.source.extractor.extract.Command)2 JdbcCommandType (org.apache.gobblin.source.jdbc.JdbcCommand.JdbcCommandType)2 Statement (java.sql.Statement)1