11

I wanna read csv files in Zeppelin and would like to use databricks' spark-csv package: https://github.com/databricks/spark-csv

In the spark-shell, I can use spark-csv with

spark-shell --packages com.databricks:spark-csv_2.11:1.2.0

But how do I tell Zeppelin to use that package?

Thanks in advance!

fabsta
  • 147
  • 1
  • 4
  • 13
  • 2
    https://zeppelin.incubator.apache.org/docs/interpreter/spark.html#dependencyloading – zero323 Oct 06 '15 at 10:03
  • ok, added: %dep --packages com.databricks:spark-csv_2.11:1.2.0 to a zeppeling notebook, but gave: "Must be used before SparkInterpreter (%spark) initialized". Haven't used %spark in the notebook however – fabsta Oct 06 '15 at 14:07
  • How about %pyspark or %sql? – zero323 Oct 06 '15 at 14:11
  • not sure I understand. Can you give an example, @zero323? – fabsta Oct 07 '15 at 06:53
  • Did you use either `%pyspark` or `%sql` in your notebook? – zero323 Oct 07 '15 at 06:54
  • You can also try: `ZEPPELIN_JAVA_OPTS="-Dspark.jars=/path/to/spark-csv"` – zero323 Oct 07 '15 at 06:58
  • @fabsta: were you able to solve the the ""Must be used before SparkInterpreter (%spark) initialized"". If not, the answer is to restart the interpreter(Interpreter tab and then restart the spark interpreter), along with Samuel's answer. I did not have to use z.reset() though. – RAbraham Dec 24 '15 at 15:22

6 Answers6

15

You need to add the Spark Packages repository to Zeppelin before you can use %dep on spark packages.

%dep
z.reset()
z.addRepo("Spark Packages Repo").url("http://dl.bintray.com/spark-packages/maven")
z.load("com.databricks:spark-csv_2.10:1.2.0")

Alternatively, if this is something you want available in all your notebooks, you can add the --packages option to the spark-submit command setting in the interpreters config in Zeppelin, and then restart the interpreter. This should start a context with the package already loaded as per the spark-shell method.

Simon Elliston Ball
  • 4,375
  • 1
  • 21
  • 18
  • 1
    %dep is now depricated (0.6.1)... see Paul's answer (use the GUI) – Pete Aug 31 '16 at 14:38
  • True. This should now be done in the interpreter configuration as Paul below states. – Simon Elliston Ball Sep 10 '16 at 09:20
  • %dep should not anymore be considered depreciated at this time. See Paul-Armand's answer for the reasons why. – Paul-Armand Verhaegen Nov 07 '16 at 07:14
  • This approach should not be used because jars added using this approach will not be distributed to the spark executors. – Harvinder Singh Apr 19 '18 at 10:29
  • @HarvinderSingh are you sure about that? This approach *does* distribute the jars as part of the way it submits to spark, which is why these notes have to be run before the spark interpreters (at least in the older versions of zeppelin I tested on). That said, the other approaches suggested here provide a cleaner alternative on newer versions. – Simon Elliston Ball Apr 20 '18 at 12:51
8
  1. Go to the Interpreter tab, click Repository Information, add a repo and set the URL to http://dl.bintray.com/spark-packages/maven
  2. Scroll down to the spark interpreter paragraph and click edit, scroll down a bit to the artifact field and add "com.databricks:spark-csv_2.10:1.2.0" or a newer version. Then restart the interpreter when asked.
  3. In the notebook, use something like:

    import org.apache.spark.sql.SQLContext
    
    val sqlContext = new SQLContext(sc)
    val df = sqlContext.read
        .format("com.databricks.spark.csv")
        .option("header", "true") // Use first line of all files as header
        .option("inferSchema", "true") // Automatically infer data types
        .load("my_data.txt")
    

Update:

In the Zeppelin user mailing list, it is now (Nov. 2016) stated by Moon Soo Lee (creator of Apache Zeppelin) that users prefer to keep %dep as it allows for:

  • self-documenting library requirements in the notebook;
  • per Note (and possible per User) library loading.

The tendency is now to keep %dep, so it should not be considered depreciated at this time.

  • I'm not sure what you mean by "create a repo". In my Zeppelin interpreter tab I can create a full new interpreter environment. Plus, I have that Spark packages URL against the field `zeppelin.dep.additionalRemoteRepository`, so how should I exactly make it load the CSV package? – mar tin Oct 12 '16 at 14:20
  • @martin Create a repo (repository) by clicking on the gear icon to the left of the "create" button (for creating a full new interpreter environment which is not what you want). This should expand the available repository list, and reveal a "+" button. Click on the "+" button and add http://dl.bintray.com/spark-packages/maven as URL. You can then just follow Steps 2 and 3. As for your other question, it is normal to have that URL in zeppelin.dep.additionalRemoteRepository . This is a dependency can now be resolved since the external repo is added in Step 1. – Paul-Armand Verhaegen Oct 14 '16 at 06:36
4

BEGIN-EDIT

%dep is deprecated in Zeppelin 0.6.0. Please refer Paul-Armand Verhaegen's answer.

Please read further in this answer, if you are using zeppelin older than 0.6.0

END-EDIT

You can load the spark-csv package using %dep interpreter.

like,

%dep
z.reset()

// Add spark-csv package
z.load("com.databricks:spark-csv_2.10:1.2.0")

See Dependency Loading section in https://zeppelin.incubator.apache.org/docs/interpreter/spark.html

If you've already initialized Spark Context, quick solution is to restart zeppelin and execute zeppelin paragraph with above code first and then execute your spark code to read the CSV file

sag
  • 5,333
  • 8
  • 54
  • 91
1

You can add jar files under Spark Interpreter dependencies:

  1. Click 'Interpreter' menu in navigation bar.
  2. Click 'edit' button for Spark interpreter.
  3. Fill artifact and exclude fields.
  4. Press 'Save'
Gilad
  • 121
  • 1
  • 8
0

if you define in conf/zeppelin-env.sh

export SPARK_HOME=<PATH_TO_SPARK_DIST>

Zeppelin will then look in $SPARK_HOME/conf/spark-defaults.conf and you can define jars there:

spark.jars.packages                com.databricks:spark-csv_2.10:1.4.0,org.postgresql:postgresql:9.3-1102-jdbc41

then look at

http://zepplin_url:4040/environment/ for the following:

spark.jars file:/root/.ivy2/jars/com.databricks_spark-csv_2.10-1.4.0.jar,file:/root/.ivy2/jars/org.postgresql_postgresql-9.3-1102-jdbc41.jar

spark.jars.packages com.databricks:spark-csv_2.10:1.4.0,org.postgresql:postgresql:9.3-1102-jdbc41

For more reference: https://zeppelin.incubator.apache.org/docs/0.5.6-incubating/interpreter/spark.html

Community
  • 1
  • 1
lapolonio
  • 1,107
  • 2
  • 14
  • 24
0

Another solution:

In conf/zeppelin-env.sh (located in /etc/zeppelin for me) add the line:

export SPARK_SUBMIT_OPTIONS="--packages com.databricks:spark-csv_2.10:1.2.0"

Then start the service.

Zack
  • 1,201
  • 8
  • 21