sparkStreaming读取kafka数据实现wordcount
程序员文章站
2022-06-14 13:40:01
...
pom.xml如下
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_2.11</artifactId>
<version>${spark.version}</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-streaming_2.11</artifactId>
<version>${spark.version}</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-streaming-kafka-0-10_2.11</artifactId>
<version>2.1.1</version>
</dependency>
</dependencies>
package nj.zb
import java.util
import org.apache.kafka.clients.consumer.{ConsumerConfig, ConsumerRecord}
import org.apache.kafka.clients.producer.{KafkaProducer, ProducerConfig, ProducerRecord}
import org.apache.spark.SparkConf
import org.apache.spark.streaming.dstream.{DStream, InputDStream}
import org.apache.spark.streaming.kafka010.{ConsumerStrategies, KafkaUtils, LocationStrategies}
import org.apache.spark.streaming.{Seconds, StreamingContext}
/*
*将数据从kafkatopic A 取出数据,加工处理后输出到kafkatopic B
* */
object SparkStreamKafkaSourceToKafkaSinkWordCount {
def main(args: Array[String]): Unit = {
val conf: SparkConf = new SparkConf().setAppName("sparkKafkaStream").setMaster("local[*]")
val streamingContext = new StreamingContext(conf, Seconds(5))
streamingContext.checkpoint("checokpoint")
val kafkaParmas: Map[String, String] = Map(
(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.119.125:9092"),
(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,
"org.apache.kafka.common.serialization.StringDeserializer"),
(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG,
"org.apache.kafka.common.serialization.StringDeserializer"),
(ConsumerConfig.GROUP_ID_CONFIG, "kafkaGroup1")
)
val kafkaStream: InputDStream[ConsumerRecord[String, String]] = KafkaUtils.createDirectStream(
streamingContext,
LocationStrategies.PreferConsistent,
ConsumerStrategies.Subscribe(Seq("sparkKafkaDemo"), kafkaParmas)
)
//TODO
val wordCountStream: DStream[(String, Int)] =
kafkaStream.flatMap(v=>v.value().toString.split("\\s+"))
.map(x => (x, 1)).reduceByKey(_ + _)
wordCountStream.foreachRDD(
rdd=>{
rdd.foreachPartition(
x=>{
val props = new util.HashMap[String, Object]()
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.119.125:9092")
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer")
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer")
val producer = new KafkaProducer[String, String](props)
x.foreach(
y=>{
val word=y._1
val num=y._2
val record =
new ProducerRecord[String, String]("sparkKafkaDemoOUT", "", word+","+num)
producer.send(record)
}
)
}
)
}
)
streamingContext.start()
streamingContext.awaitTermination()
}
}
上一篇: kafka stream实现wordcount计数
下一篇: PHP笔考题2
推荐阅读
-
Android设备与外接U盘实现数据读取操作的示例
-
php+ajax 实现输入读取数据库显示匹配信息
-
[Spring cloud 一步步实现广告系统] 16. 增量索引实现以及投送数据到MQ(kafka)
-
Pytorch 实现数据集自定义读取
-
详解ASP.NET Core实现强类型Configuration读取配置数据
-
php excel reader读取excel内容存入数据库实现代码
-
C# Ado.net实现读取SQLServer数据库存储过程列表及参数信息示例
-
jsp读取数据库实现分页技术简析
-
Html5中localStorage存储JSON数据并读取JSON数据的实现方法
-
Android开发实现读取excel数据并保存为xml的方法