我想在不使用jdbc连接的情况下使用apache calcite api raw。我可以使用jdbc api就好了但是在尝试使用api时我得到了空的ptr异常。到目前为止我所做的是:
package calcite.examples
import java.util.Properties
import calcite.demo.DemoSchema
import org.apache.calcite.DataContext
import org.apache.calcite.config.CalciteConnectionConfigImpl
import org.apache.calcite.jdbc.CalcitePrepare.Query
import org.apache.calcite.jdbc.{CalcitePrepare, CalciteSchema, JavaTypeFactoryImpl}
import org.apache.calcite.prepare.CalcitePrepareImpl
import scala.collection.JavaConverters._
object TryIt extends App
{
val ctx = new AdapterContext
val sql = Query.of[Any]("SELECT * FROM dep")
// assert(sql.rel != null)
val elementType = classOf[Array[Object]]
val prepared = new CalcitePrepareImpl().prepareSql(ctx, sql, elementType, -1)
val enumerable = prepared.enumerable(new MyDataContext)
}
class AdapterContext extends CalcitePrepare.Context
{
private val properties = new Properties
private val rootSchema = CalciteSchema.createRootSchema(true)
rootSchema.add("default", new DemoSchema)
// default schema names
override def getDefaultSchemaPath = List("default").asJava
override def spark() = {
val enable = config.spark
CalcitePrepare.Dummy.getSparkHandler(enable)
}
override val config = new CalciteConnectionConfigImpl(properties)
override val getTypeFactory = new JavaTypeFactoryImpl
override def getRootSchema = rootSchema
override def getDataContext = new MyDataContext
}
class MyDataContext extends DataContext
{
override def get(name: String) = {
println(s"MyDataContext name=$name")
null
}
override def getTypeFactory = ???
override def getQueryProvider = ???
override def getRootSchema = ???
}
但是当我运行这个时,我得到了
MyDataContext name = v0stashed
Exception in thread "main" java.lang.NullPointerException
at org.apache.calcite.interpreter.Interpreter.<init>(Interpreter.java:71)
at Baz.bind(Unknown Source)
at org.apache.calcite.jdbc.CalcitePrepare$CalciteSignature.enumerable(CalcitePrepare.java:327)
at calcite.examples.TryIt$.delayedEndpoint$calcite$examples$TryIt$1(TryIt.scala:26)
at calcite.examples.TryIt$delayedInit$body.apply(TryIt.scala:18)
at scala.Function0$class.apply$mcV$sp(Function0.scala:34)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
at scala.App$$anonfun$main$1.apply(App.scala:76)
at scala.App$$anonfun$main$1.apply(App.scala:76)
at scala.collection.immutable.List.foreach(List.scala:381)
at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:35)
at scala.App$class.main(App.scala:76)
at calcite.examples.TryIt$.main(TryIt.scala:18)
at calcite.examples.TryIt.main(TryIt.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
请注意,它试图从上下文中获取v0stashed,我不知道它应该是什么。我看到的唯一一个使用CalciteConnection的CalcitePrepare.Context,我试图避免使用它。我还从方解石文档中获取了代码片段,但它与方解石1.7已经过时了
想法?
答案 0 :(得分:0)
这几乎可以工作,我使用JdbcSchema
更改了一些内容。我的预感是你尝试使用火花模式,只是根据方解石的版本号来判断,我不认为它保持得很好。尝试一个更简单的模式,比如反射式HR模式,或JDBC模式,它应该可以工作,对你所拥有的东西进行一些小改动。
答案 1 :(得分:0)
这里显然有一些疯狂的事情。您需要将从prepare调用中获得的internalParameters
传递到DataContext中,并在get
中进行查找。显然方解石使用它来传递查询对象。您可能还希望实现其他DataContext键(当前时间等)。
final class MyDataContext(rootSchema: SchemaPlus, map: util.Map[String, Object])
extends DataContext {
override def get(name: String): AnyRef = map.get(name)
...
}
// ctx is your AdapterContext from above
val prepared = new CalcitePrepareImpl().prepareSql(ctx, query, classOf[Array[Object]], -1)
val dataContext = new DerpDataContext(
ctx.getRootSchema.plus(),
prepared.internalParameters
)