【问题标题】:How to extract a tuple in list in scala如何在scala中提取列表中的元组
【发布时间】:2018-08-27 09:51:54
【问题描述】:

我有一个如下的RDD:

scala> topWithKeysSorted.take(10).foreach(println)
(1,[Lscala.Tuple4;@3e0c2650)
(2,[Lscala.Tuple4;@fef700c)
(3,[Lscala.Tuple4;@1ecb9068)
(4,[Lscala.Tuple4;@35b53a35)
(5,[Lscala.Tuple4;@6ad4a475)
(6,[Lscala.Tuple4;@1e4fd633)
(7,[Lscala.Tuple4;@5c455d42)
(8,[Lscala.Tuple4;@40bdb06d)
(9,[Lscala.Tuple4;@f854303)
(10,[Lscala.Tuple4;@6f9be28e)

scala> topWithKeysSorted.first
res38: (Int, Array[(Int, Int, String, Float)]) = (1,Array((1,2,Quest Q64 10 FT. x 10 FT. Slant Leg Instant U,59.98)))

如何将其转换为(Int, Int, Int, String, Float)?我在想应该有一些我不知道的简单易行的方法。

非常感谢。

【问题讨论】:

    标签: scala list apache-spark tuples


    【解决方案1】:

    您可以使用pattern matching 轻松处理输入元组的各个部分:

    val input = (1, Array((1, 2, "Quest Q64 10 FT. x 10 FT. Slant Leg Instant U", 59.98)))
    input match { case (k, Array((v1, v2, v3, v4))) => (k, v1, v2, v3, v4) }
    

    返回:

    (1,1,2,Quest Q64 10 FT. x 10 FT. Slant Leg Instant U,59.98)
    

    如果你想在你的 RDD 的每个元素上应用这个转换:

    topWithKeysSorted.map{
      case (k, Array((v1, v2, v3, v4))) => (k, v1, v2, v3, v4) 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-18
      • 1970-01-01
      • 2012-10-18
      • 1970-01-01
      相关资源
      最近更新 更多