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