Created
May 27, 2016 05:23
-
-
Save randerzander/d56b5b675e3c9d46eae13335cb70fb36 to your computer and use it in GitHub Desktop.
Data-Driven dynamic forms in Apache Zeppelin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def list(table: String, col: String) : Array[(String, String)] = { | |
sqlContext.sql("select distinct " + col + " from " + table + " order by " + col).collect.map(x => (x(0).asInstanceOf[String], x(0).asInstanceOf[String])) | |
} | |
def tables(): Array[(String, String)] = { | |
sqlContext.sql("show tables").collect.map(x => (x(0).asInstanceOf[String], x(0).asInstanceOf[String])) | |
} | |
def columns(table: String) : Array[(String, String)] = { | |
sqlContext.sql("select * from " + table + " limit 0").columns.map(x => (x, x)) | |
} | |
val table = z.select("table", tables()).asInstanceOf[String] | |
val col1 = z.select("col1", columns(table)) | |
val col2 = z.select("col2", columns(table)) | |
val query = "select " + col1 + "," + col2 + ", count(*) as count from " + table + " group by " + col1 + "," + col2 + " order by count desc" | |
val df = sqlContext.sql(query) | |
println("%table " + df.columns.mkString("\t")) | |
println(df.map(x => x.mkString("\t")).collect().mkString("\n")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment