Spark SQL on Hive配置及实战
因为底层hive最为存储引擎,所以需要配置hive-site.xml
//配置以下参数
hive.metastore.uris
thrift://Master:9083
在linux下运行如下命令
hive --service metastore >metastore.log 2>& 1&
启动spark-shell
val hiveContext = org.apache.spark.sql.hive.HiveContext(sc)
hiveContext.sql("use hive") //使用之前已经建好的hive库
hiveContext.sql("show tables").collect.foreach(println)
hiveContext.sql("select count(*) from sougouq1").collect.foreach(println)
hiveContext.sql("select count(*) from sougouq3 where website like "%baidu%").collect.foreach(println)
hiveContext.sql("select count(*) from sougouq3 where website like "%baidu%" and c_seq=1 and s_seq=1).collect.foreach(println)
Spark SQL在计算后第一次10多秒,第二次以后7秒左右,使用Hive速度在40多秒左右
val df = sqlContext.read.json("/library/examples/src/main/resources/people.json")
df.show()
df.printschema()
df.select("name").show()
df.select(df("name"),df("age") +1).show()
df.filter(df("age")>21).show()
df.groupBy("age").count().show()