Skip to main content

Create and run Scala and Java JARs on serverless compute

Beta

Serverless Scala and Java jobs are in Beta. You can use JAR tasks to deploy your JAR. See Manage Databricks previews if it's not already enabled.

A Java archive (JAR) packages Java or Scala code into a single file. This article shows you how to create a JAR with Spark code and deploy it as a Lakeflow Job on serverless compute.

tip

For automated deployment and continuous integration workflows, use Databricks Asset Bundles to create a project from a template with pre-configured build and deployment settings. See Build a Scala JAR using Databricks Asset Bundles and Bundle that uploads a JAR file to Unity Catalog. This article describes the manual approach for deployments or learning how JARs work with serverless compute.

Requirements

Your local development environment must have the following:

  • sbt 1.11.7 or higher (for Scala JARs)
  • Maven 3.9.0 or higher (for Java JARs)
  • JDK, Scala, and Databricks Connect versions that match your serverless environment (this example uses JDK 17, Scala 2.13.16, and Databricks Connect 17.0.1)

Step 1. Build a JAR

  1. Run the following command to create a new Scala project:

    Bash
    sbt new scala/scala-seed.g8

    When prompted, enter a project name, for example, my-spark-app.

  2. Replace the contents of your build.sbt file with the following:

    scalaVersion := "2.13.16"
    libraryDependencies += "com.databricks" %% "databricks-connect" % "17.0.1"
    // other dependencies go here...

    // to run with new jvm options, a fork is required otherwise it uses same options as sbt process
    fork := true
    javaOptions += "--add-opens=java.base/java.nio=ALL-UNNAMED"
  3. Edit or create a project/assembly.sbt file, and add this line:

    addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "2.3.1")
  4. Create your main class in src/main/scala/example/DatabricksExample.scala:

    Scala
    package com.examples

    import org.apache.spark.sql.SparkSession

    object SparkJar {
    def main(args: Array[String]): Unit = {
    val spark = SparkSession.builder().getOrCreate()

    // Prints the arguments to the class, which
    // are job parameters when run as a job:
    println(args.mkString(", "))

    // Shows using spark:
    println(spark.version)
    println(spark.range(10).limit(3).collect().mkString(" "))
    }
    }
  5. To build your JAR file, run the following command:

    Bash
    sbt assembly

Step 2. Create a job to run the JAR

  1. In your workspace, click Workflows icon. Jobs & Pipelines in the sidebar.

  2. Click Create, then Job.

    The Tasks tab displays with the empty task pane.

    note

    If the Lakeflow Jobs UI is ON, click the JAR tile to configure the first task. If the JAR tile is not available, click Add another task type and search for JAR.

  3. Optionally, replace the name of the job, which defaults to New Job <date-time>, with your job name.

  4. In Task name, enter a name for the task, for example JAR_example.

  5. If necessary, select JAR from the Type drop-down menu.

  6. For Main class, enter the package and class of your Jar. If you followed the example above, enter com.examples.SparkJar.

  7. For Compute, select Serverless.

  8. Configure the serverless environment:

    1. Choose an environment, then click Pencil icon. Edit to configure it.
    2. Select 4-scala-preview for the Environment version.
    3. Add your JAR file by dragging and dropping it into the file selector, or browse to select it from a Unity Catalog volume or workspace location.
  9. For Parameters, for this example, enter ["Hello", "World!"].

  10. Click Create task.

Step 3: Run the job and view the job run details

Click Run Now Button to run the workflow. To view details for the run, click View run in the Triggered run pop-up or click the link in the Start time column for the run in the job runs view.

When the run completes, the output displays in the Output panel, including the arguments passed to the task.

Next steps