Spark sql hive 整合 http://shiyanjun.cn/archives/1113.html
都这个点了,很多同事还没有到公司,得那我就简单写个spark sql 的示例 回顾而知新,孔老子明智
package com.ib.e3 import org.apache.spark.sql.SQLContext import org.apache.spark.{SparkConf, SparkContext} /** * Created by xxxxoooo on 9/2/2016. * 不需要导入额外的jar */ class SparkSQLExample { } object SparkSQLExample{ def main(args: Array[String]) { val conf = new SparkConf().setAppName("sparksql").setMaster("local") val sc = new SparkContext(conf) val sql = new SQLContext(sc) //file 文件放在跟src平行的位置 val df = sql.read.json("privateInfo") df.cache() //显示整个数据框 df.show(); /** * +---+-------+ * |age| name| * +---+-------+ * | 18| xiao| * | 20|xiaoyan| * | 19| yan| * +---+-------+ */ df.printSchema() //root //|-- age: long (nullable = true) //|-- name: string (nullable = true) //以下写法经测试都对 df.select("name").show() println("---1---") df.select(df.col("name")).show() df.select(df.col("name"),df.col("age")+2).show() println("---2---") // "age" + 3 不能这么写 ,reason ? //df.select("name","age"+3).show() df.filter(df.col("age").gt(18)).show() df.groupBy(df.col("age")).count().show() } }
埃及 ,一个不太平但依然美丽的地方