2017-02-17 80 views
0

我目前使用的Spark和Scala 2.11.8阵列字符串来

斯卡拉+星火结构的阵列我有以下模式:

root 
|-- partnumber: string (nullable = true) 
|-- brandlabel: string (nullable = true) 
|-- availabledate: string (nullable = true) 
|-- descriptions: array (nullable = true) 
|-- |-- element: string (containsNull = true) 

我试图使用UDF将其转换以下几点:

root 
|-- partnumber: string (nullable = true) 
|-- brandlabel: string (nullable = true) 
|-- availabledate: string (nullable = true) 
|-- description: array (nullable = true) 
| |-- element: struct (containsNull = true) 
| | |-- value: string (nullable = true) 
| | |-- code: string (nullable = true) 
| | |-- cost: int(nullable = true) 

所以源数据是这样的:

[WrappedArray(a abc 100,b abc 300)] 
[WrappedArray(c abc 400)] 

我需要使用“”(空格)作为分隔符,但不知道如何在scala中执行此操作。

def convert(product: Seq[String]): Seq[Row] = { 
    ??/ 
} 

我是相当新的斯卡拉,所以有人可以指导我如何构建这种类型的功能?

谢谢。

回答

2

我不知道我是否理解你的问题,但map可能是你的朋友。

case class Row(a: String, b: String, c: Int) 
val value = List(List("a", "abc", 123), List("b", "bcd", 321)) 

value map { 
    case List(a: String, b: String, c: Int) => Row(a,b,c); 
} 

如果你必须首先对其进行解析:

val value2 = List("a b 123", "c d 345") 
value2 map { 
    case s => { 
     val split = s.toString.split(" ") 
     Row(split(0), split(1), split(2).toInt) 
    } 
} 
+0

谢谢你的快速回复。我试图实现你的建议,但它是说“无法解决符号拆分”我应该使用不同的方法吗? – SuWon

+0

[String.split](http://www.scala-lang.org/api/2.12.x/​​scala/collection/immutable/StringOps.html#split(separator:Char):Array [String])是一种方法为字符串。我不知道你的包装数组是什么类型。也许你必须在每个值上调用toString来获得一个字符串。 val split = s.toString.split(“”) –

+0

谢谢。我能够在你的帮助下解决这个问题。 – SuWon