[KYUUBI #884] Rewrite kyuubi-hive-jdbc in Java

<!--
Thanks for sending a pull request!

Here are some tips for you:
  1. If this is your first time, please read our contributor guidelines: https://kyuubi.readthedocs.io/en/latest/community/contributions.html
  2. If the PR is related to an issue in https://github.com/apache/incubator-kyuubi/issues, add '[KYUUBI #XXXX]' in your PR title, e.g., '[KYUUBI #XXXX] Your PR title ...'.
  3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][KYUUBI #XXXX] Your PR title ...'.
-->

### _Why are the changes needed?_
<!--
Please clarify why the changes are needed. For instance,
  1. If you add a feature, you can talk about the use case of it.
  2. If you fix a bug, you can clarify why it is a bug.
-->
close #765

### _How was this patch tested?_
- [ ] Add some test cases that check the changes thoroughly including negative and positive cases if possible

- [ ] Add screenshots for manual tests if appropriate

- [ ] [Run test](https://kyuubi.readthedocs.io/en/latest/tools/testing.html#running-tests) locally before make a pull request

Closes #884 from pan3793/jdbc-java.

Closes #884

ca7bad48 [Cheng Pan] Rewrite kyuubi-hive-jdbc in Java

Authored-by: Cheng Pan <chengpan@apache.org>
Signed-off-by: Cheng Pan <chengpan@apache.org>
This commit is contained in:
Cheng Pan 2021-08-02 17:35:00 +08:00
parent eb96c6ea64
commit 1a8b4ebaae
No known key found for this signature in database
GPG Key ID: F07E6C29ED4E2E5B
9 changed files with 157 additions and 123 deletions

View File

@ -32,6 +32,12 @@
<name>Kyuubi Project Dev Spark Extensions</name>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_${scala.binary.version}</artifactId>

View File

@ -32,6 +32,12 @@
<name>Kyuubi Project Dev TPCDS Generator</name>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_${scala.binary.version}</artifactId>

View File

@ -32,6 +32,10 @@
<name>Kyuubi Project Common</name>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>

View File

@ -0,0 +1,52 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.kyuubi.jdbc;
import java.lang.reflect.Field;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.Properties;
import org.apache.hive.jdbc.HiveConnection;
import org.apache.hive.service.rpc.thrift.TCLIService;
import org.apache.hive.service.rpc.thrift.TSessionHandle;
public class KyuubiConnection extends HiveConnection {
public KyuubiConnection(String url, Properties info) throws SQLException {
super(url, info);
}
@Override
public DatabaseMetaData getMetaData() throws SQLException {
if (isClosed()) {
throw new SQLException("Connection is closed");
}
try {
Field clientField = HiveConnection.class.getDeclaredField("client");
clientField.setAccessible(true);
TCLIService.Iface client = (TCLIService.Iface) clientField.get(this);
Field handleField = HiveConnection.class.getDeclaredField("sessHandle");
handleField.setAccessible(true);
TSessionHandle sessionHandle = (TSessionHandle) handleField.get(this);
return new KyuubiDatabaseMetaData(this, client, sessionHandle);
} catch (NoSuchFieldException | IllegalAccessException rethrow) {
throw new RuntimeException(rethrow);
}
}
}

View File

@ -0,0 +1,70 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.kyuubi.jdbc;
import org.apache.hive.jdbc.HiveDatabaseMetaData;
import org.apache.hive.jdbc.HiveQueryResultSet;
import org.apache.hive.service.cli.HiveSQLException;
import org.apache.hive.service.rpc.thrift.*;
import org.apache.thrift.TException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
public class KyuubiDatabaseMetaData extends HiveDatabaseMetaData {
private final KyuubiConnection conn;
private final TCLIService.Iface client;
private final TSessionHandle sessHandle;
public KyuubiDatabaseMetaData(KyuubiConnection conn, TCLIService.Iface client, TSessionHandle sessHandle) {
super(conn, client, sessHandle);
this.conn = conn;
this.client = client;
this.sessHandle = sessHandle;
}
@Override
public ResultSet getTables(String catalog, String schemaPattern,
String tableNamePattern, String[] types) throws SQLException {
TGetTablesReq getTableReq = new TGetTablesReq(sessHandle);
getTableReq.setCatalogName(catalog);
getTableReq.setSchemaName(schemaPattern == null ? "%" : schemaPattern);
getTableReq.setTableName(tableNamePattern);
if (types != null) {
getTableReq.setTableTypes(Arrays.asList(types));
}
TGetTablesResp getTableResp;
try {
getTableResp = client.GetTables(getTableReq);
} catch (TException rethrow) {
throw new SQLException(rethrow.getMessage(), "08S01", rethrow);
}
TStatus tStatus = getTableResp.getStatus();
if (tStatus.getStatusCode() != TStatusCode.SUCCESS_STATUS) {
throw new HiveSQLException(tStatus);
}
new HiveQueryResultSet.Builder(conn)
.setClient(client)
.setSessionHandle(sessHandle)
.setStmtHandle(getTableResp.getOperationHandle())
.build();
return super.getTables(catalog, schemaPattern, tableNamePattern, types);
}
}

View File

@ -15,25 +15,26 @@
* limitations under the License.
*/
package org.apache.kyuubi.jdbc
package org.apache.kyuubi.jdbc;
import java.sql.{Connection, DriverManager, SQLException}
import java.util.Properties
import org.apache.hive.jdbc.HiveDriver;
import org.apache.hive.jdbc.HiveDriver
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
class KyuubiDriver extends HiveDriver {
override def connect(url: String, info: Properties): Connection = {
if (acceptsURL(url)) {
new KyuubiConnection(url, info)
} else null
}
}
object KyuubiDriver {
try {
DriverManager.registerDriver(new KyuubiDriver)
} catch {
case e: SQLException => throw new RuntimeException("Failed to register driver", e)
}
public class KyuubiDriver extends HiveDriver {
static {
try {
DriverManager.registerDriver(new KyuubiDriver());
} catch (SQLException e) {
throw new RuntimeException("Failed to register driver", e);
}
}
@Override
public Connection connect(String url, Properties info) throws SQLException {
return acceptsURL(url) ? new KyuubiConnection(url, info) : null;
}
}

View File

@ -1,41 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.kyuubi.jdbc
import java.sql.{DatabaseMetaData, SQLException}
import java.util.Properties
import org.apache.hive.jdbc.HiveConnection
import org.apache.hive.service.rpc.thrift.{TCLIService, TSessionHandle}
class KyuubiConnection(url: String, info: Properties) extends HiveConnection(url, info) {
override def getMetaData: DatabaseMetaData = {
if (isClosed) {
throw new SQLException("Connection is closed")
} else {
val clientField = classOf[HiveConnection].getDeclaredField("client")
clientField.setAccessible(true)
val client = clientField.get(this).asInstanceOf[TCLIService.Iface]
val handleField = classOf[HiveConnection].getDeclaredField("sessHandle")
handleField.setAccessible(true)
val sessionHandle = handleField.get(this).asInstanceOf[TSessionHandle]
new KyuubiDatabaseMetaData(this, client, sessionHandle)
}
}
}

View File

@ -1,59 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.kyuubi.jdbc
import java.sql.{ResultSet, SQLException}
import scala.collection.JavaConverters._
import org.apache.hive.jdbc.{HiveDatabaseMetaData, HiveQueryResultSet}
import org.apache.hive.service.cli.HiveSQLException
import org.apache.hive.service.rpc.thrift.{TGetTablesReq, TSessionHandle, TStatusCode}
import org.apache.hive.service.rpc.thrift.TCLIService.Iface
import org.apache.thrift.TException
class KyuubiDatabaseMetaData(conn: KyuubiConnection, client: Iface, sessHandle: TSessionHandle)
extends HiveDatabaseMetaData(conn, client, sessHandle) {
override def getTables(
catalog: String,
schemaPattern: String,
tableNamePattern: String,
types: Array[String]): ResultSet = {
val getTableReq: TGetTablesReq = new TGetTablesReq(sessHandle)
getTableReq.setCatalogName(catalog)
getTableReq.setSchemaName(if (schemaPattern == null) "%" else schemaPattern)
getTableReq.setTableName(tableNamePattern)
if (types != null) {
getTableReq.setTableTypes(types.toList.asJava)
}
val getTableResp = try {
client.GetTables(getTableReq)
} catch {
case e: TException => throw new SQLException(e.getMessage, "08S01", e)
}
val tStatus = getTableResp.getStatus
if (tStatus.getStatusCode != TStatusCode.SUCCESS_STATUS) {
throw new HiveSQLException(tStatus)
}
new HiveQueryResultSet.Builder(conn)
.setClient(client)
.setSessionHandle(sessHandle)
.setStmtHandle(getTableResp.getOperationHandle)
.build
}
}

View File

@ -1083,11 +1083,6 @@
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
</dependency>
<dependency>
<groupId>org.scalatest</groupId>
<artifactId>scalatest_${scala.binary.version}</artifactId>