我经常发布事件,每个事件都包含有关Car的一些信息。我需要处理此类事件,但要排除具有特定城市和车牌号码组合的事件。有关这些列入黑名单的城市和车牌号组合的信息来自S3文件,该文件每天更新一次。
示例:汽车事件如下:
[
{
"name": "Car1",
"plate": "XYZ123",
"city": "Berlin"
},
{
"name": "Car2",
"plate": "XYZ1234",
"city": "Amsterdam"
},
{
"name": "Car3",
"plate": "ASD 123",
"city": "Kuala Lumpur"
},
{
"name": "Car1",
"plate": "XYZ123",
"city": "Moscow"
},
{
"name": "Car1",
"plate": "XYZ123",
"city": "Barcelona"
}
]
S3文件如下:例如。可以说它叫做excludedCars
[
{
"plate": "XYZ123",
"city": "Berlin"
},
{
"plate": "ABC1231",
"city": "Berlin"
},
{
"plate": "AWS121",
"city": "Berlin"
},
{
"plate": "XYZ1234",
"city": "Amsterdam"
},
{
"plate": "AMC3421",
"city": "Amsterdam"
},
{
"plate": "ASD 123",
"city": "Kuala Lumpur"
},
{
"plate": "XYZ123",
"city": "Moscow"
},
{
"plate": "XYZ123",
"city": "Barcelona"
}
]
方法:
excludedCars
作为流式传输源。{
"Berlin": ["XYZ123", "ABC1231", "AWS121"],
"Amsterdam": ["XYZ1234", "AMC3421"],
"Kuala Lumpur":["ASD 123"],
"Moscow":["XYZ123"],
"Barcelona":["XYZ123"]
}
代码:
object Cars {
def main(args: Array[String]): Unit = {
val env = StreamExecutionEnvironment.getExecutionEnvironment
val excludedCarStream: DataStream[Array[ExcludedCarDetail]] = getExcludedCarsStream(env)
val excludedCarDetails = excludedCarStream.flatMap(item => item) // Array of Excluded Car objects
excludedCarDetails.map(car => (car.cityId, car.plateNumber)).keyBy(0) // As per my understanding, this should result into a map of city to array of plate number maps
excludedCarDetails.print() // This just prints the simple tuples without any grouping by city
env.execute("Scala SocketTextStreamWordCount Example")
}
private def getExcludedCarsStream(env: StreamExecutionEnvironment): DataStream[Array[ExcludedCarDetail]] = {
val path: String = "file:///Users/name/flinkTest/excluded"
val textInputFormat = new TextInputFormat(new Path(path))
env
.readFile(
textInputFormat,
path,
FileProcessingMode.PROCESS_CONTINUOUSLY,
1000
)
.map(jsonString => {
val excludedCars: Array[ExcludedCarDetail] = (new Gson).fromJson(jsonString, classOf[Array[ExcludedCarDetail]])
excludedCars
})
}
}
case class ExcludedCarDetail(
@(SerializedName @scala.annotation.meta.field)("city") cityId: String,
@(SerializedName @scala.annotation.meta.field)("plate") plateNumber: String
)
据我了解,excludedCarDetails.map(car => (car.cityId, car.plateNumber)).keyBy(0)
应该会生成city to array of plate numbers
的地图,可以将其广播到我的主流(汽车)中。相反,它只是简单地打印(city, plateNumber)
的元组。
我对Flink绝对是新鲜的,并试图掌握和实现概念。请提出我做错了什么以及如何实现所需的行为。
约束:广播地图的结构无法更改。
广播状态解决方案:
object Cars {
def main(args: Array[String]): Unit = {
val env = StreamExecutionEnvironment.getExecutionEnvironment
val excludedCarsState: MapStateDescriptor[Int, List[String]] = new MapStateDescriptor("excludedCars", classOf[Int], classOf[List[String]])
val excludedCarDetails: DataStream[ExcludedCarDetail] = getExcludedCarsStream(env)
val excludedCarBroadcast: BroadcastStream[ExcludedCarDetail] = excludedCarDetails.broadcast(excludedCarsState)
val carsStream: DataStream[CarDetail] = getMainCarsStream(env)
val bs = carsStream
.keyBy(_.cityId)
.connect(excludedCarBroadcast)
.process(new CarsStateLogic(excludedCarsState))
bs.print()
env.execute("Scala SocketTextStreamWordCount Example")
}
private def getExcludedCarsStream(env: StreamExecutionEnvironment): DataStream[ExcludedCarDetail] = {
val cars: ListBuffer[ExcludedCarDetail] = ListBuffer()
for(i <- 0 until 3) {
val cityId = i+1
val plateNumber = "Plate"+(i+1)
cars += ExcludedCarDetail(cityId, plateNumber) // Basically exclude cars with plate1 in city1, plate2 in city2, plate3 in city3
}
env.fromCollection(cars.toList)
}
private def getMainCarsStream(env: StreamExecutionEnvironment): DataStream[CarDetail] = {
val cars: ListBuffer[CarDetail] = ListBuffer()
for(i <- 0 until 10) {
val cityId = i+1
val plateNumber = "Plate"+(i+1)
val name = "Name"+(i+1)
cars += CarDetail(cityId, plateNumber, name)
}
env.fromCollection(cars.toList)
}
}
case class ExcludedCarDetail(cityId: Int, plateNumber: String)
case class CarDetail(cityId: Int, plateNumber: String, name: String)
class CarsStateLogic(excludedCarsState: MapStateDescriptor[Int, List[String]]) extends KeyedBroadcastProcessFunction[String, CarDetail, ExcludedCarDetail, CarDetail] {
override def processElement(car: CarDetail, ctx: KeyedBroadcastProcessFunction[String, CarDetail, ExcludedCarDetail, CarDetail]#ReadOnlyContext, out: Collector[CarDetail]): Unit = {
val state = ctx.getBroadcastState(excludedCarsState)
if(state.contains(car.cityId)) {
val cityState = state.get(car.cityId)
if(cityState.indexOf(car.plateNumber) < 0) { // not excluded
out.collect(car)
}
} else {
out.collect(car)
}
}
override def processBroadcastElement(value: ExcludedCarDetail, ctx: KeyedBroadcastProcessFunction[String, CarDetail, ExcludedCarDetail, CarDetail]#Context, out: Collector[CarDetail]): Unit = {
val state = ctx.getBroadcastState(excludedCarsState)
val newStateForKey = if(state.contains(value.cityId)) {
value.plateNumber :: state.get(value.cityId)
} else {
List(value.plateNumber)
}
ctx.getBroadcastState(excludedCarsState).put(value.cityId, newStateForKey)
println("BroadCast element: CityId:"+ value.cityId+ ", State: "+state.get(value.cityId))
}
}
但是我现在遇到了冷启动问题。在处理主要数据之前,确保广播状态可用的可靠方法是什么。
答案 0 :(得分:1)
如果排除的汽车数据集很小,则可以按原样广播(不按城市分组)。如果很大,则按城市(与汽车流相同)进行键入,并将这两个流连接起来,以便每个子任务仅获得所有排除的汽车和常规汽车数据的分区集。
请注意,您遇到了冷启动问题,您想在处理任何常规汽车数据之前先处理所有当前排除的汽车数据,以免从正在处理的汽车数据中得到误报在收到排除的汽车数据之前。