Search in sources :

Example 1 with CalciteConnection

use of org.apache.calcite.jdbc.CalciteConnection in project apex-malhar by apache.

the class SQLExecEnvironment method withModel.

/**
 * Use given model file to initialize {@link SQLExecEnvironment}.
 * The model file contains definitions of endpoints and data formats.
 * Example of file format is like following:
 * <pre>
 *   {
 *     version: '1.0',
 *     defaultSchema: 'APEX',
 *     schemas: [{
 *       name: 'APEX',
 *       tables: [
 *         {
 *            name: 'ORDERS',
 *            type: 'custom',
 *            factory: 'org.apache.apex.malhar.sql.schema.ApexSQLTableFactory',
 *            stream: {
 *              stream: true
 *            },
 *            operand: {
 *              endpoint: 'file',
 *              messageFormat: 'csv',
 *              endpointOperands: {
 *                directory: '/tmp/input'
 *              },
 *              messageFormatOperands: {
 *                schema: '{"separator":",","quoteChar":"\\"","fields":[{"name":"RowTime","type":"Date","constraints":{"format":"dd/MM/yyyy hh:mm:ss"}},{"name":"id","type":"Integer"},{"name":"Product","type":"String"},{"name":"units","type":"Integer"}]}'
 *            }
 *            }
 *         }
 *       ]
 *     }]
 *   }
 * </pre>
 *
 * @param model String content of model file.
 * @return Returns this {@link SQLExecEnvironment}
 */
public SQLExecEnvironment withModel(String model) {
    if (model == null) {
        return this;
    }
    Properties p = new Properties();
    p.put("model", "inline:" + model);
    try (Connection connection = DriverManager.getConnection("jdbc:calcite:", p)) {
        CalciteConnection conn = connection.unwrap(CalciteConnection.class);
        this.schema = conn.getRootSchema().getSubSchema(connection.getSchema());
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
    return this;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) Properties(java.util.Properties) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection)

Example 2 with CalciteConnection

use of org.apache.calcite.jdbc.CalciteConnection in project lucene-solr by apache.

the class CalciteSolrDriver method connect.

@Override
public Connection connect(String url, Properties info) throws SQLException {
    if (!this.acceptsURL(url)) {
        return null;
    }
    Connection connection = super.connect(url, info);
    CalciteConnection calciteConnection = (CalciteConnection) connection;
    final SchemaPlus rootSchema = calciteConnection.getRootSchema();
    String schemaName = info.getProperty("zk");
    if (schemaName == null) {
        throw new SQLException("zk must be set");
    }
    rootSchema.add(schemaName, new SolrSchema(info));
    // Set the default schema
    calciteConnection.setSchema(schemaName);
    return connection;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) SchemaPlus(org.apache.calcite.schema.SchemaPlus) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection)

Aggregations

Connection (java.sql.Connection)2 SQLException (java.sql.SQLException)2 CalciteConnection (org.apache.calcite.jdbc.CalciteConnection)2 Properties (java.util.Properties)1 SchemaPlus (org.apache.calcite.schema.SchemaPlus)1