gpt4 book ai didi

scala - 在 Spark RDD (Scala) 中指定元素的子集

转载 作者:行者123 更新时间:2023-12-01 13:50:17 25 4
gpt4 key购买 nike

我的数据集是一个 RDD[Array[String]],有 140 多列。如何在不对列号 (.map(x => (x(0),x(3),x(6)...)) 进行硬编码的情况下选择列的子集?

这是我到目前为止(成功)尝试过的方法:

val peopleTups = people.map(x => x.split(",")).map(i => (i(0),i(1)))

但是,我需要多个列,并且希望避免对它们进行硬编码。

这是我迄今为止尝试过的方法(我认为会更好,但失败了):

// Attempt 1
val colIndices = [0,3,6,10,13]
val peopleTups = people.map(x => x.split(",")).map(i => i(colIndices))

// Error output from attempt 1:
<console>:28: error: type mismatch;
found : List[Int]
required: Int
val peopleTups = people.map(x => x.split(",")).map(i => i(colIndices))

// Attempt 2
colIndices map peopleTups.lift

// Attempt 3
colIndices map peopleTups

// Attempt 4
colIndices.map(index => peopleTups.apply(index))

我发现了这个问题并进行了尝试,但是因为我正在查看 RDD 而不是数组,所以它没有用:How can I select a non-sequential subset elements from an array using Scala and Spark?

最佳答案

您应该映射到 RDD 而不是索引。

val list = List.fill(2)(Array.range(1, 6))
// List(Array(1, 2, 3, 4, 5), Array(1, 2, 3, 4, 5))

val rdd = sc.parallelize(list) // RDD[Array[Int]]
val indices = Array(0, 2, 3)

val selectedColumns = rdd.map(array => indices.map(array)) // RDD[Array[Int]]

selectedColumns.collect()
// Array[Array[Int]] = Array(Array(1, 3, 4), Array(1, 3, 4))

关于scala - 在 Spark RDD (Scala) 中指定元素的子集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32340650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com