当我想读取文件时,我的文件格式为: 12334 this:23,word:21,老师:23
val fp = "/user/user_id.txt"
sc.textFile(fp).map { s =>
val Array(did, info_s) = s.split("\t")
val info = info_s.split(",").map { kv =>
val Array(k, v) = kv.split(":")
(k, v.toDouble)
}.toSeq
(did, info)
}
这个scala错误出现了。这个怎么样?
scala.MatchError: [Ljava.lang.String;@51443799 (of class [Ljava.lang.String;)
at com.test.news.IO$$anonfun$1.apply(App.scala:58)
at com.test.news.IO$$anonfun$1.apply(App.scala:57)
at scala.collection.Iterator$$anon$11.next(Iterator.scala:409)
at scala.collection.Iterator$class.foreach(Iterator.scala:893)
at scala.collection.AbstractIterator.foreach(Iterator.scala:1336)
at scala.collection.generic.Growable$class.$plus$plus$eq(Growable.scala:59)
at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:104)
at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:48)
at scala.collection.TraversableOnce$class.to(TraversableOnce.scala:310)
at scala.collection.AbstractIterator.to(Iterator.scala:1336)
at scala.collection.TraversableOnce$class.toBuffer(TraversableOnce.scala:302)
at scala.collection.AbstractIterator.toBuffer(Iterator.scala:1336)
at scala.collection.TraversableOnce$class.toArray(TraversableOnce.scala:289)
at scala.collection.AbstractIterator.toArray(Iterator.scala:1336)
at org.apache.spark.rdd.RDD$$anonfun$collect$1$$anonfun$13.apply(RDD.scala:912)
at org.apache.spark.rdd.RDD$$anonfun$collect$1$$anonfun$13.apply(RDD.scala:912)
at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:1916)
at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:1916)
at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:70)
at org.apache.spark.scheduler.Task.run(Task.scala:86)
at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:274)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
第58行 val数组(did,info_s)= s.split(" \ t"),我不能用这种方式吗?我很困惑〜帮忙!
答案 0 :(得分:7)
第5行(val Array(k, v) = ...
)的语法正在使用数组unapply method。如果您在提取器中提供的绑定数量不等于数组的长度,则可能会出现匹配错误:
scala> val Array(k, v) = "1,2".split(",")
k: String = 1
v: String = 2
scala> val Array(k, v) = "1,2,3".split(",")
scala.MatchError: [Ljava.lang.String;@508dec2b (of class [Ljava.lang.String;)
在您的情况下,这可能是由输入格式错误(多个:
或无)引起的。虽然提取器很有用且简洁,但它们的错误消息含糊不清,所以如果你不是肯定你的匹配是正确的(比如读取任意文本文件),那么使用稍微冗长的语法是很好的做法:
val (k, v) = kv.split(":") match {
case Array(f1, f2) => (f1, f2)
case Array(elems) => fatal("found invalid K/V pair: expected 2 elements, found ${elems.length}")
}
答案 1 :(得分:3)
在scala中解压缩对的语法为:
val (id, info) = ("123", "word:123")
但如果数组中的元素与传递的参数数量不匹配,那么对split()
返回的数组不起作用,只需使用变量捕获结果然后使用index访问数组中的值:
sc.textFile("user_id.txt").map{ line =>
val fields = line.split("\t")
val info = fields(1).split(",").map { kv =>
val pairs = kv.split(":")
(pairs(0), pairs(1).toDouble)
}.toSeq
(fields(0), info)
}.collect()
# Array[(String, Seq[(String, Double)])] = Array((12334,WrappedArray((this,23.0), (word,21.0), (teacher,23.0))))
显然,当我使用上述方法时,我不知道数组的unapply
方法,但我发现使用unapply
方法很有吸引力,这里有一个替代方法可以遵循相同的方法通过解包数组,上面的方法是哲学,只取每行的前两个字段。
基本上在这一行之后,使用_*
来捕获数组中不需要的元素:
val Array(k, v, _*) = Array(1, 2, 3, 4, 5)
#k: Int = 1
#v: Int = 2
以上方法可以改写为:
sc.textFile("user_id.txt").map{ line =>
val Array(id, info_s, _*) = line.split("\t")
val info = info_s.split(",").map { kv =>
val Array(key, value, _*) = kv.split(":")
(key, value.toDouble)
}.toSeq
(id, info)
}.collect()
# Array[(String, Seq[(String, Double)])] = Array((12334,WrappedArray((this,23.0), (word,21.0), (teacher,23.0))))