【问题标题】:Error while using "newAPIHadoopFile" API使用“newAPIHadoopFile”API 时出错
【发布时间】:2017-02-26 17:59:00
【问题描述】:

我正在编写以下代码以使用 newAPIHadoopFile API 将文件加载到 Spark。

val lines = sc.newAPIHadoopFile("new_actress.list",classOf[TextInputFormat],classOf[Text],classOf[Text])

但我收到以下错误:

scala> val lines = sc.newAPIHadoopFile("new_actress.list",classOf[TextInputFormat],classOf[Text],classOf[Text])
<console>:34: error: inferred type arguments [org.apache.hadoop.io.Text,org.apache.hadoop.io.Text,org.apache.hadoop.mapred.TextInputFormat] do not conform to method newAPIHadoopFile's type parameter bounds [K,V,F <: org.apache.hadoop.mapreduce.InputFormat[K,V]]
 val lines = sc.newAPIHadoopFile("new_actress.list",classOf[TextInputFormat],classOf[Text],classOf[Text])
                ^
<console>:34: error: type mismatch;
found   : Class[org.apache.hadoop.mapred.TextInputFormat](classOf[org.apache.hadoop.mapred.TextInputFormat])
required: Class[F]
val lines = sc.newAPIHadoopFile("new_actress.list",classOf[TextInputFormat],classOf[Text],classOf[Text])
                                                          ^
<console>:34: error: type mismatch;
found   : Class[org.apache.hadoop.io.Text](classOf[org.apache.hadoop.io.Text])
required: Class[K]
val lines = sc.newAPIHadoopFile("new_actress.list",classOf[TextInputFormat],classOf[Text],classOf[Text])
                                                                                   ^
<console>:34: error: type mismatch;
found   : Class[org.apache.hadoop.io.Text](classOf[org.apache.hadoop.io.Text])
required: Class[V]
val lines = sc.newAPIHadoopFile("new_actress.list",classOf[TextInputFormat],classOf[Text],classOf[Text])
                                                                                                 ^

我在代码中做错了什么?

【问题讨论】:

    标签: scala hadoop apache-spark


    【解决方案1】:

    TextInputFormat 接受&lt;LongWritable,Text&gt;

    注意:关注**InputFormat中的扩展部分

    @InterfaceAudience.Public
    @InterfaceStability.Stable
    public class TextInputFormat
    extends FileInputFormat<LongWritable,Text>
    

    这意味着您不能将FileInputFormat 的两种类型都设置为Text。如果您想使用FileInputFormat,您需要执行以下操作:

    你可以试试:

    import org.apache.hadoop.mapreduce.lib.input.TextInputFormat
    import org.apache.hadoop.io.Text
    import org.apache.hadoop.io.LongWritable
    val lines = sc.newAPIHadoopFile("test.csv", classOf[TextInputFormat], classOf[LongWritable], classOf[Text])
    

    但如果您仍想将两种类型都用作Text,您可以使用KeyValueTextInputFormat,其定义为:

    @InterfaceAudience.Public @InterfaceStability.Stable public class
    KeyValueTextInputFormat extends FileInputFormat<Text,Text>
    

    你可以试试:

    import org.apache.hadoop.mapreduce.lib.input.KeyValueTextInputFormat
    import org.apache.hadoop.io.Text
    val lines = sc.newAPIHadoopFile("test.csv", classOf[KeyValueTextInputFormat], classOf[Text], classOf[Text])
    

    【讨论】:

    • 谢谢...还有一件事,org.apache.hadoop.mapreduce.lib.input.TextInputFormatorg.apache.hadoop.mapred.TextInputFormat 之间有什么区别吗?应该选择哪一个?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-15
    • 2012-12-26
    • 2018-12-04
    • 2014-12-14
    相关资源
    最近更新 更多