gpt4 book ai didi

apache-calcite - Apache 方解石 |使用关系代数从 MongoDB 查询数据

转载 作者:行者123 更新时间:2023-12-03 22:43:40 27 4
gpt4 key购买 nike

我能够获得 MongoDB 连接并能够获得节点

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

但是当我执行节点时,出现空指针异常。

完整代码:

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);
}
}

从上面的代码中得到以下异常,在执行 Mongo DB 集合的 relnode 时,模式获取空值

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)

最佳答案

这应该有帮助:

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);
...
}

您需要一个收藏模型。像这样:model-example模型文件包含与 mongodb 建立连接所需的所有数据。

关于apache-calcite - Apache 方解石 |使用关系代数从 MongoDB 查询数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45373953/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com