Friday, July 31, 2015

Getting Started with Scala Programming on NetBeans IDE 8.0.2

Objectives

This post is intended to help you get started with Scala Programming on NetBeans IDE in 15 minutes.

For demonstration purposes I am using

· Ubuntu Linux 14.04 though commands will be the same in any other Unix / Linux flavor

· Scala 2.105 (Apache Spark distribution) though commands will be the same in other lower / higher scala versions

· JDK 1.7 u80 64-Bit. Scala 2.10.5 requires JDK 1.6 or higher. Make sure you have the appropriate JDK pre-installed on your machine. Installation of JDK is out of scope for this post

Steps

1) Go to any directory on your machine (I use my home directory /home/admnistrator) and type the command

scala –version

You will see the following message which indicates that scala is not installed on your machine.

clip_image002

2) To download Scala 2.10.5 go to your browser and paste the following URL. Scala download page will show up.

http://www.scala-lang.org/download/2.10.5.html

Click the button “Scala 2.10.5” to download Scala binaries. The downloaded file has the name scala-2.10.5.tgz which is a compressed file.

I am using the Linux user “administrator” that was created while installing Ubuntu. Hence, the file got downloaded to /home/administrator/Downloads on my machine.

clip_image004

clip_image006

3) Extract this file using the following command

tar -xvzf scala-2.10.5.tgz

4) Once the extract is complete you will see the folder “scala-2.10.5” under /home/administrator/Downloads. Folders are in blue in the below screenshot.

clip_image008

5) To make this folder available to all users move it to /usr/local/ with the following command. You will need sudo privileges to move this folder to /usr/local/. This is because /usr/local/ is owned by root and not your userid.

sudo mv scala-2.10.5 /usr/local/

clip_image010

You will be prompted for sudo password. Go ahead and enter it.

clip_image012

Note – if there are organizational restrictions that do not allow you to move anything to /usr/local/ then just move this folder to your home directory using the below command. If you copy the scala folder to your home directory, be sure to set the environment variables described in later steps to refer to this location and NOT /usr/local/

mv scala-2.10.5 /home/<your_user>/

6) Go to the destination directory and verify that folder “scala-2.10.5” has execute permissions. This can be done by the command

ls –ltr

If you do not see “rwx” for your user on the scala folder assign permissions as follows

chmod –R 755 scala-2.10.5

This will give your userid full permissions (read, write, execute) and others (read, execute) permissions on the folder “scala-2.10.5”.

Final permissions should look like the following:

clip_image014

7) Next, set environment variables in .bashrc file. You can use any compatible editor available on your machine (vim, nano, etc.). I like nano editor better because it is easier to use.

Open .bashrc file in nano editor using the following command

nano ~/.bashrc

Add entries for environment variables SCALA_HOME and PATH using the following commands:

export SCALA_HOME=/usr/local/scala-2.10.5

export PATH=$PATH:$SCALA_HOME/bin

Ignore the other entries in my screenshot below as they are not meant for this installation

clip_image016

8) Press Ctrl+X to save your changes. At the below question enter “y”.

clip_image018

9) The system will prompt you for a filename as seen in the below screenshot. DO NOT enter another file name as it will result in the .bashrc file getting replaced with the new filename.

Just press ENTER to complete saving the file.

clip_image020

10) To populate Linux environment variables after adding your entries, execute the .bashrc file again using the following command:

source ~/.bashrc

Restarting your machine will also execute the .bashrc file again though you do not need to do it.

11) Check whether your SCALA_HOME variable got updated, using the following command:

echo $SCALA_HOME

This should output the value of SCALA_HOME variable as seen below:

clip_image022

12) Also check if the system is ready to use Scala by going to any directory on your machine and just typing “scala”

For e.g.

$cd /home/administrator

$scala

Scala prompt should show up as follows:

clip_image024

13) Check the scala version on your machine with the following command:

scala –version

The output of this command will show you the scala version you just installed as seen in the below screenshot

clip_image026

Scala is now setup successfully on your machine.

14) Open NetBeans IDE on your machine and go to ToolsàPlugins as shown below

clip_image028

15) Go to Available Plugins tab

clip_image030

16) On the right hand side, enter “scala” in the search text box and press ENTER. You will then see the list of scala plugin libraries available

clip_image032

17) Select all libraries that appear in the search results and click Install

clip_image034

18) In the next dialog box, click Next

clip_image036

19) Accept the License Agreement and click Install

clip_image038

clip_image040

20) In the below certificate warning, click Continue

clip_image042

21) Click Finish when the installation completes successfully. Scala plugins are now installed.

clip_image044

22) To start coding, go to the main page on NetBeans IDE and select FileàNew Project. The New Project window appears. Select the category and project as seen in the below screenshot

Category: Scala

Project: Scala Sbt Project

Click Next

clip_image046

23) Enter a Project Name for your project, e.g.ScalaProject. You can leave the Project Location and Project Folder unchanged or set them to where you want to your project and folder to reside on the system. Click Finish

clip_image048

24) Your project will get created as follows. Observe all sbt dependencies getting downloaded, the log for which is displayed in the bottom white area. Let this process complete.

Note – make sure you have an internet connection at this time

25

25) To start writing Scala code, go to FileàNew File on NetBeans IDE main page as seen below. Select the appropriate Category (Scala) and File Type (Class, Object, Main Object, etc.) and click Next.

clip_image052

That’s it you’re all set! Code away to glory……

Now read .... Access HDFS In Spark

No comments:

Post a Comment