一、添加依赖
ThisBuild / version := "0.1.0-SNAPSHOT"ThisBuild / scalaVersion := "2.12.12"
libraryDependencies ++= Seq( "org.apache.spark" % "spark-core_2.12" % "3.0.0")
libraryDependencies ++= Seq( "org.apache.spark" % "spark-sql_2.12" % "3.0.0")
libraryDependencies ++= Seq( "org.apache.spark" % "spark-streaming_2.12" % "3.0.0")
libraryDependencies ++= Seq( "org.apache.spark" % "spark-streaming-kafka-0-10_2.12" % "3.0.0")
libraryDependencies ++= Seq( "com.fasterxml.jackson.core" % "jackson-core" % "2.10.1")libraryDependencies ++= Seq( "mysql" % "mysql-connector-java" % "5.1.30")lazy val root = (project in file(".")).settings(name := "scala-proj")
二、demo程序
package example3import org.apache.kafka.clients.consumer.{ConsumerConfig, ConsumerRecord}
import org.apache.spark.SparkConf
import org.apache.spark.rdd.RDD
import org.apache.spark.storage.StorageLevel
import org.apache.spark.streaming.dstream.{DStream, InputDStream, ReceiverInputDStream}
import org.apache.spark.streaming.kafka010.{ConsumerStrategies, KafkaUtils, LocationStrategies}
import org.apache.spark.streaming.receiver.Receiver
import org.apache.spark.streaming.{Duration, Seconds, StreamingContext}import scala.collection.mutable
import scala.util.Randomobject HelloStreaming04 {def main(args: Array[String]): Unit = {val sparkConf = new SparkConf().setMaster("local[*]").setAppName("KafkaConsumer")val ssc = new StreamingContext(sparkConf, Seconds(5))val kafkaPara: Map[String, Object] = Map[String, Object](ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG -> "xx.xx.xx.xx:9092", ConsumerConfig.GROUP_ID_CONFIG -> "myGroup","key.deserializer" -> "org.apache.kafka.common.serialization.StringDeserializer","value.deserializer" -> "org.apache.kafka.common.serialization.StringDeserializer")val kafkaDataDs: InputDStream[ConsumerRecord[String, String]] = KafkaUtils.createDirectStream[String, String](ssc,LocationStrategies.PreferConsistent,ConsumerStrategies.Subscribe[String, String](Set("myTopic"), kafkaPara))kafkaDataDs.map(_.value()).print()ssc.start();ssc.awaitTermination();}}