SparkSQL>Spark-On-Hive

概述

官网
http://spark.apache.org/docs/latest/sql-data-sources-hive-tables.html

Configuration of Hive is done by placing your hive-site.xml, core-site.xml (for security configuration), and hdfs-site.xml (for HDFS configuration) file in conf/.
  • Hive查询流程及原理
    执行HQL时,先到MySQL元数据库中查找描述信息,然后解析HQL并根据描述信息生成MR任务
    Hive将SQL转成MapReduce执行速度慢
    使用SparkSQL整合Hive其实就是让SparkSQL去加载Hive 的元数据库,然后通过SparkSQL执行引擎去操作Hive表内的数据
    所以首先需要开启Hive的元数据库服务,让SparkSQL能够加载元数据

  • 小结:
    Hive 作用是接收SQL将其转化成MR
    Spark作用是接收SQL将其转化成算子
    SparkSQL可以读取Hive内得数据

Hive开启MetaStore服务

第一步:修改 hive/conf/hive-site.xml 新增如下配置

cd /export/install/hive-1.1.0-cdh5.14.0/conf
vim hive-site.xml
//新增如下配置
<property>
      <name>hive.metastore.warehouse.dir</name>
      <value>/user/hive/warehouse</value>
    </property>
    <property>
      <name>hive.metastore.local</name>
      <value>false</value>
    </property>

  <--  
//<property>
     // <name>hive.metastore.uris</name>
      //<value>thrift://node01:9083</value>
//</property>
-->

SparkSQL整合Hive MetaStore

hive-site.xml 元数据仓库的位置等信息
core-site.xml 安全相关的配置
hdfs-site.xml HDFS 相关的配置

第二步:拷贝hive-site.xml core-site.xml hdfs-site.xml到spark得配置文件内

cd  /export/install/hive-1.1.0-cdh5.14.0/conf 
[root@node01 conf]# cp hive-site.xml  /export/install/spark-2.2.0-bin-2.6.0-cdh5.14.0/conf/

cd  /export/install/hadoop-2.6.0-cdh5.14.0/etc/hadoop
[root@node01 hadoop]# cp  core-site.xml  hdfs-site.xml /export/install/spark-2.2.0-bin-2.6.0-cdh5.14.0/conf/ 

在这里插入图片描述
第三步:spark所有节点拷贝三个配置文件

scp hive-site.xml core-site.xml hdfs-site.xml  node02:$PWD
scp hive-site.xml core-site.xml hdfs-site.xml  node02:$PWD

在这里插入图片描述
启动./spark-shell 先配置驱动文件到spark/jars目录下
在这里插入图片描述
第四步:下载/查找mysql连接驱动包添加到spark得jars文件夹中
在这里插入图片描述
第五步:后台启动 Hive MetaStore服务【可选】

nohup /export/servers/hive/bin/hive --service metastore  &
  • 集群方式开启MetaStore
[root@node01 bin]# ./spark-shell 
scala> spark.sql("show databases").show
+------------+
|databaseName|
+------------+
|          aa|
|      course|
|     default|
|     telecom|
|       video|
|      videos|
+------------+
//查询表
scala> spark.sql("show tables").show
scala> spark.sql("select * from data_connection limit 2").show

使用SparkSQL操作Hive表

  • 将hive-site.xml core-site.xml hdfs-site.xml拷贝到项目得resources中,清空target.
package cn.itcast.sql

import org.apache.spark.sql.SparkSession

object HiveSupport {
  def main(args: Array[String]): Unit = {
    //创建sparkSession
    val spark = SparkSession
      .builder()
      .appName("HiveSupport")
      .master("local[*]")
      //.config("spark.sql.warehouse.dir", "hdfs://node01:8020/user/hive/warehouse")
      //.config("hive.metastore.uris", "thrift://node01:9083")
      .enableHiveSupport()//开启hive语法的支持
      .getOrCreate()
      //设置日志级别
    spark.sparkContext.setLogLevel("WARN")
    
	//查看数据库
 	spark.sql("show databases").show()
    //查看有哪些表
    spark.sql("show tables").show()

    //创建表
    spark.sql("CREATE TABLE person (id int, name string, age int) row format delimited fields terminated by ' '")

    //加载数据,数据为当前SparkDemo项目目录下的person.txt(和src平级)
    spark.sql("LOAD DATA LOCAL INPATH 'SparkDemo/person.txt' INTO TABLE person")

    //查询数据
    spark.sql("select * from person ").show()

    spark.stop()
  }
}

在这里插入图片描述
三个文件复制到resources
在这里插入图片描述
运行之前删除target包
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值