Search in sources :

Example 1 with FlowFileTable

use of org.apache.nifi.queryrecord.FlowFileTable in project nifi by apache.

the class QueryRecord method query.

protected QueryResult query(final ProcessSession session, final FlowFile flowFile, final String sql, final ProcessContext context, final RecordReaderFactory recordParserFactory) throws SQLException {
    final Properties properties = new Properties();
    properties.put(CalciteConnectionProperty.LEX.camelName(), Lex.MYSQL_ANSI.name());
    Connection connection = null;
    ResultSet resultSet = null;
    Statement statement = null;
    try {
        connection = DriverManager.getConnection("jdbc:calcite:", properties);
        final CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
        final SchemaPlus rootSchema = calciteConnection.getRootSchema();
        final FlowFileTable<?, ?> flowFileTable = new FlowFileTable<>(session, flowFile, recordParserFactory, getLogger());
        rootSchema.add("FLOWFILE", flowFileTable);
        rootSchema.setCacheEnabled(false);
        statement = connection.createStatement();
        resultSet = statement.executeQuery(sql);
        final ResultSet rs = resultSet;
        final Statement stmt = statement;
        final Connection conn = connection;
        return new QueryResult() {

            @Override
            public void close() throws IOException {
                closeQuietly(rs, stmt, conn);
            }

            @Override
            public ResultSet getResultSet() {
                return rs;
            }

            @Override
            public int getRecordsRead() {
                return flowFileTable.getRecordsRead();
            }
        };
    } catch (final Exception e) {
        closeQuietly(resultSet, statement, connection);
        throw e;
    }
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) ResultSet(java.sql.ResultSet) SchemaPlus(org.apache.calcite.schema.SchemaPlus) FlowFileTable(org.apache.nifi.queryrecord.FlowFileTable) Properties(java.util.Properties) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) SchemaNotFoundException(org.apache.nifi.schema.access.SchemaNotFoundException) ProcessException(org.apache.nifi.processor.exception.ProcessException) SQLException(java.sql.SQLException) IOException(java.io.IOException)

Example 2 with FlowFileTable

use of org.apache.nifi.queryrecord.FlowFileTable in project nifi by apache.

the class QueryRecord method buildCachedStatement.

private CachedStatement buildCachedStatement(final String sql, final Supplier<CalciteConnection> connectionSupplier, final ProcessSession session, final FlowFile flowFile, final RecordReaderFactory recordReaderFactory) throws SQLException {
    final CalciteConnection connection = connectionSupplier.get();
    final SchemaPlus rootSchema = connection.getRootSchema();
    final FlowFileTable<?, ?> flowFileTable = new FlowFileTable<>(session, flowFile, recordReaderFactory, getLogger());
    rootSchema.add("FLOWFILE", flowFileTable);
    rootSchema.setCacheEnabled(false);
    final PreparedStatement stmt = connection.prepareStatement(sql);
    return new CachedStatement(stmt, flowFileTable, connection);
}
Also used : SchemaPlus(org.apache.calcite.schema.SchemaPlus) FlowFileTable(org.apache.nifi.queryrecord.FlowFileTable) PreparedStatement(java.sql.PreparedStatement) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection)

Aggregations

PreparedStatement (java.sql.PreparedStatement)2 CalciteConnection (org.apache.calcite.jdbc.CalciteConnection)2 SchemaPlus (org.apache.calcite.schema.SchemaPlus)2 FlowFileTable (org.apache.nifi.queryrecord.FlowFileTable)2 IOException (java.io.IOException)1 Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 Properties (java.util.Properties)1 ProcessException (org.apache.nifi.processor.exception.ProcessException)1 SchemaNotFoundException (org.apache.nifi.schema.access.SchemaNotFoundException)1