Spark通过Spark-SQL使用hive 语句,操作hive,底层运行的还是 spark rdd。
(1)就是通过sparksql,加载hive的配置文件,获取到hive的元数据信息
(2)spark sql获取到hive的元数据信息之后就可以拿到hive的所有表的数据
(3)接下来就可以通过spark sql来操作hive表中的数据

image.png

如何使用SparkSql连接Hive

工具

Hive :
hive cli : 命令行
hiveserver2: 允许使用JDBC方式连接hive
hiveserver2提供了:beeline(jdbc客户端),需要先启动hiveserver2,以提供10000端口号
hivemetastore service: 读写元数据、

区别:
hive cli 和 hiveserver2 功能: 解析HQL ———> 读写元数据————->翻译为Job提交允许
hivemetastore service : 提供元数据的操作服务

  1. <br />Spark: <br /> spark-sql : 命令行,只接受sql<br /> thriftserver: 支持JDBC方式访问,类比hiveserver2<br /> thriftserver提供了: beeline(jdbc客户端)

区别:
类似上面。

Hive元数据操作

直连模式

使用JDBC的方式直接去mysql中读取元数据,称为直连模式
需要的条件:

  • 连接Mysql的驱动,已经放入到$HIVE_HOME/lib下
  • 创建连接时,需要有url,username,password,driveClassName,在hive-site.xml中配置 ```xml <?xml version=”1.0”?> <?xml-stylesheet type=”text/xsl” href=”configuration.xsl”?> javax.jdo.option.ConnectionURL jdbc:mysql://hadoop104:3306/metastore(修改此可以使用不同的元数据库)?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=UTF-8 javax.jdo.option.ConnectionDriverName com.mysql.jdbc.Driver javax.jdo.option.ConnectionUserName root javax.jdo.option.ConnectionPassword 123321 <—关闭元数据检查—> hive.metastore.schema.verification false

// 将存储位置从本地改为hdfs

hive.metastore.warehouse.dir hdfs://hadoop102:9820/m

或在spark/conf/spark-defaults.conf中添加 spark.sql.warehouse.dir hdfs://hadoop102:9820/user/hive/warehouse

  1. 或者直接复制hive中的hive-site.xml文件,到sparkconf目录中
  2. hive-clihiveserver2, hive metastoreservice都是采取直连模式获取元数据!<br />因为:<br />1hive-cli的启动: <br />hive中:
  3. ```powershell
  4. hive

实际上,等价于
spark中:

  1. hive --service cli

2)hiveserver2的启动:
hive中

  1. hiveserver2

实际上,等价于
spark中:

  1. hive --service hiveserver2

3)hive metastoreservice的启动:
启动该指令才能连接idea
spark中:

  1. hive --service metastore

hive命令在启动时,会读取hive-site.xml配置文件,加载lib下所有的jar!

代理模式

指第三方程序(非hive的程序,例如sparksql程序,presto,impala),希望读取Hive中的元数据!
有两种方式:

  • 直连模式(不安全,不推荐)
  • 代理模式

在idea中的resources文件夹中天街hive-site.xml文件

  1. <?xml version="1.0"?>
  2. <?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
  3. <configuration>
  4. <property>
  5. <name>hive.metastore.uris</name>
  6. <value>thrift://hadoop102:9083</value>
  7. </property>
  8. </configuration>

在IDEA连接Hive

①添加依赖
依赖后置

  1. <dependency>
  2. <groupId>org.apache.spark</groupId>
  3. <artifactId>spark-hive_2.12</artifactId>
  4. <version>3.0.0</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.apache.hive</groupId>
  8. <artifactId>hive-exec</artifactId>
  9. <version>3.1.2</version>
  10. </dependency>
  11. -----------------------------------------------
  12. // jackson文件依赖
  13. <dependency>
  14. <groupId>com.fasterxml.jackson.core</groupId>
  15. <artifactId>jackson-core</artifactId>
  16. <version>2.10.1</version>
  17. </dependency>

②添加hive-site.xml,向其中配置获取元数据的方式

  1. <property>
  2. <name>hive.metastore.uris</name>
  3. <value>thrift://hadoop102:9083</value>
  4. </property>


/
在Hive安装的机器,开启元数据服务:

  1. hive --service metastore

③idea中:配置SparkSession

  1. private val sparkSession: SparkSession = SparkSession.builder
  2. .enableHiveSupport().
  3. config("spark.sql.warehouse.dir", "hdfs://hadoop102:9820/user/hive/warehouse").
  4. config(conf).getOrCreate()