Introduction: Everyone can type the code, the key is to have clear logic
- 1- Why does SparkOnHive ( SparkSQL ) appear ?
- 1- In essence, SparkOnHive is designed to solve the problem of slow Hive calculations
- 2- Now more offline analysis in industrial scenarios is based on the integration of Hive+Spark
- 2- Note:
- SparkOnHive only uses Hive metadata information, the others are all spark technology
- You need to copy hive-site.xml of hive to spark/conf to realize that spark knows the metadata information of hive,
- 3-step:
- 1-Principle: Need to integrate Hive metadata information to start metator service
1. SparkSQL integrates Hive
Step 1: Copy hive-site.xml to the conf directory of the spark installation path
因为Hive仅仅只是一个操作MapReduce的一个客户端工具,所以我就在我的第三台主机上安装了Hive
在node3执行以下命令来拷贝hive-site.xml到所有的spark安装服务器上面去
cd /export/server/hive/conf
cp hive-site.xml /export/server/spark/conf/
scp hive-site.xml root@node2:/export/server/spark/conf/
scp hive-site.xml root@node1:/export/server/spark/conf/
Step 2: Copy the mysql connection driver package to the spark jars directory
node3执行以下命令将连接驱动包拷贝到spark的jars目录下,三台机器都要进行拷贝
cd /export/server/hive/lib
cp mysql-connector-java-5.1.38.jar /export/server/spark/jars/
scp mysql-connector-java-5.1.38.jar root@node2:/export/server/spark/jars/
scp mysql-connector-java-5.1.38.jar root@node1:/export/server/spark/jars/
Step 3: Hive opens MetaStore service
1: 修改 hive/conf/hive-site.xml 新增如下配置
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<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://node3:9083</value>
</property>
</configuration>
2: 后台启动 Hive MetaStore服务
nohup /export/server/hive/bin/hive --service metastore &
或者
nohup /export/server/hive/bin/hive --service metastore 2>&1 >> /var/log.log &
Step 4: Test whether Sparksql integrates Hive successfully
先启动hadoop集群,在启动spark集群,确保启动成功之后node01执行命令:
Spark-Shell方式启动:
bin/spark-shell --master local[3]
spark.sql("show databases").show
看到数据的结果,说明sparksql整合hive成功!
日志太多,我们可以修改spark的日志输出级别(conf/log4j.properties)
The tables created in Hive can be seen in the Spark shell
2. IDEA integrates Hive
1. Preparation:
2. Profile explained
3- Note that setting the parameters of the shuffle sql
spark.default.parallieize rdd only be used in the shuffle sparkcore the
official website screenshot
4-code
/**
* @author liu a fu
* @date 2021/1/18 0018
* @version 1.0
* @DESC: 整合Hive和SparkSQL
* 1-准备SparkSession的环境
* 2-使用spark.sql(hive的创建数据库的语句)
* 3-使用spark.sql(hive的创建表的语句)
* 4-使用spark.sql(hive的展示数据库表的语句)
* 5-使用spark.sql(hive的加载本地文件的数据)
* 6-使用spark.sql查询
* 7-停止sparksession
*/
object SparkToHive {
def main(args: Array[String]): Unit = {
//1-准备SparkSession的环境
val spark: SparkSession = SparkSession.builder()
.appName(this.getClass.getSimpleName.stripSuffix("$"))
.master("local[4]")
.enableHiveSupport()
.config("spark.sql.shuffle.partitions", "4")//默认200
//元数据的信息放在那里
//thrift的地址是哪个
//指定hive元数据在hdfs上的位置
.config("spark.sql.warehouse.dir", "hdfs://node1:8020/user/hive/warehouse")
// 指定Hive MetaStore服务地址,客户端连接metastore服务,metastore再去连接MySQL数据库来存取元数据有了metastore服务,就可以有多个客户端同时连接,而且这些客户端不需要知道MySQL数据库的用户名和密码,只需要连接metastore 服务即可。
.config("hive.metastore.uris", "thrift://node3:9083") //Hive安装的位置
// TODO: 告诉Spark要集成Hive,读取Hive表的数据
.getOrCreate()
spark.sparkContext.setLogLevel("WARN")
//2-使用spark.sql(hive的创建数据库的语句)
spark.sql("show databases")
spark.sql("use sparkhive")
//3-使用spark.sql(hive的创建表的语句)
spark.sql("create table student(id int,name String,age int) row format delimited fields terminated by \",\"")
//4-使用spark.sql(hive的展示数据库表的语句)
spark.sql("load data local inpath 'data/input/sql/hive/student.csv' overwrite into table student")
//5-使用spark.sql(hive的加载本地文件的数据)
//6-使用spark.sql查询
spark.sql("desc student").show()
spark.sql("select * from student").show()
//7-停止sparksession
spark.stop()
}
}