You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@calcite.apache.org by Srinivasan Hariharan02 <Sr...@infosys.com> on 2016/03/16 13:58:10 UTC

Connect multi database using calcite jdbc

Hi,

I am trying to query hive tables frm calcite jdbc.  Took below code as clue to connect hive using calcite jdbc.

https://github.com/apache/calcite/blob/master/core/src/test/java/org/apache/calcite/test/MultiJdbcSchemaJoinTest.java

My code looks like below

Class.forName("org.apache.calcite.jdbc.Driver");
        Class.forName("org.apache.hive.jdbc.HiveDriver");
        final String db1 = "jdbc:hive2://host:10000";
        Connection c1 = DriverManager.getConnection(db1, "username", "password");
        Statement stmt1 = c1.createStatement();
       stmt1.execute("create table table1(id string, "
                + "field1 string)");
        stmt1.execute("insert into table1 values('a', 'aaaa')");
        c1.close();
        Connection connection = DriverManager.getConnection("jdbc:calcite:");
        CalciteConnection calciteConnection =
                connection.unwrap(CalciteConnection.class);
        SchemaPlus rootSchema = calciteConnection.getRootSchema();
        final DataSource ds1 =
                JdbcSchema.dataSource(db1, "org.apache.hive.jdbc.HiveDriver", "username", "password");
        rootSchema.add("DB1",
                JdbcSchema.create(rootSchema, "DB1", ds1, null, null));


       Statement stmt3 = connection.createStatement();
        ResultSet rs = stmt3.executeQuery("select id1,field1 from table1");

I can create the table successfully in hive using hive jdbc connection , However when I query the table from calcite jdbc it throws below error saying table1 not found. Could anyone help.

Exception in thread "main" java.sql.SQLException: Error while executing SQL "select id1,field1 from table1": From line 1, column 15 to line 1, column 20: Table 'TABLE1' not found
               at org.apache.calcite.avatica.Helper.createException(Helper.java:52)
               at org.apache.calcite.avatica.Helper.createException(Helper.java:41)
               at org.apache.calcite.avatica.AvaticaStatement.executeInternal(AvaticaStatement.java:143)
               at org.apache.calcite.avatica.AvaticaStatement.executeQuery(AvaticaStatement.java:186)
               at org.infosys.sqldriver.jdbc.ConnectTest.main(ConnectTest.java:51)
               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)
Caused by: org.apache.calcite.runtime.CalciteContextException: From line 1, column 15 to line 1, column 20: Table 'TABLE1' not found
               at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
               at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
               at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
               at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
               at org.apache.calcite.runtime.Resources$ExInstWithCause.ex(Resources.java:405)
               at org.apache.calcite.sql.SqlUtil.newContextException(SqlUtil.java:715)
               at org.apache.calcite.sql.SqlUtil.newContextException(SqlUtil.java:703)
               at org.apache.calcite.sql.validate.SqlValidatorImpl.newValidationError(SqlValidatorImpl.java:3866)
               at org.apache.calcite.sql.validate.IdentifierNamespace.validateImpl(IdentifierNamespace.java:106)
               at org.apache.calcite.sql.validate.AbstractNamespace.validate(AbstractNamespace.java:86)
               at org.apache.calcite.sql.validate.SqlValidatorImpl.validateNamespace(SqlValidatorImpl.java:842)
               at org.apache.calcite.sql.validate.SqlValidatorImpl.validateQuery(SqlValidatorImpl.java:828)
               at org.apache.calcite.sql.validate.SqlValidatorImpl.validateFrom(SqlValidatorImpl.java:2743)
               at org.apache.calcite.sql.validate.SqlValidatorImpl.validateFrom(SqlValidatorImpl.java:2728)
               at org.apache.calcite.sql.validate.SqlValidatorImpl.validateSelect(SqlValidatorImpl.java:2946)
               at org.apache.calcite.sql.validate.SelectNamespace.validateImpl(SelectNamespace.java:60)
               at org.apache.calcite.sql.validate.AbstractNamespace.validate(AbstractNamespace.java:86)
               at org.apache.calcite.sql.validate.SqlValidatorImpl.validateNamespace(SqlValidatorImpl.java:842)
               at org.apache.calcite.sql.validate.SqlValidatorImpl.validateQuery(SqlValidatorImpl.java:828)
               at org.apache.calcite.sql.SqlSelect.validate(SqlSelect.java:207)
               at org.apache.calcite.sql.validate.SqlValidatorImpl.validateScopedExpression(SqlValidatorImpl.java:802)
               at org.apache.calcite.sql.validate.SqlValidatorImpl.validate(SqlValidatorImpl.java:516)
               at org.apache.calcite.sql2rel.SqlToRelConverter.convertQuery(SqlToRelConverter.java:569)
               at org.apache.calcite.prepare.Prepare.prepareSql(Prepare.java:223)
               at org.apache.calcite.prepare.Prepare.prepareSql(Prepare.java:190)
               at org.apache.calcite.prepare.CalcitePrepareImpl.prepare2_(CalcitePrepareImpl.java:727)
               at org.apache.calcite.prepare.CalcitePrepareImpl.prepare_(CalcitePrepareImpl.java:587)
               at org.apache.calcite.prepare.CalcitePrepareImpl.prepareSql(CalcitePrepareImpl.java:557)
               at org.apache.calcite.jdbc.CalciteConnectionImpl.parseQuery(CalciteConnectionImpl.java:214)
               at org.apache.calcite.jdbc.CalciteMetaImpl.prepareAndExecute(CalciteMetaImpl.java:573)
               at org.apache.calcite.avatica.AvaticaConnection.prepareAndExecuteInternal(AvaticaConnection.java:571)
               at org.apache.calcite.avatica.AvaticaStatement.executeInternal(AvaticaStatement.java:135)
               ... 7 more
Caused by: org.apache.calcite.sql.validate.SqlValidatorException: Table 'TABLE1' not found
               at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
               at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
               at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
               at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
               at org.apache.calcite.runtime.Resources$ExInstWithCause.ex(Resources.java:405)
               at org.apache.calcite.runtime.Resources$ExInst.ex(Resources.java:514)
               ... 34 more
Regards,
Srinivasan Hariharan
+91-9940395830