1

我在使用 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]
)  
4

1 回答 1

0

你检查了吗:https ://ci.apache.org/projects/flink/flink-docs-release-1.0/apis/batch/hadoop_compatibility.html#using-hadoop-outputformats

它包含以下示例:

// Obtain your result to emit.
val hadoopResult: DataSet[(Text, IntWritable)] = [...]

val hadoopOF = new HadoopOutputFormat[Text,IntWritable](
  new TextOutputFormat[Text, IntWritable],
  new JobConf)

hadoopOF.getJobConf.set("mapred.textoutputformat.separator", " ")
FileOutputFormat.setOutputPath(hadoopOF.getJobConf, new Path(resultPath))

hadoopResult.output(hadoopOF)
于 2016-06-02T11:29:42.510 回答