Skip to content

Commit

Permalink
Renames sql to dsql
Browse files Browse the repository at this point in the history
  - updates logging calls in DruidHttpClient
  - updates documentation

ing-bankGH-90
  • Loading branch information
anskarl committed May 8, 2020
1 parent dcc1f98 commit 94033e5
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 18 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,12 @@ All the methods above can be applied to any timeseries, group-by or top-N query

## Druid SQL support

Instead of using the Druid native API, Scruid also supports Druid queries
via [SQL](https://druid.apache.org/docs/latest/querying/sql.html).
Instead of using the Druid native API, Scruid also supports Druid queries via [SQL](https://druid.apache.org/docs/latest/querying/sql.html).

```scala
import ing.wbaa.druid.SQL._

val query = sql"""SELECT COUNT(*) as "count" FROM wikipedia WHERE "__time" >= TIMESTAMP '2015-09-12 00:00:00'"""
val query = dsql"""SELECT COUNT(*) as "count" FROM wikipedia WHERE "__time" >= TIMESTAMP '2015-09-12 00:00:00'"""

val response = query.execute()
```
Expand Down
12 changes: 6 additions & 6 deletions docs/sql.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import scala.concurrent.Future
import ing.wbaa.druid.SQL._
import ing.wbaa.druid.DruidSQLResults

val query = sql"""SELECT COUNT(*) as "count" FROM wikipedia WHERE "__time" >= TIMESTAMP '2015-09-12 00:00:00'"""
val query = dsql"""SELECT COUNT(*) as "count" FROM wikipedia WHERE "__time" >= TIMESTAMP '2015-09-12 00:00:00'"""

val response: Future[DruidSQLResults] = query.execute()

Expand All @@ -23,7 +23,7 @@ Function `sql`, allows multiline queries:
```scala
import ing.wbaa.druid.SQL._

val query = sql"""
val query = dsql"""
|SELECT COUNT(*) as "count"
|FROM wikipedia
|WHERE "__time" >= TIMESTAMP '2015-09-12 00:00:00'
Expand All @@ -39,7 +39,7 @@ val countColumnName = "count"
val dataSourceName = "wikipedia"
val dateTime = "2015-09-12 00:00:00"

val query = sql"""
val query = dsql"""
|SELECT COUNT(*) as "${countColumnName}"
|FROM ${dataSourceName}
|WHERE "__time" >= TIMESTAMP '${dateTime}'
Expand All @@ -60,7 +60,7 @@ val fromDateTime = LocalDateTime.of(2015, 9, 12, 0, 0, 0, 0)
val untilDateTime = fromDateTime.plusDays(1)

val queryParameterized: SQLQuery.Parameterized =
sql"""
dsql"""
|SELECT FLOOR(__time to HOUR) AS hourTime, count(*) AS "count"
|FROM wikipedia
|WHERE "__time" BETWEEN ? AND ?
Expand All @@ -86,7 +86,7 @@ val contextParameters = Map(
QueryContext.SqlTimeZone -> "America/Los_Angeles"
)
val query =
sql"""SELECT COUNT(*) as "count" FROM wikipedia WHERE "__time" >= TIMESTAMP '2015-09-12 00:00:00'"""
dsql"""SELECT COUNT(*) as "count" FROM wikipedia WHERE "__time" >= TIMESTAMP '2015-09-12 00:00:00'"""
.setContext(contextParameters)
```

Expand All @@ -100,7 +100,7 @@ import akka.stream.scaladsl.Source
import ing.wbaa.druid.SQL._
import ing.wbaa.druid.{DruidSQLResult, DruidSQLResults}

val query = sql"""SELECT COUNT(*) as "count" FROM wikipedia WHERE "__time" >= TIMESTAMP '2015-09-12 00:00:00'"""
val query = dsql"""SELECT COUNT(*) as "count" FROM wikipedia WHERE "__time" >= TIMESTAMP '2015-09-12 00:00:00'"""

val source: Source[DruidSQLResult, NotUsed] = query.stream()

Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/ing/wbaa/druid/SQL.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package ing.wbaa.druid
object SQL {

implicit class StringToSQL(val sc: StringContext) extends AnyVal {
def sql(query: Any*)(implicit context: Map[String, String] = Map.empty,
config: DruidConfig = DruidConfig.DefaultConfig): SQLQuery =
def dsql(query: Any*)(implicit context: Map[String, String] = Map.empty,
config: DruidConfig = DruidConfig.DefaultConfig): SQLQuery =
SQLQuery(sc.s(query: _*), context)(config)
}

Expand Down
15 changes: 10 additions & 5 deletions src/main/scala/ing/wbaa/druid/client/DruidHttpClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ class DruidHttpClient private (connectionFlow: DruidHttpClient.ConnectionFlowTyp
.map { entity =>
val requestURL =
if (query.queryType == QueryType.SQL) s"${druidConfig.url}sql/" else druidConfig.url
logger.info(s"requestURL = ${requestURL}")

logger.debug("requestURL = {}", requestURL)

HttpRequest(HttpMethods.POST, uri = requestURL)
.withEntity(entity.withContentType(`application/json`))
}
Expand All @@ -82,9 +84,10 @@ class DruidHttpClient private (connectionFlow: DruidHttpClient.ConnectionFlowTyp
queryType: QueryType,
request: HttpRequest
)(implicit druidConfig: DruidConfig): Future[T] = {
logger.debug(
s"Executing api ${request.method} request to ${request.uri} with entity: ${request.entity}"
)
logger.debug("Executing api {} request to {} with entity: {}",
request.method,
request.uri,
request.entity)

Source
.single(request)
Expand All @@ -101,7 +104,9 @@ class DruidHttpClient private (connectionFlow: DruidHttpClient.ConnectionFlowTyp
.map { entity =>
val requestURL =
if (q.queryType == QueryType.SQL) s"${druidConfig.url}sql/" else druidConfig.url
logger.info(s"requestURL = ${requestURL}")

logger.debug("requestURL = {}", requestURL)

HttpRequest(HttpMethods.POST, uri = requestURL)
.withEntity(entity.withContentType(`application/json`))
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/scala/ing/wbaa/druid/sql/SQLQuerySpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class SQLQuerySpec extends WordSpec with Matchers with ScalaFutures with CirceDe

"SQL query" should {

val query: SQLQuery = sql"""
val query: SQLQuery = dsql"""
|SELECT FLOOR(__time to HOUR) AS hourTime, count(*) AS "count"
|FROM wikipedia
|WHERE "__time" BETWEEN TIMESTAMP '2015-09-12 00:00:00' AND TIMESTAMP '2015-09-13 00:00:00'
Expand Down Expand Up @@ -57,7 +57,7 @@ class SQLQuerySpec extends WordSpec with Matchers with ScalaFutures with CirceDe
val untilDateTime = fromDateTime.plusDays(1)

val queryParameterized: SQLQuery.Parameterized =
sql"""
dsql"""
|SELECT FLOOR(__time to HOUR) AS hourTime, count(*) AS "count"
|FROM wikipedia
|WHERE "__time" BETWEEN ? AND ?
Expand Down

0 comments on commit 94033e5

Please sign in to comment.