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

Development Plan

Overview

The project uses Structured Streaming in Spark applications to call Kafka APIs to obtain word records. Word records are classified to obtain the number of records of each word.

Preparing Data

Use a user with Kafka permission to send data to Kafka. (Structured Streaming sample project data is stored in the Kafka component.)
  1. Ensure that the cluster is installed with all the required components, namely HDFS, YARN, Spark, and Kafka.
  2. Create a topic.

    {zkQuorum} indicates ZooKeeper cluster information in the IP address:Port number format.

    $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 the project JAR package that is specified by the user. For details, see Commissioning a Spark Application in a Linux Environment.

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

Development Guidelines

  1. Receive data from Kafka and generate the corresponding DataStreamReader.
  2. Classify word records.
  3. Calculate and print the result.

Packaging the Project

Running the Task

  • 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),
  • <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 streamingClient010/ 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>