Spark-SQL连接Hive

Spark-SQL连接Hive

Apache Hive 是 Hadoop 上的 SQL 引擎,Spark SQL 编译时可以包含 Hive 支持,也可以不包含。包含 Hive 支持的 Spark SQL 可以支持 Hive 表访问、UDF (用户自定义函数)、Hive 查询语言(HQL)等。需要强调的一点是,如果要在 Spark SQL 中包含Hive 的库,并不需要事先安装 Hive。一般来说,最好还是在编译 Spark SQL 时引入 Hive支持,这样就可以使用这些特性了。
使用方式分为内嵌Hive、外部Hive、Spark-SQL CLI、Spark beeline 以及代码操作。

*方法一

内嵌的 HIVE*
如果使用 Spark 内嵌的 Hive, 则什么都不用做, 直接使用即可。但是在实际生产活动当中,几乎没有人去使用内嵌Hive这一模式。

*方法二

外部的 HIVE*
如果想在spark-shell中连接外部已经部署好的 Hive,需要通过以下几个步骤:

➢ Spark 要接管 Hive 需要把 hive-site.xml 拷贝到 conf/目录下
➢ 把 Mysql 的驱动 copy 到 jars/目录下
➢ 如果访问不到 hdfs,则需要把 core-site.xml 和 hdfs-site.xml 拷贝到 conf/目录下
➢ 重启 spark-shell    测试spark.sql("show tables").show

Spark-SQL连接Hive_第1张图片
Spark-SQL连接Hive_第2张图片
Spark-SQL连接Hive_第3张图片

*方法三

运行 Spark beeline*
Spark Thrift Server 是 Spark 社区基于 HiveServer2 实现的一个 Thrift 服务。旨在无缝兼容HiveServer2。因为 Spark Thrift Server 的接口和协议都和 HiveServer2 完全一致,因此我们部署好 Spark Thrift Server 后,可以直接使用 hive 的 beeline 访问 Spark Thrift Server 执行相关语句。Spark Thrift Server 的目的也只是取代 HiveServer2,因此它依旧可以和 Hive Metastore进行交互,获取到 hive 的元数据。

如果想连接 Thrift Server,需要通过以下几个步骤:

➢ Spark 要接管 Hive 需要把 hive-site.xml 拷贝到 conf/目录下
[root@node01 conf]# cp /opt/software/hive-2.3.3/conf/hive-site.xml  /opt/software/spark-local/conf/  
➢ 把 Mysql 的驱动 copy 到 jars/目录下
[root@node01 jars]# cp /opt/software/hive-2.3.3/lib/mysql-connector-java-5.1.49.jar  /opt/software/spark-local/jars/
➢ 如果访问不到 hdfs,则需要把 core-site.xml 和 hdfs-site.xml 拷贝到 conf/目录下
[root@node01 conf]# cp /opt/software/hadoop/hadoop-2.9.2/etc/hadoop/core-site.xml  /opt/software/spark-local/conf/
[root@node01 conf]# cp /opt/software/hadoop/hadoop-2.9.2/etc/hadoop/hdfs-site.xml  /opt/software/spark-local/conf/
➢ 启动 Thrift Server  
sbin/start-thriftserver.sh
➢ 使用 beeline 连接 Thrift Server
beeline -u jdbc:hive2://node01:10000 -n root

Spark-SQL连接Hive_第4张图片

<4>Spark SQL CLI

可以很方便的在本地运行 Hive 元数据服务以及从命令行执行查询任务。在 Spark 目录下执行如下命令启动 Spark SQL CLI,直接执行 SQL 语句,类似于 Hive 窗口。
操作步骤:

➢将mysql的驱动放入jars/当中;
➢将hive-site.xml文件放入conf/当中;
➢运行bin/目录下的spark-sql.cmd 或者打开cmd,在
spark-3.0.0-bin-hadoop3.2\bin当中直接运行spark-sql

Spark-SQL连接Hive_第5张图片
查看 show databases;
Spark-SQL连接Hive_第6张图片

<5>代码操作Hive

导入依赖 我们配置依赖一定要配置我们需要的依赖,对于我们不要的依赖最好不要加上,要不然就会导致依赖冲突。


    org.apache.spark
    spark-hive_2.12
    3.0.0


    org.apache.hive
    hive-exec
    2.3.3

  1. 将hive-site.xml 文件拷贝到项目的 resources 目录中。hive的hive-site.xml一定要配置元数据的uri,没有这个一定连接不上

  2. 代码实现。

import org.apache.spark.sql.SparkSession
import org.apache.spark.{SparkConf, SparkContext}

object WordCount {
   def main(args: Array[String]): Unit = {
System.setProperty("HADOOP_USER_NAME", "node01")
val sparkConf = new SparkConf().setMaster("local[*]").setAppName("hive")
val spark:SparkSession = SparkSession.builder()
  .enableHiveSupport()
  .config(sparkConf)
  .getOrCreate()

spark.sql("show databases").show()
spark.sql("create database spark_sql")
spark.sql("show databases").show()
   }
}
结果
+----------+
| namespace|
+----------+
|   default|
| spark_sql|
|  spark_t1|
+----------+

问题1

出现

Unable to instantiate org.apache.hadoop.hive.ql.metadata.SessionHiveMetaStoreClient;


   hive.metastore.uris
   thrift://自己机器的IP:9083

问题2

启动 Thrift Server 没有出来SparkSubmit
需要启动集群还有zkServer.sh start //QuorumPeerMain

问题3

"DBS" in Catalog "" Schema "". DataNucleus requires this table to perform its persistence operations. Either your MetaData is incorrect, or you need to enable "datanucleus.schema.autoCreateTables")

解决方案
在conf下的hive-site.xml添加


    datanucleus.schema.autoCreateAll
    true
 

问题4

不存在hive与spark不兼容的情况,遇到这种问题时可能是spark jars包中小了有hive的相关jar包。

hive-site.xml

  

	  javax.jdo.option.ConnectionURL
         jdbc:mysql://localhost:3306/myhive?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false
  
	 

	  javax.jdo.option.ConnectionDriverName
	  com.mysql.jdbc.Driver
 
	  
  
	  javax.jdo.option.ConnectionUserName  
	  root
 
	  
  
	  javax.jdo.option.ConnectionPassword  
	  123456

	 

	  hive.cli.print.header
	  true

	 

	  hive.cli.print.current.db
	  true
 

		hive.server2.thrift.port
		10000
		Port number of HiveServer2 Thrift interface.
			Canbeoverridden by setting $HIVE SERVER2 THRIFT PORT
		


	       hive.server2.thrift.bind.host
           192.168.67.110 //主机地址
	        Bind host on which to run the HiveServer2 Thrift interface. Can be overridden by setting $HIVE_SERVER2_THRIFT_BIND_HOST


	hive.server2.long.polling.timeout
		5000
	Time in milliseconds that HiveServer2 willwait,before responding to asynchronous calls that use long polling
	hive.server2.enable.doAs 
	false


       
    	  hive.metastore.schema.verification
    	  false


         hive.zookeeper.quorum
        node01,node02,node03


    hbase.zookeeper.quorum
    node01,node02,node03


   hive.metastore.uris
   thrift://node01:9083

你可能感兴趣的:(hive,spark,sql,大数据)