前提:
1、spark1.0的包编译时指定支持hive:./make-distribution.sh --hadoop 2.3.0-cdh5.0.0 --with-yarn --with-hive --tgz
2、安装完spark1.0;
3、拷贝mysql驱动包到$SPARK_HOME/lib下;
Spark SQL 支持Hive案例:
1、将hive-site.xml配置文件拷贝到$SPARK_HOME/conf下,编译时已经将hive打到包中了,故此处只需要hive-site配置文件即可
hive-site.xml文件内容形如:
javax.jdo.option.ConnectionURL jdbc:mysql://hadoop000:3306/hive?createDatabaseIfNotExist=true javax.jdo.option.ConnectionDriverName com.mysql.jdbc.Driver javax.jdo.option.ConnectionUserName root javax.jdo.option.ConnectionPassword root
2、启动spark: spark-shell
案例来源于spark官方文档:
//创建hiveContextval hiveContext = new org.apache.spark.sql.hive.HiveContext(sc)// 隐式转换import hiveContext._//创建hive表hql("CREATE TABLE IF NOT EXISTS hive.kv_src (key INT, value STRING)")//加载数据到hive表hql("LOAD DATA LOCAL INPATH '/home/spark/app/spark-1.0.0-bin-2.3.0-cdh5.0.0/examples/src/main/resources/kv1.txt' INTO TABLE hive.kv_src")//通过hql查询hql("FROM hive.kv_src SELECT key, value").collect().foreach(println)
详细信息参见官方文档: