Pooja3101
Pooja3101

Reputation: 711

reading a file into array list in scala spark

I am completely new to spark and scala.

I want to read a file into an array list.

This is how its done in java.

List<String> sourceRecords;
sourceRecords = new ArrayList<String>();
BufferedReader SW;
SW = new BufferedReader(new FileReader(srcpath[0].toString()));
String srcline ;
while ((srcline = SW.readLine()) != null)  {
sourceRecords.add(srcline.toString());
}

How to do it in scala in spark

Upvotes: 2

Views: 10194

Answers (1)

zsxwing
zsxwing

Reputation: 20816

It's very easy. For example,

val rdd = sc.textFile("your_file_path")
val sourceRecords = rdd.toArray

However, you don't need to convert rdd to Array. You can manipulate rdd like an Array.

You can find more information in https://spark.incubator.apache.org/examples.html

Upvotes: 5

Related Questions