Search in sources :

Example 1 with ResultSetAdapter

use of com.thinkbiganalytics.util.ResultSetAdapter in project kylo by Teradata.

the class ExecuteHQL method onTrigger.

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    final ComponentLog logger = getLog();
    FlowFile flowFile = null;
    try {
        if (context.hasIncomingConnection()) {
            flowFile = session.get();
            if (flowFile == null) {
                return;
            }
        }
    } catch (NoSuchMethodError e) {
        logger.error("Failed to get incoming", e);
    }
    FlowFile outgoing = (flowFile == null ? session.create() : flowFile);
    final ThriftService thriftService = context.getProperty(THRIFT_SERVICE).asControllerService(ThriftService.class);
    final String selectQuery = context.getProperty(SQL_SELECT_QUERY).evaluateAttributeExpressions(outgoing).getValue();
    final Integer queryTimeout = context.getProperty(QUERY_TIMEOUT).asTimePeriod(TimeUnit.SECONDS).intValue();
    final StopWatch stopWatch = new StopWatch(true);
    try (final Connection con = thriftService.getConnection();
        final Statement st = con.createStatement()) {
        setQueryTimeout(st, queryTimeout);
        final AtomicLong nrOfRows = new AtomicLong(0L);
        outgoing = session.write(outgoing, new OutputStreamCallback() {

            @Override
            public void process(final OutputStream out) throws IOException {
                try {
                    logger.debug("Executing query {}", new Object[] { selectQuery });
                    final ResultSet resultSet = new ResultSetAdapter(st.executeQuery(selectQuery));
                    nrOfRows.set(JdbcCommon.convertToAvroStream(resultSet, out));
                } catch (final SQLException e) {
                    throw new ProcessException(e);
                }
            }
        });
        // set attribute how many rows were selected
        outgoing = session.putAttribute(outgoing, RESULT_ROW_COUNT, Long.toString(nrOfRows.get()));
        logger.info("{} contains {} Avro records", new Object[] { nrOfRows.get() });
        logger.info("Transferred {} to 'success'", new Object[] { outgoing });
        session.getProvenanceReporter().modifyContent(outgoing, "Retrieved " + nrOfRows.get() + " rows", stopWatch.getElapsed(TimeUnit.MILLISECONDS));
        session.transfer(outgoing, REL_SUCCESS);
    } catch (final ProcessException | SQLException e) {
        logger.error("Unable to execute SQL select query {} for {} due to {}; routing to failure", new Object[] { selectQuery, outgoing, e });
        session.transfer(outgoing, REL_FAILURE);
    }
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) SQLException(java.sql.SQLException) Statement(java.sql.Statement) OutputStream(java.io.OutputStream) Connection(java.sql.Connection) ComponentLog(org.apache.nifi.logging.ComponentLog) StopWatch(org.apache.nifi.util.StopWatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) ProcessException(org.apache.nifi.processor.exception.ProcessException) ResultSetAdapter(com.thinkbiganalytics.util.ResultSetAdapter) ResultSet(java.sql.ResultSet) OutputStreamCallback(org.apache.nifi.processor.io.OutputStreamCallback)

Aggregations

ResultSetAdapter (com.thinkbiganalytics.util.ResultSetAdapter)1 OutputStream (java.io.OutputStream)1 Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 FlowFile (org.apache.nifi.flowfile.FlowFile)1 ComponentLog (org.apache.nifi.logging.ComponentLog)1 ProcessException (org.apache.nifi.processor.exception.ProcessException)1 OutputStreamCallback (org.apache.nifi.processor.io.OutputStreamCallback)1 StopWatch (org.apache.nifi.util.StopWatch)1