“Every problem becomes childish when once it is explained to you”

Friday, July 8, 2016

Spark Hello World with Java 8 and Maven

Java8 is a major release for Java in recent times. Support for functional programming makes Java a feature rich programming language with new additions like Lambda expression,new Stream API  etc..Here we can see how the new features of java can be leveraged to write big data applications using the popular spark framework.
             Spark is written in Scala, which is naturally a functional programming language. Eventhough most of the spark libraries can be accessed via its java API, it wasn't really straightforward to write Spark programs with Java7 due to the lack of Functional nature in java7.
Java8 makes it easy to write Spark programs  with its functional features. 

Below program is written in Java8 on Apache Spark. Those who wanted to see the same in Java7 can refer my previousblog.

Eclipse IDE is used to create and run this program.

Follow the below steps to quickly setup a sample project.

  • Create a simple maven project and update pom.xml with below configuration

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<name>Spark Helloworld</name>

·Create a HelloWorld Java class

package com.semanticeyes.sparkhelloworld;

import java.util.Arrays;
import java.util.List;

import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;

public class HelloSpark {
            public static void main(String[] args) {

                        // Local mode
                        JavaSparkContext sc = new JavaSparkContext("local", "HelloSpark");
                        String[] arr = new String[] { "John", "Paul", "Gavin", "Rahul", "Angel" };
                        List<String> inputList = Arrays.asList(arr);
                        JavaRDD<String> inputRDD = sc.parallelize(inputList);
                        inputRDD.foreach(x -> System.out.println(x));


Thats it..Go and execute it...

No comments:

Post a Comment