scala - 使用 "newAPIHadoopFile"API 时出错

标签 scala hadoop apache-spark

我正在编写以下代码,使用 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])
                                                                                                 ^

我在代码中做错了什么?

最佳答案

TextInputFormat 需要 <LongWritable,Text> .

注意:同时关注 **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])

关于scala - 使用 "newAPIHadoopFile"API 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40091673/

相关文章:

scala - 检查远程Akka actor是否可用

hadoop - PIG - HBASE - 类型转换值

apache-spark - SparkSql 如果值为 null 则取前一个

Scala Spark 循环没有任何错误,但不产生输出

java - 合并配置库

未找到 Scala 程序的 MySql 驱动程序

java - 使用 Android 从 Scala 调用不同的 Java 父构造函数

java - 如何使用hadoop配置Nut 1.4?

hadoop - 分割tar.gz后将.tar.gz转换为序列文件

scala - 找到 : org. apache.spark.sql.Dataset[(Double, Double)] 需要 : org. apache.spark.rdd.RDD[(Double, Double)]