简体   繁体   English

如何在Scala \\ Spark中动态选择某些字段?

[英]How to select certain field dynamically in Scala \ Spark?

I am not sure if the title describes my problem accurately but here is my problem: 我不确定标题是否准确地描述了我的问题,但这是我的问题:

dump is of type: dump类型为:

dump: org.apache.spark.rdd.RDD[(String, String, String, String)]

for example: 例如:

val dump = sc.parallelize(List(("a","b","c","s")))

and I have the following for-loop: 而且我有以下for循环:

   for (i <- List(0,1,2,3)) {
      val temp = dump.map(x=> x._i)
    }

But IntelliJ indicates there is an error in x._i . 但是IntelliJ指示x._i存在错误。 Any ideas? 有任何想法吗?

IntelliJ is correct in pointing out that you are using an incorrect syntax. IntelliJ指出您使用的语法不正确是正确的。

What you are trying to do, can be achieved using something like: 您可以尝试使用以下方法来实现:

for (i <- List(0, 1, 2, 3)) {
  val temp = dump.map(x => x.productElement(i))
}

Tuples are actual instances of a class, and they are not exactly an array that you can access using an index. 元组是一个类的实际实例,它们不完全是您可以使用索引访问的数组。 Also, scala, unlike some other languages like JavaScript, doesn't allow string based property access (unless you want to use reflection). 此外,与JavaScript等其他语言不同,scala不允许基于字符串的属性访问(除非您要使用反射)。 What you are trying could work, with some syntactical changes, in a languages like JS but not in Scala. 经过一些语法上的更改,您尝试的内容在像JS这样的语言中却无法在Scala中工作。

However, at least in this case, the same thing can be achieved using the productElement method call as each all Tuples are also instances of Product , which does have the facilities to iterate over the elements, or access them via indices. 但是,至少在这种情况下,使用productElement方法调用可以实现相同的效果,因为每个元组都是Product的实例,它确实具有遍历元素或通过索引访问元素的功能。 Note that index of 0 equals ._1 , and so on. 请注意,索引0等于._1 ,依此类推。

Also, with reference to the comment by @Archeg, there is a limit to what you can put into tuples. 另外,参考@Archeg的评论,可以放入元组的内容有限制。 There are tuple classes ranging from Tuple1 to Tuple22 . 有从Tuple1Tuple22的元组类。 Which means that tuples can contain, at most, 22 elements. 这意味着元组最多可以包含22个元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM