Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ Library for using [Amazon Athena](https://aws.amazon.com/athena/) JDBC Driver wi

![CI Status](https://github.com/zaneli/scalikejdbc-athena/actions/workflows/ci.yml/badge.svg)

**scalikejdbc-athena was originally developed to work around the fact that the Athena JDBC driver did not support PreparedStatement.**
**This limitation was likely fixed in Athena JDBC driver version 2.x or later, so in most cases this library is no longer necessary.**
**However, to maintain compatibility with the previous behavior — where PreparedStatement was processed via scalikejdbc-athena's custom string replacement — we provide the `useCustomPreparedStatement` parameter.**

## setup

- Download Athena JDBC driver
Expand Down Expand Up @@ -52,6 +56,20 @@ athena {
LogLevel=3
}
}

# compat PreparedStatement custom implementation
athena {
default {
driver="com.amazon.athena.jdbc.AthenaDriver"
url="jdbc:athena://AwsRegion={REGION}"
readOnly="false"
useCustomPreparedStatement="true"
S3OutputLocation="s3://query-results-bucket/folder/"
AwsCredentialsProviderClass="DefaultChain"
LogPath="logs/application.log"
LogLevel=3
}
}
```

If you need to update partitions etc., set `readOnly="false"`
Expand Down
14 changes: 11 additions & 3 deletions src/main/scala/scalikejdbc/athena/AthenaSession.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,17 @@ import scalikejdbc.{DBConnectionAttributes, DBSession, SettingsProvider, TimeZon
class AthenaSession(config: Config) extends DBSession {

override private[scalikejdbc] val conn: Connection = {
val c = new AthenaConnection(DriverManager.getConnection(config.url, config.options))
config.readOnly.foreach(c.setReadOnly)
c
val original = DriverManager.getConnection(config.url, config.options)
val connection = if (config.useCustomPreparedStatement) {
// In older versions of the Athena JDBC driver, PreparedStatement was not supported,
// so we used a custom implementation as a workaround.
// This is generally unnecessary for Athena JDBC driver 2.x or later.
new AthenaConnection(original)
} else {
original
}
config.readOnly.foreach(connection.setReadOnly)
connection
}

def getTmpStagingDir: Option[String] = config.getTmpStagingDir
Expand Down
2 changes: 2 additions & 0 deletions src/main/scala/scalikejdbc/athena/Config.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Config(dbName: Any) {
private[athena] lazy val driver: String = map.getOrElse(Driver, throw new ConfigException(s"no configuration setting: key=$prefix.$Driver"))
private[athena] lazy val readOnly: Option[Boolean] = map.get(ReadOnly).flatMap(v => Try(v.toBoolean).toOption)
private[athena] lazy val timeZone: Option[String] = map.get(TimeZone)
private[athena] lazy val useCustomPreparedStatement: Boolean = map.get(UseCustomPreparedStatement).flatMap(v => Try(v.toBoolean).toOption).getOrElse(false)

private[athena] lazy val options: Properties = {
val p = new Properties()
Expand Down Expand Up @@ -69,6 +70,7 @@ private object Config {
val Driver = "driver"
val ReadOnly = "readOnly"
val TimeZone = "timeZone"
val UseCustomPreparedStatement = "useCustomPreparedStatement"

val S3OutputLocation = "S3OutputLocation"
val S3OutputLocationPrefix = "S3OutputLocationPrefix"
Expand Down
20 changes: 13 additions & 7 deletions src/test/resources/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,19 @@ athena {
}
v2 {
default {
driver="com.simba.athena.jdbc.Driver"
url="jdbc:awsathena://AwsRegion=us-east-2"
readOnly="false"
Workgroup="my-own-workgroup"
AwsCredentialsProviderClass="com.simba.athena.amazonaws.auth.profile.ProfileCredentialsProvider"
LogPath="logs/application.log"
LogLevel=3
driver="com.simba.athena.jdbc.Driver"
url="jdbc:awsathena://AwsRegion=us-east-2"
readOnly="false"
Workgroup="my-own-workgroup"
AwsCredentialsProviderClass="com.simba.athena.amazonaws.auth.profile.ProfileCredentialsProvider"
LogPath="logs/application.log"
LogLevel=3
}
}
compat {
driver="com.amazon.athena.jdbc.AthenaDriver"
url="jdbc:athena://AwsRegion=ap-southeast-1"
useCustomPreparedStatement="true"
S3OutputLocation="s3://query-results-bucket/folder/"
}
}
14 changes: 14 additions & 0 deletions src/test/scala/scalikejdbc/athena/ConfigSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class ConfigSpec extends AnyFunSpec with OptionValues {
assert(config.getTmpStagingDir.isEmpty)
assert(config.readOnly.value === false)
assert(config.timeZone.isEmpty)
assert(config.useCustomPreparedStatement === false)
}
it("workgropup") {
val config = new Config("workgroup-only")
Expand All @@ -24,6 +25,7 @@ class ConfigSpec extends AnyFunSpec with OptionValues {
assert(config.getTmpStagingDir.isEmpty)
assert(config.readOnly.value === false)
assert(config.timeZone.isEmpty)
assert(config.useCustomPreparedStatement === false)
}
it("primary workgroupRequires S3OutputLocation") {
assertThrows[ConfigException](new Config("workgroup-primary").options)
Expand All @@ -37,6 +39,7 @@ class ConfigSpec extends AnyFunSpec with OptionValues {
assert(config.getTmpStagingDir.value === config.options.getProperty("S3OutputLocation"))
assert(config.readOnly.value === true)
assert(config.timeZone.value === "UTC")
assert(config.useCustomPreparedStatement === false)
}
it("v3") {
// This test ensures that new config parameters is supported
Expand All @@ -54,6 +57,7 @@ class ConfigSpec extends AnyFunSpec with OptionValues {
assert(config.getTmpStagingDir.isEmpty)
assert(config.readOnly.value === false)
assert(config.timeZone.isEmpty)
assert(config.useCustomPreparedStatement === false)
}
it("invalid settings") {
val config = new Config("duplicated")
Expand All @@ -66,5 +70,15 @@ class ConfigSpec extends AnyFunSpec with OptionValues {
it("unsupported db name type") {
assertThrows[ConfigException](new Config("athena".toCharArray))
}
it("compat") {
val config = new Config("compat")
assert(config.url === "jdbc:athena://AwsRegion=ap-southeast-1")
assert(config.options.getProperty("S3OutputLocation") === "s3://query-results-bucket/folder/")
assert(config.options.getProperty("LogPath") === null)
assert(config.getTmpStagingDir.isEmpty)
assert(config.readOnly.isEmpty)
assert(config.timeZone.isEmpty)
assert(config.useCustomPreparedStatement === true)
}
}
}
Loading