Package org.apache.nifi.sql
Class CalciteDatabase
java.lang.Object
org.apache.nifi.sql.CalciteDatabase
- All Implemented Interfaces:
Closeable,AutoCloseable
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 Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new database using the default connection propertiesCalciteDatabase(Properties calciteConnectionProperties) Creates a new database with the given calcite connection properties -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds the given table to the databasevoidaddUserDefinedFunction(String functionName, Class<?> functionClass, String methodName) Adds a user-defined function (UDF) to the database with the given name.voidaddUserDefinedFunction(String functionName, Method method) Adds a user-defined function (UDF) to the database with the given name.voidclose()private org.apache.calcite.jdbc.CalciteConnectioncreateConnection(Properties properties) The database connection that can be used for creating Statements, etc.Retrieves the table with the given nameremoveTable(String tableName) Removes the table with the given name from the database
-
Field Details
-
logger
private static final org.slf4j.Logger logger -
connection
private final org.apache.calcite.jdbc.CalciteConnection connection -
tables
-
-
Constructor Details
-
CalciteDatabase
Creates a new database using the default connection properties- Throws:
SQLException- if unable to create the database/connection
-
CalciteDatabase
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
Adds the given table to the database- Parameters:
table- the table to add
-
getTable
Retrieves the table with the given name- Parameters:
tableName- the name of the table- Returns:
- the NiFiTable with the given name, or
nullif no table exists with the given name
-
getTables
- Returns:
- all NiFiTables in the database
-
removeTable
Removes the table with the given name from the database- Parameters:
tableName- the name of the table- Returns:
- the NiFiTable that was removed, or
nullif no table was removed
-
addUserDefinedFunction
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 typeFunctionContext.- Parameters:
functionName- the name of the functionfunctionClass- the class that implements the functionmethodName- the name of the method that implements the function
-
addUserDefinedFunction
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 typeFunctionContext.- Parameters:
functionName- the name of the functionmethod- the method to invoke in order to call the function
-
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
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-