2017-02-17 78 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