maven:3.3.9 java
jdk:java version "1.8.0_51"mysql
spark:spark-1.6.1.tgzsql
scala:2.11.7apache
若是scala版本是2.11.x,執行以下腳本bash
./dev/change-scala-version.sh 2.11
spark默認狀況下用scala的2.10.5編譯maven
編譯命令以下:ide
mvn -Pyarn -Phadoop-2.6 -Dhadoop.version=2.6.0 -Phive -Phive-thriftserver -Dscala-2.11 -DskipTests clean packageoop
紅色部分爲spark-sql連接hive的所須要的依賴,以及指定的scala的版本spa
注意:hive-site.xml文件須要放到$SPARK_HOME/conf目錄下,不然找不到表scala
運用spark-sql訪問hive
package com.infra.codelab.spark.hive import org.apache.spark.SparkConf import org.apache.spark.SparkContext object HiveTest { val conf = new SparkConf() val sc = new SparkContext(conf) def main(args: Array[String]): Unit = { val sqlContext = new org.apache.spark.sql.hive.HiveContext(sc) sqlContext.sql("SELECT line FROM filecontent ").collect().foreach(println) } }
提交任務:
spark-submit --class com.infra.codelab.spark.hive.HiveTest --master spark://localhost:7077 /home/xiaobin/test/spark/wordcount-0.0.1-SNAPSHOT.jar
spark-sql:
export SPARK_CLASSPATH=$SPARK_CLASSPATH:/home/xiaobin/soft/apache-hive-0.14.0-bin/lib/mysql-connector-java-5.1.35.jar
spark-sql --master spark://xiaobin:7077
spark-sql> select count(*) from filecontent; 483 Time taken: 3.628 seconds, Fetched 1 row(s)