Apache Flink:转换广播变量失败,但我无法确定原因



我正试图在ApacheFlink上准备一个小样本应用程序,主要目的是演示如何使用广播变量。此应用程序读取CSV文件并准备DataSet[BuildingInformation]

case class BuildingInformation(
     buildingID: Int, buildingManager: String, buildingAge: Int,
     productID: String, country: String
)

这就是我目前创建BuildingInformation数据集的方式:

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转换case类失败的特殊情况(尽管我自己并不相信),我尝试用它所有成员字段的Tuple5替换BuildingInformation。这种行为没有改变。

我本可以尝试提供CanBuildFrom,但我没有做到。我的想法是,一个简单的case类无法映射到另一个数据结构。有什么不对的地方,这对我来说并不明显。

为了完成这篇文章,我尝试了对应Scala2.11.x和Scala2.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有关,但我当然不确定。

有人能帮我找出错误吗?

问题是必须在readBuildingInfomap函数中返回一些内容。此外,如果您提供了List[BuildingInformation],则不应该使用fromElements,而如果您想使列表变平,则应使用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)
}

相关内容

  • 没有找到相关文章

最新更新