Getting access to Spark cluster in Spark 2.0
In this recipe, we demonstrate how to get access to a Spark cluster using a single point access named SparkSession
. Spark 2.0 abstracts multiple contexts (such as SQLContext, HiveContext) into a single entry point, SparkSession
, which allows you to get access to all Spark subsystems in a unified way.
How to do it...
- Start a new project in IntelliJ or in an IDE of your choice. Make sure that the necessary JAR files are included.
- Set up the package location where the program will reside:
package spark.ml.cookbook.chapter4
- Import the necessary packages for SparkContext to get access to the cluster.
- In Spark 2.x,
SparkSession
is more commonly used instead.
import org.apache.spark.sql.SparkSession
- Create Spark's configuration and
SparkSession
so we can have access to the cluster:
val spark = SparkSession .builder .master("local[*]") // if use cluster master("spark://master:7077") .appName("myAccesSparkCluster20") .config("spark.sql.warehouse.dir", ".") .getOrCreate...