Apache Calcite | Querying data from MongoDB by usi

2019-05-14 02:17发布

问题:

I am able to get a MongoDB connection and able to get a node

(LogicalTableScan(table=[[enlivenDev, collection1]]))

But when I execute the node, I am getting null pointer exception.

Complete code:

private void executeMongoDB(){
        final FrameworkConfig config = mongoConfig().build();
        final RelBuilder builder = RelBuilder.create(config);
        final RelNode node =  builder.scan("collection1").build();
        System.out.println(RelOptUtil.toString(node));  
        PreparedStatement ps = RelRunners.run(node);
        ResultSet resultSet = ps.executeQuery();
}
    public static Frameworks.ConfigBuilder mongoConfig() {
            final SchemaPlus rootSchema = Frameworks.createRootSchema(true);
            org.apache.calcite.tools.Frameworks.ConfigBuilder configBuilder =Frameworks.newConfigBuilder()
                    .parserConfig(SqlParser.Config.DEFAULT)
                    .defaultSchema(
                        MongoDBConnection.addMongoSchema(rootSchema, CalciteAssert.SchemaSpec.MONGO_DB))
                    .traitDefs((List<RelTraitDef>) null)
                    .programs(Programs.heuristicJoinOrder(Programs.RULE_SET, true, 2));
            return configBuilder;
        }
public static SchemaPlus addMongoSchema(SchemaPlus rootSchema, SchemaSpec schema) {
        switch (schema) {
        case MONGO_DB:
            return rootSchema.add("enlivenDev",
                    MongoSchemaFactory.create(rootSchema, "192.168.1.01", "enlivenDev", 27017, "mgp", "mg1"));
        default:
            throw new AssertionError("unknown schema " + schema);
        }
    }

Getting following exception from an above code,there is schema getting null values,while execute relnode of Mongo DB collection

SEVERE: exception while executing query: null
java.sql.SQLException: exception while executing query: null
    at org.apache.calcite.avatica.Helper.createException(Helper.java:56)
    at org.apache.calcite.avatica.Helper.createException(Helper.java:41)
    at org.apache.calcite.avatica.AvaticaConnection.executeQueryInternal(AvaticaConnection.java:540)
    at org.apache.calcite.avatica.AvaticaPreparedStatement.executeQuery(AvaticaPreparedStatement.java:133)
    at org.ramyam.eis.core.ApachecalcitePOC.processMongoDB(ApachecalcitePOC.java:106)
    at org.ramyam.eis.core.ApachecalcitePOC.main(ApachecalcitePOC.java:42)
Caused by: java.lang.NullPointerException
    at org.apache.calcite.schema.Schemas.queryable(Schemas.java:232)
    at Baz.bind(Unknown Source)
    at org.apache.calcite.jdbc.CalcitePrepare$CalciteSignature.enumerable(CalcitePrepare.java:335)
    at org.apache.calcite.jdbc.CalciteConnectionImpl.enumerable(CalciteConnectionImpl.java:294)
    at org.apache.calcite.jdbc.CalciteMetaImpl._createIterable(CalciteMetaImpl.java:559)
    at org.apache.calcite.jdbc.CalciteMetaImpl.createIterable(CalciteMetaImpl.java:550)
    at org.apache.calcite.avatica.AvaticaResultSet.execute(AvaticaResultSet.java:204)
    at org.apache.calcite.jdbc.CalciteResultSet.execute(CalciteResultSet.java:67)
    at org.apache.calcite.jdbc.CalciteResultSet.execute(CalciteResultSet.java:44)
    at org.apache.calcite.avatica.AvaticaConnection.executeQueryInternal(AvaticaConnection.java:536)

回答1:

This should help:

public void useCalcite(String modelPath) {
    Connection connection = DriverManager.getConnection("jdbc:calcite:model=" + modelPath);
    CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
    String query = "select count(*) from zips";
    Statement statement = calciteConnection.createStatement();
    ResultSet result = statement.executeQuery(query);
    ...
}

You need a model of your collection. Like this: model-example The model file contains all data needed to establish a connection to mongodb.