Skip to content

Commit

Permalink
Fix code examples in readme
Browse files Browse the repository at this point in the history
  • Loading branch information
bjgbeelen authored Mar 27, 2018
1 parent 47eaa0b commit 036ef47
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,22 @@ Scruid (Scala+Druid) is an open source library that allows you to compose querie

Currently the API is under heavy development, so changes might occur.



## Example queries:

Scruid provides three query consructors: `TopNQuery`, `GroupByQuery` and `TimeSeriesQuery` (see below for details). You can call the `execute` method ona query to send the query to Druid. This will return a `Future[DruidResponse]`. This response contains the [Circe](http://circe.io) JSON data without having it parsed to a specific case class yet. To interpret this JSON data you can run two methods on a `DruidResponse`:

- `.list[T](implicit decoder: Decoder[T]): List[T]` : This decodes the JSON to a list with items of type `T`.
- `.series[T](implicit decoder: Decoder[T]): Map[ZonedDateTime, T]` : This decodes the JSON to a timeseries map with the timestamp as key and `T` as value.

Below the example queries supported by Scruid. For more information about how to query Druid, and what query to pick, please refer to the [Druid documentation](http://druid.io/docs/latest/querying/querying.html)

### TopN query
```scala
case class TopCountry(count: Int, countryName: String = null)

TopNQuery(
val response = TopNQuery(
dimension = Dimension(
dimension = "countryName"
),
Expand All @@ -27,26 +34,25 @@ TopNQuery(
),
intervals = List("2011-06-01/2017-06-01")
).execute
```

val result: Map[ZonedDateTime, List[TopCountry]] = response.series[List[TopCountry]]
```

This will return a `Future[DruidResponse]`. This response contains the Circe JSON data without having it parsed to a specific case class yet. To interpret this JSON data you can run two methods on a `DruidResponse`:

- `.list[T](implicit decoder: Decoder[T]): List[T]` : This decodes the JSON to a list with items of type `T`.
- `.series[T](implicit decoder: Decoder[T]): Map[ZonedDateTime, T]` : This decodes the JSON to a timeseries map with the timestamp as key and `T` as value.

### GroupBy query

```scala
case class GroupByIsAnonymous(isAnonymous: Boolean, count: Int)

val result = GroupByQuery[GroupByIsAnonymous](
val response = GroupByQuery(
aggregations = List(
CountAggregation(name = "count")
),
dimensions = List("isAnonymous"),
intervals = List("2011-06-01/2017-06-01")
).execute()

val result: List[GroupByIsAnonymous] = response.list[GroupByIsAnonymous]
```

The returned `Future[DruidResponse]` will contain json data where `isAnonymouse` is either `true or false`. Please keep in mind that Druid is only able to handle strings, and recently also numerics. So Druid will be returning a string, and the conversion from a string to a boolean is done by the json parser.
Expand All @@ -56,24 +62,32 @@ The returned `Future[DruidResponse]` will contain json data where `isAnonymouse`
```scala
case class TimeseriesCount(count: Int)

val result = TimeSeriesQuery[TimeseriesCount](
val response = TimeSeriesQuery(
aggregations = List(
CountAggregation(name = "count")
),
granularity = "hour",
intervals = List("2011-06-01/2017-06-01")
).execute

val series: Map[ZonedDateTime, TimeseriesCount] = response.series[TimeseriesCount]
```

To get the timeseries data from this `Future[DruidRespones]` you can run `val series = result.series[List[TimeseriesCount]]`.
To get the timeseries data from this `Future[DruidRespones]` you can run `val series = result.series[TimeseriesCount]`.

## Configuration

The configuration is done by [Typesafe config](https://github.com/typesafehub/config). The configuration can be overriden by using environment variables, e.g. `DRUID_URL` and `DRUID_DATASOURCE`. Or by placing an application.conf in your own project and this will override the reference.conf of the scruid library.
The configuration is done by [Typesafe config](https://github.com/typesafehub/config). The configuration can be overriden by using environment variables, e.g. `DRUID_HOST`, `DRUID_PORT` and `DRUID_DATASOURCE`. Or by placing an application.conf in your own project and this will override the reference.conf of the scruid library.

```
druid = {
url = "http://localhost:8082/druid/v2/"
host = "localhost"
host = ${?DRUID_HOST}
port = 8082
port = ${?DRUID_PORT}
secure = false
secure = ${?DRUID_USE_SECURE_CONNECTION}
url = "/druid/v2/"
url = ${?DRUID_URL}
datasource = "wikiticker"
Expand Down

0 comments on commit 036ef47

Please sign in to comment.