SparkContext(master: Optional[str] = None, appName: Optional[str] = None, sparkHome: Optional[str] = None, pyFiles: Optional[List[str]] = None, environment: Optional[Dict[str, Any]] = None, batchSize: int = 0, serializer: pyspark.serializers.Serializer = CloudPickleSerializer(), conf: Optional[pyspark.conf.SparkConf] = None, gateway: Optional[py4j.java_gateway.JavaGateway] = None, jsc: Optional[py4j.java_gateway.JavaObject] = None, profiler_cls: Type[pyspark.profiler.BasicProfiler] = <class 'pyspark.profiler.BasicProfiler'>, udf_profiler_cls: Type[pyspark.profiler.UDFBasicProfiler] = <class 'pyspark.profiler.UDFBasicProfiler'>, memory_profiler_cls: Type[pyspark.profiler.MemoryProfiler] = <class 'pyspark.profiler.MemoryProfiler'>)¶
Main entry point for Spark functionality. A SparkContext represents the connection to a Spark cluster, and can be used to create
RDDand broadcast variables on that cluster.
When you create a new SparkContext, at least the master and app name should be set, either through the named parameters here or through conf.
- masterstr, optional
Cluster URL to connect to (e.g. mesos://host:port, spark://host:port, local).
- appNamestr, optional
A name for your job, to display on the cluster web UI.
- sparkHomestr, optional
Location where Spark is installed on cluster nodes.
- pyFileslist, optional
Collection of .zip or .py files to send to the cluster and add to PYTHONPATH. These can be paths on the local file system or HDFS, HTTP, HTTPS, or FTP URLs.
- environmentdict, optional
A dictionary of environment variables to set on worker nodes.
- batchSizeint, optional, default 0
The number of Python objects represented as a single Java object. Set 1 to disable batching, 0 to automatically choose the batch size based on object sizes, or -1 to use an unlimited batch size
Serializer, optional, default
The serializer for RDDs.
An object setting Spark properties.
- gatewayclass:py4j.java_gateway.JavaGateway, optional
Use an existing gateway and JVM, otherwise a new JVM will be instantiated. This is only used internally.
- jscclass:py4j.java_gateway.JavaObject, optional
The JavaSparkContext instance. This is only used internally.
- profiler_clstype, optional, default
A class of custom Profiler used to do profiling
- udf_profiler_clstype, optional, default
A class of custom Profiler used to do udf profiling
SparkContextinstance is not supported to share across multiple processes out of the box, and PySpark does not guarantee multi-processing execution. Use threads instead for concurrent processing purpose.
>>> from pyspark.context import SparkContext >>> sc = SparkContext('local', 'test') >>> sc2 = SparkContext('local', 'test2') Traceback (most recent call last): ... ValueError: ...
Add an archive to be downloaded with this Spark job on every node.
Add a file to be downloaded with this Spark job on every node.
Add a tag to be assigned to all the jobs started by this thread.
Add a .py or .zip dependency for all tasks to be executed on this SparkContext in the future.
Read a directory of binary files from HDFS, a local file system (available on all nodes), or any Hadoop-supported file system URI as a byte array.
Load data from a flat binary file, assuming each record is a set of numbers with the specified numerical format (see ByteBuffer), and the number of bytes per record is constant.
Broadcast a read-only variable to the cluster, returning a
Broadcastobject for reading it in distributed functions.
Cancel all jobs that have been scheduled or are running.
Cancel active jobs for the specified group.
Cancel active jobs that have the specified tag.
Clear the current thread’s job tags.
Dump the profile stats into directory path
RDDthat has no partitions or elements.
Return the directory where RDDs are checkpointed.
Return a copy of this SparkContext’s configuration
Get the tags that are currently set to be assigned to all the jobs started by this thread.
Get a local property set in this thread, or null if it is missing.
Get or instantiate a
SparkContextand register it as a singleton object.
hadoopFile(path, inputFormatClass, keyClass, …)
Read an ‘old’ Hadoop InputFormat with arbitrary key and value class from HDFS, a local file system (available on all nodes), or any Hadoop-supported file system URI.
hadoopRDD(inputFormatClass, keyClass, valueClass)
Read an ‘old’ Hadoop InputFormat with arbitrary key and value class, from an arbitrary Hadoop configuration, which is passed in as a Python dict.
newAPIHadoopFile(path, inputFormatClass, …)
Read a ‘new API’ Hadoop InputFormat with arbitrary key and value class from HDFS, a local file system (available on all nodes), or any Hadoop-supported file system URI.
newAPIHadoopRDD(inputFormatClass, keyClass, …)
Read a ‘new API’ Hadoop InputFormat with arbitrary key and value class, from an arbitrary Hadoop configuration, which is passed in as a Python dict.
Distribute a local Python collection to form an RDD.
Load an RDD previously saved using
range(start[, end, step, numSlices])
Create a new RDD of int containing elements from start to end (exclusive), increased by step every element.
Remove a tag previously added to be assigned to all the jobs started by this thread.
runJob(rdd, partitionFunc[, partitions, …])
Executes the given partitionFunc on the specified set of partitions, returning the result as an array of elements.
sequenceFile(path[, keyClass, valueClass, …])
Read a Hadoop SequenceFile with arbitrary key and value Writable class from HDFS, a local file system (available on all nodes), or any Hadoop-supported file system URI.
Set the directory under which RDDs are going to be checkpointed.
Set the behavior of job cancellation from jobs started in this thread.
Set a human readable description of the current job.
setJobGroup(groupId, description[, …])
Assigns a group ID to all the jobs started by this thread until the group ID is set to a different value or cleared.
Set a local property that affects jobs submitted from this thread, such as the Spark fair scheduler pool.
Control our logLevel.
Set a Java system property, such as spark.executor.memory.
Print the profile stats to stdout
Get SPARK_USER for user who is running SparkContext.
Shut down the
textFile(name[, minPartitions, use_unicode])
Read a text file from HDFS, a local file system (available on all nodes), or any Hadoop-supported file system URI, and return it as an RDD of Strings.
Build the union of a list of RDDs.
wholeTextFiles(path[, minPartitions, …])
Read a directory of text files from HDFS, a local file system (available on all nodes), or any Hadoop-supported file system URI.
A unique identifier for the Spark application.
Default min number of partitions for Hadoop RDDs when not given by user
Default level of parallelism to use when not given by user (e.g.
Returns a list of archive paths that are added to resources.
Returns a list of file paths that are added to resources.
Return the resource information of this
Return the epoch time when the
Return the URL of the SparkUI instance started by this
The version of Spark on which this application is running.