Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache - Zeppelin using variables across paragraphs

I am trying to accomplish the following use case on Apache Zeppelin: When I write an sql query, for example

%sql SELECT * FROM table1 WHERE column1 = ${column1=1,1|2|3|4}

I get a combo box displayed with these values (1,2,3,4) as options. What I want to do is populate this list with all distinct values available for this column (or as a matter of fact any other set of values which I might want to take from another paragraph in form of a variable). So currently I am stuck at how to use some variables defined in one paragraph inside an sql statement in another paragraph ?

Diving into the code, I saw that inside the Zeppelin-interpreter, a file called Input.java checks for a pattern ${someColumn=someValues}, fills up the combo-box options and then creates a simple query, and hence I have dropped the idea of populating it by running a query in the same paragraph.

like image 665
kunalc92 Avatar asked Nov 25 '15 08:11

kunalc92


2 Answers

I'm using a Scala variable from one paragraph to Shell Script in another paragraph. Here's the answer.

In Scala Cell

%spark2
val myVal = "test-value-across-paragraphs"
z.put("objName", myVal)

In Shell Cell

%sh
echo {objName}

This requires object interpolation to be enabled which can be done by setting the value of the property zeppelin.shell.interpolation to true. Check Apache Zeppelin for further help.

Update May 19, 2019

The above procedure may not work in Zeppelin 2.2 but obviously works in Zeppelin 2.3. Also in 2.3, the value of interpolation can be changed from the sh.config cell.

%sh.conf
zeppelin.shell.interpolation true
like image 156
Abu Shoeb Avatar answered Nov 17 '22 06:11

Abu Shoeb


You can use ZeppelinContext to accomplish this, as it enables you to use put() and get() to set and retrieve objects between paragraphs.

Quoting the example from the linked page, note that the z object is the default instance of ZeppelinContext:

// Put object from scala
%spark
val myObject = ...
z.put("objName", myObject)

# Get object from python
%spark.pyspark
myObject = z.get("objName")
like image 26
jmng Avatar answered Nov 17 '22 05:11

jmng