Building and running a Spark Streaming application

Use the following steps to build and run a Spark streaming job for Cloudera Data Platform (CDP).

Depending on your compilation and build processes, one or more of the following tasks might be required before running a Spark Streaming job:

  • If you are using maven as a compile tool:

    1. Add the Cloudera repository to your pom.xml file:
          <name>Cloudera Repository</name>
    2. Specify the Cloudera version number for Spark streaming Kafka and streaming dependencies to your pom.xml file:

      Note that the correct version number includes the Spark version and the Cloudera Runtime version.

    3. (Optional) If you prefer to pack an uber .jar rather than use the default ("provided"), add the maven-shade-plugin to your pom.xml file:
  • Instructions for submitting your job depend on whether you used an uber .jar file or not:

    • If you kept the default .jar scope and you can access an external network, use --packages to download dependencies in the runtime library:

      spark-submit --master yarn-client \
          --num-executors 1 \
          --packages org.apache.spark:spark-streaming-kafka_2.10: \
          --repositories \
          --class <user-main-class> \
          <user-application.jar> \
          <user arg lists>

      The artifact and repository locations should be the same as specified in your pom.xml file.

    • If you packed the .jar file into an uber .jar, submit the .jar file in the same way as you would a regular Spark application:

      spark-submit --master yarn-client \
          --num-executors 1 \
          --class <user-main-class> \
          <user-uber-application.jar> \
          <user arg lists>
  1. Select or create a user account to be used as principal.

    This should not be the kafka or spark service account.

  2. Generate a keytab for the user.
  3. Create a Java Authentication and Authorization Service (JAAS) login configuration file: for example, key.conf.
  4. Add configuration settings that specify the user keytab.

    The keytab and configuration files are distributed using YARN local resources. Because they reside in the current directory of the Spark YARN container, you should specify the location as ./v.keytab.

    The following example specifies keytab location ./v.keytab for principal

    KafkaClient { required
  5. In your spark-submit command, pass the JAAS configuration file and keytab as local resource files, using the --files option, and specify the JAAS configuration file options to the JVM options specified for the driver and executor:
    spark-submit \
        --files key.conf#key.conf,v.keytab#v.keytab \
        --driver-java-options "" \
        --conf "" \
  6. Pass any relevant Kafka security options to your streaming application.

    For example, the KafkaWordCount example accepts PLAINTEXTSASL as the last option in the command line:

    KafkaWordCount /vagrant/spark-examples.jar c6402:2181 abc ts 1 PLAINTEXTSASL