Skip to content
Roy Brondgeest edited this page Oct 29, 2018 · 9 revisions

Clickhouse Scala DSL

** ONLY AVAILABLE FOR SCALA 2.12 **

Disclaimer

Please note: The code inside this project has not been benchmarked, tested in extreme circumstances and has not been tested by any security specialist. Use at your own discretion. Therefor, even when we are using this software in our products in production, we advise not to use this tool in production yourself, until your security specialist has done an audit of this source code.

DSL

(The DSL is still a bit rough. Imports, type paths and package names need to be sanitized. Feel free to submit a PR)

Introduction

The clickhouse-dsl of this project offers a full-featured DSL that allows to compose most Clickhouse statements. During development not much attention has been going into INSERT statements, but SELECT statements are fully supported, including complex joins, aggregating queries, merging functions etc. as well as methods to build up your database schema from the Table definitions that should be provided to use this DSL.

QuickSetup

1. Set up your dependencies

In build.sbt add the following:

    "com.crobox.clickhouse" %% "client"    % ClickhouseClientVersion,
    "com.crobox.clickhouse" %% "dsl"       % ClickhouseClientVersion, 

2. Define table schemas

for the tables that you wish to use in the DSL

package crobox.reporting.core.schema

import java.util.UUID

import com.crobox.clickhouse.dsl._
import com.crobox.clickhouse.dsl.schemabuilder._

object Actions {
  object ActionsTable extends Table  {
    override val name: String = "actions"
    val actionId              = NativeColumn[UUID]("action_id")
    val actionType            = NativeColumn[String]("action_type")
    val subActions            = NativeColumn[Seq[String]]("child_ids", ColumnType.Array(ColumnType.String))
    val valid                 = NativeColumn[Boolean]("promoted", ColumnType.Boolean)
    override val columns: List[NativeColumn[_]] =  List(actionId,
                                                        actionType,
                                                        subActions,
                                                        valid)
  }
}
  1. Use the DSL
import com.crobox.clickhouse.dsl._

implicit val tokenizerDatabase: TokenizerModule.Database = "default"

val clickCountQry: OperationalQuery = select(uniq(actionId)) from ActionsTable where(actionType isEq "click")

val parsedClickCountQry: String = clickhouseTokenizer.toSql(query.internalQuery)
//val parsedClickCountQry = "SELECT uniq(action_id) FROM default.actions WHERE action_type = \"click\" FORMAT JSON"

Roadmap

  • Cleanup of type paths, package names, import scopes
  • More documentation
  • Insert statement support

Any help is greatly appreciated

Todo pages:

Clone this wiki locally