简体   繁体   English

如何在 spark scala 中迭代 array[string]?

[英]how to iterate over array[string] in spark scala?

enter image description here here is my sample input:此处输入图像描述是我的示例输入:

val list=List("a;bc:de;f","uvw:xy;z","123:456")

I am applying following operation我正在申请以下操作

val upper=list.map(x=>x.split(":")).map(x=>x.split(";"))

but it is throwing error- error: value split is not a member of Array[String]但它抛出错误 -错误:值拆分不是 Array[String] 的成员

can anyone help how to use both split so that i can get answer!任何人都可以帮助如何使用两个拆分,以便我可以得到答案!

Thank you in advance.先感谢您。

Using list.map(x=>x.split(":")) will give you a list of Array.使用list.map(x=>x.split(":"))会给你一个数组列表。

upper: List[Array[String]] = List(Array(a;bc, de;f), Array(uvw, xy;z), Array(123, 456))

Mapping afterwards, you can see that the item will be an array where you are trying to run split on.之后映射,您可以看到该项目将是您尝试在其中运行 split 的数组。

You might use flatMap instead which will first give you List(a;bc, de;f, uvw, xy;z, 123, 456) and then you can use map on those items splitting on ;您可以改用flatMap ,它首先会为您提供List(a;bc, de;f, uvw, xy;z, 123, 456)然后您可以在这些项目上使用 map 拆分;

val upper = list.flatMap(_.split(":")).map(_.split(";"))

Output输出

upper: List[Array[String]] = List(Array(a, bc), Array(de, f), Array(uvw), Array(xy, z), Array(123), Array(456))

You can use split with multiple delimiters in one map iteration :您可以在一次映射迭代中使用带有多个分隔符的split

val upper = list.map(x => x.split("[:;]"))

//upper: List[Array[String]] = List(Array(a, bc, de, f), Array(uvw, xy, z), Array(123, 456))

Here is the code i have tried and it worked:这是我尝试过的代码,它有效:

val upper=list.map(x=>x.split(":")).map(x=>x.map(x=>x.split(";")))

which gives the output:这给出了输出:

upper: List[Array[Array[String]]] = List(Array(Array(a, bc), Array(de, f)), Array(Array(uvw), Array(xy, z)), Array(Array(123), Array(456)))

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

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