-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#164 Add the ability to create Hive tables via a JDBC connection.
- Loading branch information
Showing
7 changed files
with
155 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
pramen/core/src/main/scala/za/co/absa/pramen/core/utils/hive/QueryExecutorJdbc.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright 2022 ABSA Group Limited | ||
* | ||
* Licensed 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 za.co.absa.pramen.core.utils.hive | ||
|
||
import org.slf4j.LoggerFactory | ||
|
||
import java.sql.{Connection, ResultSet, SQLSyntaxErrorException} | ||
import scala.util.Try | ||
|
||
class QueryExecutorJdbc(connection: Connection) extends QueryExecutor { | ||
private val log = LoggerFactory.getLogger(this.getClass) | ||
|
||
override def doesTableExist(dbName: String, tableName: String): Boolean = { | ||
val query = s"SELECT 1 FROM $tableName WHERE 0 = 1" | ||
|
||
Try { | ||
execute(query) | ||
}.isSuccess | ||
} | ||
|
||
@throws[SQLSyntaxErrorException] | ||
override def execute(query: String): Unit = { | ||
log.info(s"Executing SQL: $query") | ||
val statement = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY) | ||
|
||
val resultSet = statement.executeQuery(query) | ||
|
||
resultSet.close() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
.../core/src/test/scala/za/co/absa/pramen/core/tests/utils/hive/QueryExecutorJdbcSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright 2022 ABSA Group Limited | ||
* | ||
* Licensed 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 za.co.absa.pramen.core.tests.utils.hive | ||
|
||
import org.scalatest.BeforeAndAfterAll | ||
import org.scalatest.wordspec.AnyWordSpec | ||
import za.co.absa.pramen.core.fixtures.RelationalDbFixture | ||
import za.co.absa.pramen.core.samples.RdbExampleTable | ||
import za.co.absa.pramen.core.utils.hive.QueryExecutorJdbc | ||
|
||
import java.sql.SQLSyntaxErrorException | ||
|
||
class QueryExecutorJdbcSuite extends AnyWordSpec with BeforeAndAfterAll with RelationalDbFixture { | ||
|
||
override protected def beforeAll(): Unit = { | ||
super.beforeAll() | ||
RdbExampleTable.Company.initTable(getConnection) | ||
} | ||
|
||
override protected def afterAll(): Unit = { | ||
RdbExampleTable.Company.dropTable(getConnection) | ||
super.afterAll() | ||
} | ||
|
||
"QueryExecutorJdbc" should { | ||
"execute JDBC queries" in { | ||
val connection = getConnection | ||
|
||
val qe = new QueryExecutorJdbc(connection) | ||
|
||
qe.execute("SELECT * FROM company") | ||
} | ||
|
||
"execute CREATE TABLE queries" in { | ||
val connection = getConnection | ||
|
||
val qe = new QueryExecutorJdbc(connection) | ||
|
||
qe.execute("CREATE TABLE my_table (id INT)") | ||
|
||
val exist = qe.doesTableExist(database, "my_table") | ||
|
||
assert(exist) | ||
} | ||
|
||
"throw an exception on errors" in { | ||
val qe = new QueryExecutorJdbc(getConnection) | ||
|
||
val ex = intercept[SQLSyntaxErrorException] { | ||
qe.execute("SELECT * FROM does_not_exist") | ||
} | ||
|
||
assert(ex.getMessage.contains("object not found")) | ||
} | ||
|
||
"return true if the table is found" in { | ||
val qe = new QueryExecutorJdbc(getConnection) | ||
|
||
val exist = qe.doesTableExist(database, "company") | ||
|
||
assert(exist) | ||
} | ||
|
||
"return false if the table is not found" in { | ||
val qe = new QueryExecutorJdbc(getConnection) | ||
|
||
val exist = qe.doesTableExist(database, "does_not_exist") | ||
|
||
assert(!exist) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters