Though there is nothing wrong with the code but maybe something wrong in the way you have tried to execute. Follow these steps:
Step 1:
Start spark daemons .
![image](https://attachment.freshdesk.com/inline/attachment?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6NDA2NTc0ODUyNCwiZG9tYWluIjoiZWR1cmVrYS5mcmVzaGRlc2suY29tIiwiYWNjb3VudF9pZCI6NDE0NTN9.nJ_DqLFxi9P0rXr2lR5nu9r9DqFHnFYryHYVbs5DYGg)
Step 2:
Now create a java project and copy the same code again.
After this right click on project-->buildpath-->configure buildpath-->external library-->external jars
Choose all the jars from /usr/lib/spark/jars folder and Apply
![image](https://attachment.freshdesk.com/inline/attachment?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6NDA2NTc0ODUzOCwiZG9tYWluIjoiZWR1cmVrYS5mcmVzaGRlc2suY29tIiwiYWNjb3VudF9pZCI6NDE0NTN9.tQPolt-DUEOBREm7rE96Zg28R4sodPoQpVJJdOY3l2I)
Step 3:
Now have look at the result when you run the code.
![image](https://attachment.freshdesk.com/inline/attachment?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6NDA2NTc0ODU1MCwiZG9tYWluIjoiZWR1cmVrYS5mcmVzaGRlc2suY29tIiwiYWNjb3VudF9pZCI6NDE0NTN9.k7TAGAhdcDVVO2wVFCnYkoHWRe8NRY-pfC9C-9lTB6g)