Bob Wakefield
Bob Wakefield

Reputation: 4009

Why am I importing so many classes?

I'm looking at example Spark code and I'm a bit confused as to why the sample code I'm looking at requires two import statements:

import org.apache.spark._
import org.apache.spark.SparkContext._

This is Scala. As I understand it, _ is the wildcard character. So this looks like I'm importing SparkContext twice. Can anybody shed light on this?

Upvotes: 0

Views: 80

Answers (1)

Ray Toal
Ray Toal

Reputation: 88428

This first line says to import all of the classes in the package org.apache.spark. This means you can use all of those classes without prefixing them with the package name.

The second line says to import all of the static members of the class SparkContext. This means you can use those members without prefixing their names with the class name.

Remember import doesn't really do anything at run time; it just lets you write less code. You aren't actually "importing" anything twice. The use of the term import comes from Java, and admittedly it is confusing.

This might help:

Without the first line, you would have to say

org.apache.spark.SparkContext

but the first import line lets you say

SparkContext

If you had only the first line and not the second, you would have to write

SparkContext.getOrCreate

but with both import lines you can just write

getOrCreate

Upvotes: 6

Related Questions