Updated on 2024-10-23 GMT+08:00

Development Plan

Scenarios

In Spark applications, use StructuredStreaming to invoke Kafka interface to obtain word records. Collect the statistics of records for each word.

Data Planning

Sample project data of StructuredStreaming is stored in Kafka. A user with Kafka permission sends data to Kafka.
  1. Ensure that the cluster, including HDFS, Yarn, Spark, and Kafka is successfully installed.
  2. Create a topic.

    zkQuorum} indicates ZooKeeper cluster information. The format is IP:port.

    $KAFKA_HOME/bin/kafka-topics.sh --create --zookeeper {zkQuorum}/kafka --replication-factor 1 --partitions 1 --topic {Topic}

  3. Start the Producer of Kafka and send data to Kafka.

    {ClassPath} indicates the storage path of engineer JAR packages and is specified by the user. For details, see Writing and Running the Spark Program in the Linux Environment.

    java -cp $SPARK_HOME/jars/*:$SPARK_HOME/jars/streamingClient010/*:{ClassPath} com.huawei.bigdata.spark.examples.KafkaWordCountProducer {BrokerList} {Topic} {messagesPerSec} {wordsPerMessage}

Development Approach

  1. Receive data from Kafka and generate DataStreamReader.
  2. Collect the statistics of word records.
  3. Calculate and print the result.

Packaging the Project

Running Tasks

When running the sample project, you need to specify <brokers>, <subscribe-type>, <topic>, and <checkpointDir>.
  • <brokers> indicates the Kafka address for obtaining metadata.
  • <subscribe-type> indicates the Kafka subscription type (for example, subscribe),
  • and <topic> indicates the name of the topic read from Kafka.
  • <checkpointDir> indicates the path for storing the checkpoint file, which can be a local path or an HDFS path.

The path of the Spark Structured Streaming Kafka dependency package on the client is different from that of other dependency packages. For example, the path of other dependency packages is $SPARK_HOME/jars. Whereas the path of the Spark Structured Streaming Kafka dependency package is $SPARK_HOME/jars/streamingClient010. Therefore, when running an application, you need to add a configuration item to the spark-submit command to specify the path of the dependency package of Spark Streaming Kafka, for example, --jars $(files=($SPARK_HOME/jars/streamingClient010/*.jar); IFS=,; echo "${files[*]}")

When submitting a structured stream task, you need to run the --jars command to specify the path of the Kafka-related JAR file. For the current version, you need to cope the kafka-clientsjar file from the $SPARK_HOME/jars/streamingClient010 directory to the $SPARK_HOME/jars directory. Otherwise, the "class not found" error is reported.

Go to the Spark client directory and run the following commands to invoke the bin/spark-submit script to run the code (The class name and file name must be the same as those in the actual code. The following is only an example).
  • Run Java or Scala sample code:

    bin/spark-submit --master yarn --deploy-mode client --jars $(files=($SPARK_HOME/jars/streamingClient010/*.jar); IFS=,; echo "${files[*]}") --class com.huawei.bigdata.spark.examples.KafkaWordCount /opt/SparkStructuredStreamingScalaExample-1.0.jar <brokers> <subscribe-type> <topic> <checkpointDir>

    The configuration example is as follows:

    If an error indicating that the user does not have the permission to read and write the local directory is reported, the spark.sql.streaming.checkpointLocation parameter must be specified, and the user must have the read and write permissions on the directory specified by this parameter.
  • Run the Python sample code:

    When running the Python sample code, you need to add the JAR package of the Java project to the streamingClient/ directory.

    bin/spark-submit --master yarn --deploy-mode client --jars $(files=($SPARK_HOME/jars/streamingClient010/*.jar); IFS=,; echo "${files[*]}") /opt/female/SparkStructuredStreamingPythonExample/KafkaWordCount.py <brokers> <subscribe-type> <topic> <checkpointDir>