Проверка типов взаимодействия в Scala Java - PullRequest
0 голосов
/ 19 июня 2011

Я не могу исправить эти ошибки проверки типов в Scala:

package junk

import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.io.IntWritable
import org.apache.hadoop.io.Text
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat
import org.apache.hadoop.mapreduce.Job

object Sample {
  val conf: Configuration = new Configuration()
  val job: Job = new Job(conf, "sample Hadoop MapReduce program");

  job.setInputFormatClass(classOf[TextInputFormat])
  job.setOutputFormatClass(classOf[TextOutputFormat])
  job.setOutputFormatClass(classOf[TextOutputFormat[Text, IntWritable]])
}    

Сообщения об ошибках:

Sample.scala:14: error: type mismatch;
found   : java.lang.Class[org.apache.hadoop.mapreduce.lib.input.TextInputFormat](classOf[org.apache.hadoop.mapreduce.lib.input.TextInputFormat])
required: java.lang.Class[_ <: org.apache.hadoop.mapreduce.InputFormat]
 job.setInputFormatClass(classOf[TextInputFormat])
                                ^
Sample.scala:15: error: class TextOutputFormat takes type parameters
 job.setOutputFormatClass(classOf[TextOutputFormat])
                                  ^
Sample.scala:16: error: type mismatch;
found   : java.lang.Class[org.apache.hadoop.mapreduce.lib.output.TextOutputFormat[org.apache.hadoop.io.Text,org.apache.hadoop.io.IntWritable]](classOf[org.apache.hadoop.mapreduce.lib.output.TextOutputFormat])
required: java.lang.Class[_ <: org.apache.hadoop.mapreduce.OutputFormat]
 job.setOutputFormatClass(classOf[TextOutputFormat[Text, IntWritable]])

Обратите внимание, что я попытался установитьOutputFormatClass двумя различными способами, и оба не удаются.

Я пытаюсь написать Hadoop WordCount: http://wiki.apache.org/hadoop/WordCount в Scala. Эквивалентными строками в Java являются

job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);

Документы API для работы: http://hadoop.apache.org/common/docs/current/api/org/apache/hadoop/mapreduce/Job.html

Ответы [ 2 ]

4 голосов
/ 19 июня 2011

См. Программирование в Scala, экзистенциальные типы .

val textInputFormatClass = classOf[TextInputFormat].
     asInstanceOf[Class[T] forSome {type T <: InputFormat[String, String]}]

job.setInputFormatClass(textInputFormatClass)

Замените типы, которые вы используете для [String, String]

scala> val textInputFormatClass = classOf[TextInputFormat].asInstanceOf[Class[T] forSome {type T <: InputFormat[String, String]}]
textInputFormatClass: java.lang.Class[_ <: org.apache.hadoop.mapreduce.InputFormat[String,String]] = class org.apache.hadoop.mapreduce.lib.input.TextInputFormat

scala> val job = new Job
job: org.apache.hadoop.mapreduce.Job = org.apache.hadoop.mapreduce.Job@4a205144

scala> job.setInputFormatClass(textInputFormatClass)

scala>
2 голосов
/ 02 сентября 2011

Начиная с Scala 2.9.1 (не Scala 2.9.0 - см. https://issues.scala -lang.org / browse / SI-4603 ), это также работает:

job.setInputFormatClass(classOf[TextInputFormat])
...