Skip to content
Roy Brondgeest edited this page Nov 11, 2020 · 9 revisions

Clickhouse Scala DSL

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 Q/A & security specialist have done an audit of this library.

DSL

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"

The current documentation is quite slim around the dsl. After the quick setup the most meaningful way to understand it would be to read the tests

Roadmap

  • More documentation
  • Insert statement support

Any help is greatly appreciated

Todo pages:

Clone this wiki locally