我正在尝试在Apache Flink上准备一个小样本应用程序,主要目的是演示如何使用Broadcast变量。此应用程序读取CSV文件并准备 DataSet [BuildingInformation]
case class BuildingInformation(
buildingID: Int, buildingManager: String, buildingAge: Int,
productID: String, country: String
)
这就是我现在正在创建 BuildingInformation DataSet的方法:
val buildingsBroadcastSet =
envDefault
.fromElements(
readBuildingInfo(
envDefault,
"./SensorFiles/building.csv")
)
然后,我开始转变:
val hvacStream = readHVACReadings(envDefault,"./SensorFiles/HVAC.csv")
hvacStream
.map(new HVACToBuildingMapper)
.withBroadcastSet(buildingsBroadcastSet,"buildingData")
.writeAsCsv("./hvacTemp.csv")
( buildingID - > BuildingInformation )的地图是我想要作为广播的参考数据。为了做好准备,我实现了一个RichMapFunction:
class HVACToBuildingMapper
extends RichMapFunction [HVACData,EnhancedHVACTempReading] {
var allBuildingDetails: Map[Int, BuildingInformation] = _
override def open(configuration: Configuration): Unit = {
allBuildingDetails =
getRuntimeContext
.getBroadcastVariableWithInitializer(
"buildingData",
new BroadcastVariableInitializer [BuildingInformation,Map[Int,BuildingInformation]] {
def initializeBroadcastVariable(valuesPushed:java.lang.Iterable[BuildingInformation]): Map[Int,BuildingInformation] = {
valuesPushed
.asScala
.toList
.map(nextBuilding => (nextBuilding.buildingID,nextBuilding))(breakOut)
}
}
)
}
override def map(nextReading: HVACData): EnhancedHVACTempReading = {
val buildingDetails = allBuildingDetails.getOrElse(nextReading.buildingID,UndefinedBuildingInformation)
// ... more intermediate data creation logic here
EnhancedHVACTempReading(
nextReading.buildingID,
rangeOfTempRecorded,
isExtremeTempRecorded,
buildingDetails.country,
buildingDetails.productID,
buildingDetails.buildingAge,
buildingDetails.buildingManager
)
}
}
在功能签名
中def initializeBroadcastVariable(valuesPushed:java.lang.Iterable[BuildingInformation]): Map[Int,BuildingInformation]
java.lang.Iterable 的资格是我的补充。如果没有这个,编译器就会在Intellij中抱怨。
在运行时,应用程序失败,我正在创建一个由Iterable [BuildingInformation]构建的地图,该地图由框架传递给 open()函数:
java.lang.Exception: The user defined 'open()' method caused an exception: scala.collection.immutable.$colon$colon cannot be cast to org.nirmalya.hortonworks.tutorial.BuildingInformation
at org.apache.flink.runtime.operators.BatchTask.run(BatchTask.java:475)
at org.apache.flink.runtime.operators.BatchTask.invoke(BatchTask.java:345)
at org.apache.flink.runtime.taskmanager.Task.run(Task.java:559)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassCastException: scala.collection.immutable.$colon$colon cannot be cast to org.nirmalya.hortonworks.tutorial.BuildingInformation
at org.nirmalya.hortonworks.tutorial.HVACReadingsAnalysis$HVACToBuildingMapper$$anon$7$$anonfun$initializeBroadcastVariable$1.apply(HVACReadingsAnalysis.scala:139)
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244)
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244)
at scala.collection.immutable.List.foreach(List.scala:318)
at scala.collection.TraversableLike$class.map(TraversableLike.scala:244)
at scala.collection.AbstractTraversable.map(Traversable.scala:105)
at org.nirmalya.hortonworks.tutorial.HVACReadingsAnalysis$HVACToBuildingMapper$$anon$7.initializeBroadcastVariable(HVACReadingsAnalysis.scala:139)
at org.nirmalya.hortonworks.tutorial.HVACReadingsAnalysis$HVACToBuildingMapper$$anon$7.initializeBroadcastVariable(HVACReadingsAnalysis.scala:133)
at org.apache.flink.runtime.broadcast.BroadcastVariableMaterialization.getVariable(BroadcastVariableMaterialization.java:234)
at org.apache.flink.runtime.operators.util.DistributedRuntimeUDFContext.getBroadcastVariableWithInitializer(DistributedRuntimeUDFContext.java:84)
at org.nirmalya.hortonworks.tutorial.HVACReadingsAnalysis$HVACToBuildingMapper.open(HVACReadingsAnalysis.scala:131)
at org.apache.flink.api.common.functions.util.FunctionUtils.openFunction(FunctionUtils.java:38)
at org.apache.flink.runtime.operators.BatchTask.run(BatchTask.java:471)
... 3 more
09:28:54,389 INFO org.apache.flink.runtime.client.JobClientActor - 04/29/2016 09:28:54 Job execution switched to status FAILED.
假设它可能是从(Java)Iterable转换案例类失败的特殊情况(虽然我不相信自己),但我尝试用其所有成员字段的Tuple5替换BuildingInformation。行为没有改变。
我可以通过提供 CanBuildFrom 来尝试,但我没有做到这一点。我的思绪拒绝了一个简单的案例类无法映射到另一个数据结构。有些不对劲,这对我来说并不明显。
为了完成帖子,我尝试使用与Scala 2.11.x和Scala 2.10.x相对应的Flink版本:行为是一样的。
此外,这里是EnhancedHVACTempReading(为了更好地理解代码):
case class EnhancedHVACTempReading(buildingID: Int, rangeOfTemp: String, extremeIndicator: Boolean,country: String, productID: String,buildingAge: Int, buildingManager: String)
我有一种预感,JVM的紊乱与Java的Iterable被用作Scala的List有关,但是,当然,我当然不确定。
有人可以帮我发现错误吗?
答案 0 :(得分:1)
问题是您必须在map
中的readBuildingInfo
函数中返回一些内容。此外,如果您提供fromElements
,则不应使用List[BuildingInformation]
,而是在要展平列表时使用fromCollection
。以下代码段显示了必要的更改。
def main(args: Array[String]): Unit = {
val envDefault = ExecutionEnvironment.getExecutionEnvironment
val buildingsBroadcastSet = readBuildingInfo(envDefault,"./SensorFiles/building.csv")
val hvacStream = readHVACReadings(envDefault,"./SensorFiles/HVAC.csv")
hvacStream
.map(new HVACToBuildingMapper)
.withBroadcastSet(buildingsBroadcastSet,"buildingData")
.writeAsCsv("./hvacTemp.csv")
envDefault.execute("HVAC Simulation")
}
和
private def readBuildingInfo(env: ExecutionEnvironment, inputPath: String): DataSet[BuildingInformation] = {
val input = Source.fromFile(inputPath).getLines.drop(1).map(datum => {
val fields = datum.split(",")
BuildingInformation(
fields(0).toInt, // buildingID
fields(1), // buildingManager
fields(2).toInt, // buildingAge
fields(3), // productID
fields(4) // Country
)
})
env.fromCollection(input.toList)
}