Getting Started with Couchbase and Spark on Apache Zeppelin
Author Laurent Doguin walks us through how easy it is to integrate analytics newcomer Apache Zeppelin with Couchbase and Spark from scratch.
Join the DZone community and get the full member experience.Join For Free
while i was at jdays in göteborg, i attended a presentation about apache zeppelin . it's a web-based notebook that enables interactive data analytics. it already supports many interpreters like spark, markdown, angular, elastic and more. it really is well integrated with spark. and couchbase has a spark connector . and because the people behind zeppelin knew spark users would want to use their own dependencies, they made it really easy. easy as in you don't have to write a plugin. but you do have to have the latest version.
build apache zeppelin
latest version as in built from latest sources (ok admitedly it's not 'that' easy...). fairly simple to build though: clone the
, ensure you have the right dependencies (git, jdk, npm, libfontconfig, maven), and then inside the repo type
mvn clean package -dskiptests -pbuild-distr
. this will build the whole thing, now would be a good time to get a coffee (or maybe play with the new
in couchbase 4.5 if you haven't already).
at the end you should have the distribution built form source under
. now to run it simply type
. if you go to http://localhost:8080/ you should see something like this:
adding the couchbase spark connector dependency
now the goal is to add the right dependency to the spark interpreter. an interpreter is the piece of code to transform the content of a pad into something else. so go over the interpreter tab. here you should see the list of available interpreters. you should be able to edit the spark interpreter by simply clicking on edit.
at this point there is two things to do. first mandatory step is to add the dependency to the couchbase spark connector. second is adding a property to access the beer-sample bucket.
under properties, add
as name and something as value. it seems there is a bug right now that forbid you to add a new empty property. you can edit it later.
under dependencies, add
under artifact. don't forget to click on the + buttons.
start writing spark pads
we are in a state where we can start reading or writing data from couchbase. i always import the beer-sample, for some reason. so what we can do is start reading from there. we can easily create a dataframe for all the beer documents. by default the read.couchbase method will read from the default bucket. so to make sure we read from the beer-sample we create a simple option map containing the k/v pair bucket/beer-sample. also, to make sure we only get beer and not brewery, we can add a filter on the type field. these are the two first lines you need to write in the pad to get a dataframe containing all the beer documents. then if you want to use it with spark sql, all you have to do is create a temptable from that dataframe.
to do this, click on notebook and create new note . than you should see an empty pad where you can start writing some scala code. you can also copy/paste the following (but remember copy/pasting is bad).
import org.apache.spark.sql.sources.equalto import com.couchbase.spark.sql._ val options = map("bucket" -> "beer-sample") val dataframe = sqlc.read.couchbase(schemafilter = equalto("type", "beer"), options) dataframe.registertemptable("beer")
if you run this paragraph, it will add another pad to follow up. by default the interpreter is
. if you want to use another interpreter, start the pad with its name. here i want to run spark sql queries. so i'll start the pad with
. instead of interpreting scala spark code as before, it interprets straight spark sql queries.
%sql select abv, count(1) value from beer group by abv order by abv
if you look at the piechart, you'll see there are a lot of values here that are not necessarily useful. some beer have a default 0 value for abv, some have a ridiculously high abv. we can filter all of that:
%sql select abv, count(1) value from beer where abv > 0 and abv < 15 group by abv order by abv
slightly better but we still have a lot of different abv values so we can round this up like this:
%sql select round(abv,0) roundabv, count(1) value from beer where abv > 0 and abv < 15 group by abv order by abv
this is starting to be easier to read. let's group them by category and remove the empty categories at the same time:
%sql select category, round(abv,0) roundabv, count(1) value from beer where abv > 0 and abv < 15 and category != "" group by abv, category order by abv
there are of course many other things you can do with zeppelin but this should be enough to get you started with couchabse. if you want to know more about zeppelin, you can check out their documentation here, they also have some good videos.
Published at DZone with permission of Laurent Doguin, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.