Apache Zeppelin使用入门指南:程式设计
使用Apache Zeppelin
编译和启动完Zeppelin相关的程序之后,我们就可以来使用Zeppelin了。我们进入到https://www.iteblog.com:8080页面,我们可以在页面上直接操作Zeppelin,依次选择Notebook->Create new note,然后会弹出一个对话方块,我们在Note Name里面随便输入一个名字,这里我输入iteblog,然后点选Create Note就可以建立一个新的Notebook了。我们可以在新建的Notebook里面输入相关的程式码进行测试:sc.version
sqlc
res26: String = 1.5.2
res27: org.apache.spark.sql.SQLContext = [email protected]
和Spark Shell一样,Zeppelin会初始化好SparkContext和SQLContext物件,分别命名为sc和sqlc,我们可以直接在里面使用到它。接下来我们来在Zeppelin里面载入HDFS上面的资料,如下:
sc.textFile("hdfs://www.iteblog.com/tmp/json").count
res29: Long = 200
Took 0 seconds (outdated)

我们再来使用sqlc物件读取上面的json档案来建立一个DataFrame:
val profilesJsonRdd =sqlc.jsonFile("hdfs://www.iteblog.com/tmp/json")
val profileDF=profilesJsonRdd.toDF()
val iteblog = profileDF.selectExpr("_id", "address", "age", "email")
iteblog.show()
profileDF.registerTempTable("profiles")
profilesJsonRdd: org.apache.spark.sql.DataFrame = [_id: string, about: string, address: string, age: bigint, company: string, email: string, eyeColor: string, favoriteFruit: string, gender: string, name: string, phone: string, registered: string, tags: array]
profileDF: org.apache.spark.sql.DataFrame = [_id: string, about: string, address: string, age: bigint, company: string, email: string, eyeColor: string, favoriteFruit: string, gender: string, name: string, phone: string, registered: string, tags: array]
root
|-- _id: string (nullable = true)
|-- about: string (nullable = true)
|-- address: string (nullable = true)
|-- age: long (nullable = true)
|-- company: string (nullable = true)
|-- email: string (nullable = true)
|-- eyeColor: string (nullable = true)
|-- favoriteFruit: string (nullable = true)
|-- gender: string (nullable = true)
|-- name: string (nullable = true)
|-- phone: string (nullable = true)
|-- registered: string (nullable = true)
|-- tags: array (nullable = true)
| |-- element: string (containsNull = true)
iteblog: org.apache.spark.sql.DataFrame = [_id: string, address: string, age: bigint, email: string]
+--------------------+--------------------+---+--------------------+
| _id| address|age| email|
+--------------------+--------------------+---+--------------------+
|55578ccb0cc5b350d...|694 Oriental Cour...| 30|[email protected]|
|55578ccb6975c4e2a...|267 Amber Street,...| 23|[email protected]|
|55578ccb33399a615...|243 Bridgewater S...| 24|[email protected]|
|55578ccb0f1d5ab09...|647 Loring Avenue...| 24|[email protected]|
|55578ccb591a45d4e...|721 Bijou Avenue,...| 27|penningtongilbert...|
|55578ccb9f0cd20c4...|694 Llama Court, ...| 21|[email protected]|
|55578ccb8d0accc28...|498 Perry Terrace...| 40|[email protected]|
|55578ccbd682cca21...|243 Stillwell Ave...| 32|[email protected]|
|55578ccb0d9025ddd...|649 Beard Street,...| 36|[email protected]|
|55578ccb5be70de0d...|972 Marconi Place...| 36|[email protected]|
|55578ccbc5a1050a5...|483 Hanson Place,...| 31|[email protected]|
|55578ccb07fa02369...|540 Woodpoint Roa...| 40|[email protected]|
|55578ccb809e55bf0...|442 Ainslie Stree...| 32|[email protected]|
|55578ccb204ff8ee6...|444 Argyle Road, ...| 23|[email protected]|
|55578ccb4b062fc61...|571 Sunnyside Ave...| 38|[email protected]|
|55578ccba5ff361a9...|385 Meeker Avenue...| 40|[email protected]|
|55578ccb386940ac3...|936 Cheever Place...| 37|[email protected]|
|55578ccbfc41ff7fe...|406 Lake Place, M...| 36|[email protected]|
|55578ccbfa6b6c300...|364 Metropolitan ...| 31|[email protected]|
|55578ccbdd6650d81...|113 Applegate Cou...| 29|[email protected]|
+--------------------+--------------------+---+--------------------+
only showing top 20 rows
Took 1 seconds
需要注意的是,因为Zeppelin会自动地初始化好SparkContext和SQLContext,所以我们不能再显示地建立SparkContext和SQLContext。如果你显示地建立了SQLContext,而且使用它来注册一张临时表,当你下一次使用这个临时表的时候将会出现以下的异常资讯:no such table List ([iteblog])
下面我们来使用上面注册的临时表,Zeppelin自带了SQL Interpreter,所以我们可以直接在上面编写SQL语句:
%sql select eyeColor, count(eyeColor) as count from profiles where
gender='male' group by eyeColor

执行上面的SQL我们就可以得到图形化显示的结果,而且我们可以根据自己的需要选择饼型、条型、表格,线型等方式展现我们需要的结果!上面的SQL已经我们已经将查询的gender写死成male了,其实我们可以将这个值设定成引数的形式,然后我们可以在页面上输入相关的查询引数:
%sql select eyeColor, count(eyeColor) as count from profiles where
gender='male' group by eyeColor
然后我们执行这个sql,我们可以看到下图的执行结果:

可以看出这里出现了一个文字框gender,我们可以输入需要查询的条件比如:male,然后再执行就可以得到上面sql一样的结果。大家可能看到了,文字框里面是没有输入限制的,我们可以随便输入资料,而且你也不清楚到底有几种值可以输入。值得高兴的是,我们可以将这个值设定成只固定可选的:
%sql select eyeColor, count(eyeColor) as count from profiles where gender
="${gender=male,male|female}" group by eyeColor
这里限制了gender的值只能选择male和female,而且预设male是选中的,如下:

有时候我们需要在SQL中使用自定义的函式,我们可以直接在Zeppelin中定义函式,然后在SQL使用它,如下:
def ageGroup(age: Long) = {
val buckets = Array("0-10", "11-20", "20-30", "31-40",
"41-50", "51-60", "61-70", "71-80", "81-90", "91-100", ">100")
buckets(math.min((age.toInt - 1) / 10, buckets.length - 1))
}
ageGroup: (age: Long)String
为了能够在Spark SQL中使用这个函式,我们必须先注册这个函式:
sqlc.udf.register("ageGroup", (age:Long)=>ageGroup(age.toInt))
res44: org.apache.spark.sql.UserDefinedFunction = UserDefinedFunction(,StringType,List())
然后我们就可以在Spark SQL中使用这个自定义函式:
%sql select ageGroup(age) as iteblogAgeGroup,
count(1) as total
from profiles
where gender='${gender=male,male|female}' group by ageGroup(age)
order by iteblogAgeGroup
执行的结果如下:

