Class CalciteDatabase

java.lang.Object
org.apache.nifi.sql.CalciteDatabase
All Implemented Interfaces:
Closeable, AutoCloseable

public class CalciteDatabase extends Object implements Closeable

A simple abstraction for creating a database facade over data.

While this abstraction does not provide all of the power that apache Calcite provides, it does provide the capabilities that are necessary for the uses cases where NiFi has made use of Calcite. This abstraction, however, greatly simplifies the API so that we can easily expose a SQL interface to data.

The typical usage pattern for this class is as follows:

 
     try (final CalciteDatabase database = new CalciteDatabase()) {
         final ResettableDataSource dataSource = getDataSource();
         final NiFiTable firstTable = new NiFiTable("MY_TABLE", dataSource, getLogger());
         database.addTable(firstTable);

         final ResettableDataSource secondDataSource = getDataSource();
         final NiFiTable secondTable = new NiFiTable("YOUR_TABLE", secondDataSource, getLogger());
         database.addTable(secondTable);

         try (final PreparedStatement stmt = database.getConnection().prepareStatement("SELECT * FROM MY_TABLE")) {
             ...
         }
     }
 
 
  • Field Details

    • logger

      private static final org.slf4j.Logger logger
    • connection

      private final org.apache.calcite.jdbc.CalciteConnection connection
    • tables

      private final Map<String,NiFiTable> tables
  • Constructor Details

    • CalciteDatabase

      public CalciteDatabase() throws SQLException
      Creates a new database using the default connection properties
      Throws:
      SQLException - if unable to create the database/connection
    • CalciteDatabase

      public CalciteDatabase(Properties calciteConnectionProperties) throws SQLException
      Creates a new database with the given calcite connection properties
      Parameters:
      calciteConnectionProperties - the connection properties to provide to calcite
      Throws:
      SQLException - if unable to create the database/connection
  • Method Details

    • addTable

      public void addTable(NiFiTable table)
      Adds the given table to the database
      Parameters:
      table - the table to add
    • getTable

      public NiFiTable getTable(String tableName)
      Retrieves the table with the given name
      Parameters:
      tableName - the name of the table
      Returns:
      the NiFiTable with the given name, or null if no table exists with the given name
    • getTables

      public Collection<NiFiTable> getTables()
      Returns:
      all NiFiTables in the database
    • removeTable

      public NiFiTable removeTable(String tableName)
      Removes the table with the given name from the database
      Parameters:
      tableName - the name of the table
      Returns:
      the NiFiTable that was removed, or null if no table was removed
    • addUserDefinedFunction

      public void addUserDefinedFunction(String functionName, Class<?> functionClass, String methodName)
      Adds a user-defined function (UDF) to the database with the given name. Note that Calcite requires that the Class have a default, zero-arg constructor, or a constructor that takes exactly one argument of type FunctionContext.
      Parameters:
      functionName - the name of the function
      functionClass - the class that implements the function
      methodName - the name of the method that implements the function
    • addUserDefinedFunction

      public void addUserDefinedFunction(String functionName, Method method)
      Adds a user-defined function (UDF) to the database with the given name. Note that Calcite requires that the Class that the given method belongs to have a default, zero-arg constructor, or a constructor that takes exactly one argument of type FunctionContext.
      Parameters:
      functionName - the name of the function
      method - the method to invoke in order to call the function
    • getConnection

      public Connection getConnection()
      The database connection that can be used for creating Statements, etc.
      Returns:
      the database connection
    • createConnection

      private org.apache.calcite.jdbc.CalciteConnection createConnection(Properties properties) throws SQLException
      Throws:
      SQLException
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException