Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error initializing SparkContext: A master URL must be set in your configuration

I used this code

My error is:

Using Spark's default log4j profile: org/apache/spark/log4j-defaults.properties

17/02/03 20:39:24 INFO SparkContext: Running Spark version 2.1.0

17/02/03 20:39:25 WARN NativeCodeLoader: Unable to load native-hadoop 
library for your platform... using builtin-java classes where applicable

17/02/03 20:39:25 WARN SparkConf: Detected deprecated memory fraction 
settings: [spark.storage.memoryFraction]. As of Spark 1.6, execution and  
storage memory management are unified. All memory fractions used in the old 
model are now deprecated and no longer read. If you wish to use the old 
memory management, you may explicitly enable `spark.memory.useLegacyMode` 
(not recommended).

17/02/03 20:39:25 ERROR SparkContext: Error initializing SparkContext.

org.apache.spark.SparkException: A master URL must be set in your 
configuration
at org.apache.spark.SparkContext.<init>(SparkContext.scala:379)
at PCA$.main(PCA.scala:26)
at PCA.main(PCA.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

17/02/03 20:39:25 INFO SparkContext: Successfully stopped SparkContext
Exception in thread "main" org.apache.spark.SparkException: A master URL must be set in your configuration
at org.apache.spark.SparkContext.<init>(SparkContext.scala:379)
at PCA$.main(PCA.scala:26)
at PCA.main(PCA.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at 
   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at   
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

Process finished with exit code 1
like image 213
fakherzad Avatar asked Feb 03 '17 20:02

fakherzad


People also ask

How do I find my Spark master URL?

Once started, the master will print out a spark://HOST:PORT URL for itself, which you can use to connect workers to it, or pass as the “master” argument to SparkContext . You can also find this URL on the master's web UI, which is http://localhost:8080 by default.

What is SparkContext in Apache spark?

Main entry point for Spark functionality. A SparkContext represents the connection to a Spark cluster, and can be used to create RDDs, accumulators and broadcast variables on that cluster. Only one SparkContext should be active per JVM.

Why do we use SparkContext?

SparkContext is the primary point of entry for Spark capabilities. A SparkContext represents a Spark cluster's connection that is useful in building RDDs, accumulators, and broadcast variables on the cluster. It enables your Spark Application to connect to the Spark Cluster using Resource Manager.

How do you make SparkContext?

To create a SparkContext you first need to build a SparkConf object that contains information about your application. SparkConf conf = new SparkConf(). setAppName(appName). setMaster(master); JavaSparkContext sc = new JavaSparkContext(conf);


4 Answers

Most probably you are using Spark 2.x API in Java. Use code snippet like this to avoid this error. This is true when you are running Spark standalone on your computer using Shade plug-in which will import all the runtime libraries on your computer.

SparkSession spark = SparkSession.builder()
                .appName("Spark-Demo")//assign a name to the spark application
                .master("local[*]") //utilize all the available cores on local
                .getOrCreate();
like image 143
Ashu Avatar answered Oct 19 '22 17:10

Ashu


Error message is pretty clear, you have to provide the address of the Spark Master node, either via the SparkContext or via spark-submit:

val conf = 
  new SparkConf()
    .setAppName("ClusterScore")
    .setMaster("spark://172.1.1.1:7077") // <--- This is what's missing
    .set("spark.storage.memoryFraction", "1")

val sc = new SparkContext(conf)
like image 38
Yuval Itzchakov Avatar answered Oct 19 '22 17:10

Yuval Itzchakov


If you are running spark stand alone then

val conf = new SparkConf().setMaster("spark://master") //missing 

and you can pass parameter while submit job

spark-submit --master spark://master

If you are running spark local then

val conf = new SparkConf().setMaster("local[2]") //missing 

you can pass parameter while submit job

spark-submit --master local

if you are running spark on yarn then

spark-submit --master yarn
like image 32
Hutashan Chandrakar Avatar answered Oct 19 '22 17:10

Hutashan Chandrakar


 SparkConf configuration = new SparkConf()
            .setAppName("Your Application Name")
            .setMaster("local");
 val sc = new SparkContext(conf);

It will work...

like image 2
Shyam Gupta Avatar answered Oct 19 '22 18:10

Shyam Gupta