How to expand horizontal data to vertical from DataFrame? Scala Spark

Keywords: scala apache-spark rdd

Question: 

I has a text file. Now, I want expand horizontal data to vertical. Using the fields from the first field of the specified file to the field specified by num = <n> as the key, the horizontally arranged field data is expanded vertically and rearranged. what should I do?
This is my Input:
data.txt

0000000 aa______ 50 F 91 59 20 76
0000001 bb______ 50 F 46 39 8 5
0000003 cc______ 26 F 30 50 71 36
0000004 dd______ 40 M 58 71 20 10 

I want develop vertically for every 2 fields.
Use the Command: tarr -2 num=1 data.txt
result:

0000000 aa______ 50
0000000 F        91
0000000 59       20
0000000 76
0000001 bb______ 50
0000001 F        46
0000001 39        8
0000001 5
0000003 cc______ 26
0000003 F        30
0000003 50       71
0000003 36
0000004 dd______ 40
0000004 M        58
0000004 71       20
0000004 10 

Use the Command: tarr -2 num=2 data.txt
Result:

0000000 aa______ 50 F
0000000 aa______ 91 59
0000000 aa______ 20 76
0000001 bb______ 50 F
0000001 bb______ 46 39
0000001 bb______ 8 5
0000003 cc______ 26 F
0000003 cc______ 30 50
0000003 cc______ 71 36
0000004 dd______ 40 M
0000004 dd______ 58 71
0000004 dd______ 20 10 

This is my code:

val df = sc.textFile("data.txt");
val splitRdd = df.map{s => val a = s.split("[ |]") 
var i = 0;
val date = Array(a(i) + " " + a(i+1) + " " + a(i+2) + " " + a(i+3) + " " + a(i+4))
(date ++ a.takeRight(0)).mkString(" ")
}
splitRdd.foreach(println) 

Answers: