xuwei517 / flinkexample Goto Github PK
View Code? Open in Web Editor NEWFlink代码实例
Flink代码实例
老师您好:
我在使用flink读取kafka数据后,使用watermarks进行乱序处理时,IDEA总是提示assignTimestampsAndWatermarks()有多个重载,代码如下:
object Test {
def main(args: Array[String]): Unit = {
// 创建流处理执行环境
val env = StreamExecutionEnvironment.getExecutionEnvironment
env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime)
// 配置 Kafka 消费者
val properties = System.getProperties
properties.setProperty("bootstrap.servers", "localhost:9092")
properties.setProperty("group.id", "test")
// 创建 Kafka 数据源
val kafkaSource = new FlinkKafkaConsumer[String]("test_topic", new SimpleStringSchema(), properties)
// 添加 Watermark 生成器
val watermarkGenerator = new BoundedOutOfOrdernessTimestampExtractor[String](Time.seconds(10)) {
override def extractTimestamp(element: String): Long = {
// 解析消息中的时间戳,这里假设消息格式为 "message,timestamp"
val parts = element.split(",")
parts(1).toLong
}
}
// 将 FlinkKafkaConsumer 转换为 DataStream
val stream = env.addSource(kafkaSource)
.map(new MapFunction[String, (String, Long)] {
override def map(value: String): (String, Long) = {
val parts = value.split(",")
(parts(0), parts(1).toLong)
}
})
.assignTimestampsAndWatermarks(watermarkGenerator)
// 对数据进行处理
stream.print()
// 启动 Flink 作业
env.execute("Flink Kafka Example")
}
}
使用的maven配置如下:
org.apache.flink
flink-scala_2.11
1.13.2
org.apache.flink
flink-streaming-scala_2.11
1.13.2
org.apache.flink
flink-connector-kafka_2.11
1.13.2
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.