Apache Calcite | Querying data from MongoDB using relational algebra
I can get MongoDB connection and get node
(LogicalTableScan(table=[[enlivenDev, collection1]]))
But when I execute node I get a 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);
}
}
Following exception from above code, there is a schema getting null values ββand relnode execution 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)
+3
source to share
1 answer
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 for your collection. Example: example model The model file contains all the data needed to establish a connection to mongodb.
-1
source to share