制作元组 scala 字符串



我有一个 Array[String] 的 RDD,看起来像这样:

 mystring= ['thisisastring', 'thisisastring', 'thisisastring', 'thisisastring' ......]

我需要将每个元素或每行都制作成一个元组,它将固定数量的项目组合在一起,以便它们可以作为一个整体传递。所以,从本质上讲,它就像:

(1, 'thisisastring')
(2, 'thisisastring')
(3, 'thisisastring')

所以我认为我需要使用 Tuple2,它是 Tuple2[Int, String]。如果我错了,请提醒我。

当我这样做时:瓦尔vertice = Tuple2(1, mystring).我意识到我只是在每一行中添加 int 1。所以我需要一个循环遍历我的 Array[String],将 1、2 和 3 添加到第 1 行、第 2 行和第 3 行。我想过使用 while(count<14900)。但是val count是一个固定的数字,我不能每次都更新计数的值。你有更好的方法吗?

听起来你正在寻找zipWithIndex。

您没有指定希望生成的 RDD 的类型,而是
指定这将为您提供RDD[(Int, String)]

rdd.flatMap(_.zipWithIndex)

这将为您提供RDD[Array[(Int, String)]

rdd.map(_.zipWithIndex)
how

how use for & yield.

for ( i <- 1 to count ) yield Tuple2(i, mystring(i) )

最新更新