Overview
Scenarios
In Spark applications, use StructuredStreaming to invoke Kafka interface to obtain word records. Collect the statistics of records for each word.
Data Planning
- Ensure that the cluster, including HDFS, Yarn, Spark, and Kafka is successfully installed.
- 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}
- 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 Compiling and Running the Application.
java -cp $SPARK_HOME/jars/*:$SPARK_HOME/jars/streamingClient010/*:{ClassPath} com.huawei.bigdata.spark.examples.KafkaWordCountProducer {BrokerList} {Topic} {messagesPerSec} {wordsPerMessage}
Development Approach
- Receive data from Kafka and generate DataStreamReader.
- Collect the statistics of word records.
- Calculate and print the result.
Packaging the Project
- Use the Maven tool provided by IDEA to pack the project and generate a JAR file. For details, see Compiling and Running the Application.
- Upload the JAR package to any directory (for example, /opt) on the server where the Spark client is located.
- Upload commons-pool2-xxx.jar to the $SPARK_HOME/jars/streamingClient010/ directory. The JAR package can be obtained from the $SPARK_HOME/tool/carbonPrequery directory.
Running Tasks
- When running the sample application, you need to specify <brokers>, <subscribe-type>, <topic>, and <checkpointDir>. <brokers> indicates the Kafka address for obtaining metadata (port 21007 is required), <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.
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.
- 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>
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.