Set Up Example Project and Configure Build

Full sources for all Cloudflow example applications can be found in the examples folder of the cloudflow project on Github. The sources for the example described below can be found in the application called sensor-data-scala.

A typical Cloudflow application uses the organization shown below. We will implement the example in Scala.

  1. In a convenient location, such as my-cloudflow-example create the following directory structure:

       |-project
       |--cloudflow-plugins.sbt
       |-src
       |---main
       |-----avro
       |-----blueprint
       |-----resources
       |-----scala
       |-------sensordata
       |-build.sbt

    As we move through the process, the leaf level directories of the above tree will contain the following:

    • project/cloudflow-plugins.sbt : contains the Cloudflow sbt plugin name and version.

    • avro : the avro schema of the domain objects

    • blueprint : the blueprint of the application in a file named blueprint.conf

    • scala : the source code of the application under the package name sensordata

    • build.sbt : the sbt build script

  2. From the top-level directory, initialize git (after adding files you will commit them):

    git init
    The Cloudflow sbt plugin assumes that your project is version-managed using git. It will use git commit information to generate dynamic versioning for the Docker images it produces. If the project doesn’t have an initialized git index with at least one commit, some commands will fail with a not a git repository error.

The sbt build script

Cloudflow provides sbt plugins for the supported runtimes, Akka, Spark and Flink. The plugins speed up development by adding the necessary dependencies and abstracting much of the boilerplate necessary to build a complete application. You can use multiple runtimes in the same application, provided that each runtime is defined in its own sub-project.

In this example, we use the CloudflowAkkaPlugin that provides the building blocks for developing a Cloudflow application with Akka Streams. In addition to the backend-specific plugin, we need to add the CloudflowApplicationPlugin that provides the image-building and local-running capabilities to the project.

  1. Create a build.sbt file with the following contents and save it in at the same level as your src directory:

    import sbt._
    import sbt.Keys._
    
    //tag::local-conf[]
    lazy val sensorData =  (project in file("."))
        .enablePlugins(CloudflowApplicationPlugin, CloudflowAkkaPlugin, ScalafmtPlugin)
        .settings(
          scalaVersion := "2.12.11",
          runLocalConfigFile := Some("src/main/resources/local.conf"), (1)
          runLocalLog4jConfigFile := Some("src/main/resources/log4j.xml"), (2)
          scalafmtOnCompile := true,
          name := "sensor-data-scala",
    //end::local-conf[]
    
          libraryDependencies ++= Seq(
            "com.lightbend.akka"     %% "akka-stream-alpakka-file"  % "1.1.2",
            "com.typesafe.akka"      %% "akka-http-spray-json"      % "10.1.12",
            "ch.qos.logback"         %  "logback-classic"           % "1.2.3",
            "com.typesafe.akka"      %% "akka-http-testkit"         % "10.1.12" % "test",
            "org.scalatest"          %% "scalatest"                 % "3.0.8"  % "test"
          ),
          organization := "com.lightbend.cloudflow",
          headerLicense := Some(HeaderLicense.ALv2("(C) 2016-2020", "Lightbend Inc. <https://www.lightbend.com>")),
    
          crossScalaVersions := Vector(scalaVersion.value),
          scalacOptions ++= Seq(
            "-encoding", "UTF-8",
            "-target:jvm-1.8",
            "-Xlog-reflective-calls",
            "-Xlint",
            "-Ywarn-unused",
            "-Ywarn-unused-import",
            "-deprecation",
            "-feature",
            "-language:_",
            "-unchecked"
          ),
    
    
          scalacOptions in (Compile, console) --= Seq("-Ywarn-unused", "-Ywarn-unused-import"),
          scalacOptions in (Test, console) := (scalacOptions in (Compile, console)).value
    
        )

    The script is a standard Scala sbt build file—​with the addition of the Cloudflow plugin for Akka Streams, the Cloudflow application plugin, and also the Scalafmt plugin that we suggest to keep the style consistent in the application (optional).

  2. Create the file project/cloudflow-plugins.sbt and add the Cloudflow plugin dependency in it:

    addSbtPlugin("com.lightbend.cloudflow" % "sbt-cloudflow" % "2.1.0-SNAPSHOT")

What’s next

Now, let’s define the Avro schema.