idea使用Scala编写第一个Kafka+Flink问题总结

1.需要添加flink-streaming-scala_2.11的pom文件
flink_scala
2.如果报隐式转换失败:Error:(20, 31) could not find implicit value for evidence parameter of type org.apache.flink.api.common.typeinfo.TypeInformation[String]
在代码中引入一下包:
import org.apache.flink.streaming.api.scala._
如果数据是有限的(静态数据集),我们可以引入以下包:
import org.apache.flink.api.scala._
3.完整代码
import java.util.Properties
import org.apache.flink.streaming.api.scala._
import org.apache.flink.api.common.serialization.SimpleStringSchema
import org.apache.flink.streaming.api.scala.StreamExecutionEnvironment
import org.apache.flink.streaming.api.windowing.time.Time
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer

object KafkaWindowWordCount {
def main(args: Array[String]): Unit = {
// get the execution environment
val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment
val properties= new Properties()
properties.setProperty(“bootstrap.servers”, “localhost:9092”)
properties.setProperty(“group.id”, “flink_consumer”)

val consumer =  new FlinkKafkaConsumer[String]("demo", new SimpleStringSchema, properties)
val stream = env.addSource(consumer)
val word=stream.flatMap{string=>string.split(" ")}
    .map{word=>WordWithCount(word,1)}
    .keyBy("word")
    .timeWindow(Time.seconds(5), Time.seconds(1))
   .sum("count")
word.print().setParallelism(1)

env.execute("Kafka Window WordCount")

}
// Data type for words with count
case class WordWithCount(word: String, count: Long)
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值