2

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

1 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

6 Comments

Docs for Scala, or for Spark?
For Scala, I recommend this book: amazon.com/Programming-Scala-Comprehensive-Step-Step/dp/… . For Spark, I recommend this book : packtpub.com/fast-data-processing-with-spark/book
Thanx. I need to convert a map reduce code into scala. I have seen the map function in scala. But i dont understand it. like in wordcount val file = spark.textFile("hdfs://...") val counts = file.flatMap(line => line.split(" ")) .map(word => (word, 1)) .reduceByKey(_ + _) counts.saveAsTextFile("hdfs://...") But my mapper in mapreduce has a complex algorithm. how can i implement it in scala.
If you have no time to learn Scala, you can use Spark Java APIs.
Can u tell how does the map function for spark java work. like in hadoop map function is called once for each line of file. is it the same way it works in spark
|

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.