1

i have sample.txt:

1 2 3
1 3 2 1 2
1 2 5
6

how to convert this to become sequence array same as

(Seq( Array(Array(1), Array(2), Array(3)),
      Array(Array(1), Array(3), Array(2), Array(1), Array(2)),
      Array(Array(1), Array(2), Array(5)),
      Array(Array(6) )

i want to try using text file for prefixSpan mllib Spark, check this

1 Answer 1

3

Try:

val file = new java.io.File("path/to/sample.txt")
Source.fromFile(file).getLines().map(_.split(' ').map(s => Array(s.toInt)))

This will actually produce an iterator (of type Iterator[Array[Array[Int]]]), but that can be converted to a sequence using .toSeq or .toList or similar.

Working with Spark (which I am not in a position to check with right now), this should become something like:

val data = sc.textFile("...")
data.map(_.split(' ').map(s => Array(s.toInt)))
Sign up to request clarification or add additional context in comments.

5 Comments

i have try like this : val data = sc.textFile("src/data/sample_prefixspan.txt") val transactions = data.map(s => s.trim.split(' ')).map(Array(_) its work, but the result same as (Seq( Array(1,2,3)), Array(1,3,2,1,2), Array(1,2,5), Array(6)) do you have any advice?
Sorry, I'm not sure what you are saying in your comment - it looks like you are on the right track, but I can't tell quite what the remaining problem is.
I mean, I have to try and have a result like this Seq( Array(Array(1,2,3)), not Seq( Array(Array(1), Array(2), Array(3)),
I just need to change a little bit of code and its works thanks
Ah, that is different from what you show in your question - just change s => Array(s.toInt) to _.toInt.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.