spark scala错误:value _1不是Iterable [(Int,String,String)]的成员

时间:2018-01-29 08:13:35

标签: scala apache-spark rdd

scala> data.take(5).foreach(println)


(Diamond Fear No Evil Compound Bow Package,CompactBuffer((45,Diamond Fear No Evil Compound Bow Package,599.99)))
(Fitness Gear Pro Half Rack,CompactBuffer((11,Fitness Gear Pro Half Rack,349.99), (16,Fitness Gear Pro Half Rack,349.99)))
(Nike Men's Air Max 2014 Running Shoe,CompactBuffer((19,Nike Men's Air Max 2014 Running Shoe,149.99), (49,Nike Men's Air Max 2014 Running Shoe,149.99)))
(Nike Men's Andrew Luck Jersey - Home Game Ind,CompactBuffer((59,Nike Men's Andrew Luck Jersey - Home Game Ind,100.0)))
(Diamondback Grind BMX Bike 2014,CompactBuffer((10,Diamondback Grind BMX Bike 2014,199.99), (43,Diamondback Grind BMX Bike 2014,199.99)))

如果运行查询

scala> data.map(x => x._2._1).take(5).foreach(println)

<console>:34: error: value _1 is not a member of Iterable[(Int, String, String)]
          data.map(x => x._2._1).take(5).foreach(println)

1 个答案:

答案 0 :(得分:2)

让我们分析您对数据data.map(x => x._2._1).take(5).foreach(println)

执行的操作(Diamond Fear No Evil Compound Bow Package,CompactBuffer((45,Diamond Fear No Evil Compound Bow Package,599.99)))

x._2 CompactBuffer((45,Diamond Fear No Evil Compound Bow Package,599.99))不是tuple,因此您无法使用compactBuffer符号访问underscore元素作为元组的元素_1

那就是错误意味着什么

CompactBufferiterator,其中包含Tuple3,因此您可以使用underscore符号对其进行迭代并访问元素,如下所示。

data.map(x => x._2.map(y => y._1)).take(5).foreach(println)

将为您提供结果

List(45)
List(11, 16)
......

而不是

data.map(x => x._2.map(y => y._3)).take(5).foreach(println)

会给你

List(599.99)
List(349.99, 349.99)
..........

等等。