user2492364
user2492364

Reputation: 6693

scala read file and split and then save into val

I have a hello.txt

hello.txt

     [,1]
1       2
2       2
5      12
6       4

and here is scala code:

val textFile = sc.textFile("/home/winsome/share/hello.txt")
val ratings = textFile.map { line => 
    val fields = line.split(" ")  
    val (id, linksStr) = (fields(0).toInt, fields(1).toInt)
    println(id)        //1 2 5 6
    printlin(linkStr)  //2 2 12 4
 }

println(id) and printlin(linkStr) do nothing , Please tell me how to display the format I want
thank you

Upvotes: 1

Views: 1852

Answers (3)

elm
elm

Reputation: 20415

Assuming each line is tab-separated consider a splitting like this,

line.split("\t")

Yet simpler, without separator assumptions, split alphanumerical words,

line.split("\\W+")

Also for extracting each word consider

val Array(a,b,_*) = line.split("\\W+")

Upvotes: 1

Nikita
Nikita

Reputation: 4515

I see 3 possible problems in your code. First, are you sure you have only whitespace as a delimiter? Let's use any numbers of non digits for sure: line.split("[^\\d]+"). Second, how about the first line [, 1] - it has different structure, so you should delete this line. Third problem is that you use map for side effect action. map must be pure-functional, use it only for data transformation. For printing to console use foreach. Let's wrap up:

val textFile = sc.textFile("/home/winsome/share/hello.txt")
val ratings = textFile
  .map ( line => {
    val fields = line.split("[^\\d]+")  
    (fields(0).toInt, fields(1).toInt)
  })
  .foreach(println)

Upvotes: 0

curious
curious

Reputation: 2928

You may want to try this : Reading all the lines from your file Split the line by space and map into your ids and lnkstrs and then print it.

val lines = io.Source.fromFile("hello.txt").getLines()

    lines.map { x =>
      val value = x.split(" ")
      (value.head.toInt, value.last.toInt)
    }.foreach { z =>
      println(z._1)
      println(z._2)
    }

Upvotes: 2

Related Questions