Book Image

Spark Cookbook

By : Rishi Yadav
Book Image

Spark Cookbook

By: Rishi Yadav

Overview of this book

Table of Contents (19 chapters)
Spark Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Loading data from the local filesystem


Though the local filesystem is not a good fit to store big data due to disk size limitations and lack of distributed nature, technically you can load data in distributed systems using the local filesystem. But then the file/directory you are accessing has to be available on each node.

Please note that if you are planning to use this feature to load side data, it is not a good idea. To load side data, Spark has a broadcast variable feature, which will be discussed in upcoming chapters.

In this recipe, we will look at how to load data in Spark from the local filesystem.

How to do it...

Let's start with the example of Shakespeare's "to be or not to be":

  1. Create the words directory by using the following command:

    $ mkdir words
    
  2. Get into the words directory:

    $ cd words
    
  3. Create the sh.txt text file and enter "to be or not to be" in it:

    $ echo "to be or not to be" > sh.txt
    
  4. Start the Spark shell:

    $ spark-shell
    
  5. Load the words directory as RDD:

    scala> val words...