我在使用 Apache Flink 编写程序时遇到了困难。问题是我试图生成Hadoop 的 MapFile作为计算的结果,但 Scala 编译器抱怨类型不匹配。
为了说明这个问题,让我向您展示下面的代码片段,它试图生成两种输出:一种是Hadoop 的 SequenceFile,另一种是 MapFile。
val dataSet: DataSet[(IntWritable, BytesWritable)] =
env.readSequenceFile(classOf[Text], classOf[BytesWritable], inputSequenceFile.toString)
.map(mapper(_))
.partitionCustom(partitioner, 0)
.sortPartition(0, Order.ASCENDING)
val seqOF = new HadoopOutputFormat(
new SequenceFileOutputFormat[IntWritable, BytesWritable](), Job.getInstance(hadoopConf)
)
val mapfileOF = new HadoopOutputFormat(
new MapFileOutputFormat(), Job.getInstance(hadoopConf)
)
val dataSink1 = dataSet.output(seqOF) // it typechecks!
val dataSink2 = dataSet.output(mapfileOF) // syntax error
如上所述,dataSet.output(mapfileOF) 导致 Scala 编译器抱怨如下:
仅供参考,与 SequenceFile 相比,MapFile 需要一个更强的条件,即键必须是 WritableComparable。
在使用 Flink 编写应用程序之前,我使用 Spark 实现了它,如下所示,它运行良好(没有编译错误,运行良好,没有任何错误)。
val rdd = sc
.sequenceFile(inputSequenceFile.toString, classOf[Text], classOf[BytesWritable])
.map(mapper(_))
.repartitionAndSortWithinPartitions(partitioner)
rdd.saveAsNewAPIHadoopFile(
outputPath.toString,
classOf[IntWritable],
classOf[BytesWritable],
classOf[MapFileOutputFormat]
)