启动hadoop集群
#start-dfs.sh
#start-yarn.sh
关闭hadoop集群
#stop-yarn.sh
#stop-dfs.sh

1 Hive的分区

一个表可以以多个维度来进行分区。分区是在创建表的时候用partitioned by子句定义的。

(1)文件people.csv
utf-8编码格式
姓名,年龄,性别

zhang1,21,m
zhang2,22,m
zhang3,23,m
zhang4,24,m
zhang5,25,m
zhang6,26,m
zhang7,27,m
zhang8,28,m

(2)创建分区表

hive> create database dbtest;
hive> use dbtest;
hive> create table pl(name varchar(4),age int, sex varchar(2)) partitioned by (dt string,country string) row format delimited fields terminated by ',' stored as textfile;
hive> desc pl;查看表结构

hive中PARTITION hive中partition by_hive

注意:partitioned by子句中的列定义是表中正式的列,称为分区列,但是数据文件并不包含这些列的值,因为它们源于目录名。

注意:作为分区的列不可以同时出现在表名()后面的区域内,否则会报出”Column repeated in partitioning columns”的错误。(3)数据加载到分区表

此时要显式的指定分区值。通常新建分区并导入数据时,是通过跑mapreduce程序向HDFS中导入数据的,hdfs的目录名就在这个时候被确定下来。

hive> load data local inpath ‘/root/people.csv’ into table pl partition(dt=‘2021-01-06’,country=‘china’);
 hive> load data local inpath ‘/root/student.csv’ into table pl partition(dt=‘2021-01-06’,country=‘american’);

在文件系统级别,分区只是表目录下嵌套的子目录。

数据文件则存放在底层目录中。

hive> dfs -ls /user/hive/warehouse/dbtest.db/pl/dt=2021-01-06;

hive中PARTITION hive中partition by_sql_02

(4)查看表中有哪些分区

hive> show partitions pl;

hive中PARTITION hive中partition by_spark_03


(5)可以在select语句中以通常的方式使用分区列

hive> select count(*) from pl;花费24秒钟,结果16。
hive> select count(*) from pl where dt='2021-01-06';花费19秒钟,结果16。
hive> select count(*) from pl where country='china';花费18秒钟,结果8。
hive> select count(*) from pl where country='american';花费21秒钟,结果8。

(6)移除分区

hive> alter table pl drop if exists partition(dt='2021-01-06',country='china');
#再次查询就没有了这部分数据。并且文件系统中这部分的数据及分区目录也没有了。
hive> dfs -ls /user/hive/warehouse/dbtest.db/pl/dt=2021-01-06;

hive中PARTITION hive中partition by_hive中PARTITION_04

(7)添加新分区及数据

hive> alter table pl add partition(dt=‘2021-01-06’,country=‘china’);

此时文件系统有了分区目录,但是没有数据。

hive> select count() from pl where country=‘china’;输出0。
将数据直接上传进来。
hive> dfs -put /root/people.csv /user/hive/warehouse/dbtest.db/pl/dt=2021-01-06/country=china;
此时可以查询出这部分数据。
hive> select count(
) from pl where country=‘china’;输出8。

hive> exit;退出hive的shell环境

2 spark连接hive

2.1 方式一JDBC

(1)启动集群
#start-dfs.sh
#start-yarn.sh

#start-master.sh
#start-slaves.sh
(2)启动HiveServer服务
#hive --service hiveserver2 &启动Thrift服务,端口10000。
HiveServer使用thrift服务来为客户端提供远程连接的访问端口,在JDBC连接Hive之前必须先启动HiveServer。
注意:hiveserver不能和hwi服务同时启动使用。
(3)导入Hive JDBC远程连接相关包
使用Eclipse作为开发IDE,在Eclipse中创建hive工程,并导入Hive JDBC远程连接相关包,所需的包如下所示:

hive-jdbc-0.13.1.jar 
 commons-logging-1.1.3.jar
 hive-exec-0.13.1.jar 
 hive-metastore-0.13.1.jar
 libfb303-0.9.0.jar 
 httpcore-4.2.5.ja
 slf4j-api-1.6.1.jar 
 log4j-1.2.16.jar
 hadoop-common-2.2.0.jar 
 hive-service-0.13.1.jar
 slf4j-nop-1.6.1.jar 
 httpclient-4.2.5.jar


为了防止缺漏,将hive下的所有包放到spark的下面,实际上并未执行此操作。
#cp /usr/local/hive/lib/*.jar /usr/local/spark/jars/
(4)spark-shell操作
#spark-shell

import java.sql.DriverManager
// 添加驱动
val driver = "org.apache.hive.jdbc.HiveDriver"
Class.forName(driver)
 // 获取connection
val (url,username,password) = ("jdbc:hive2://pda1:10000/dbtest","root","")
val connection= DriverManager.getConnection(url,username,password)
val sql="select * from pl"
//获取statement
val statement= connection.prepareStatement(sql)
// 获取结果
val res = statement.executeQuery()
while(res.next()){println(res.getString(1))}
// 关闭资源
res.close()
statement.close()
connection.close()

其中:org.apache.hive.jdbc.HiveDriver是Hive JDBC连接驱动名,
使用DriverManager.getConnection("jdbc:hive2://<host>:<port>/hive
中创建的数据库名", "<user>", "");创建连接。

输出
lucy
lucy
lucy
lucy
lucy
lucy
lucy
lucy
zhan
zhan
zhan
zhan
zhan
zhan
zhan
zhan
scala> :quit退出scala的shell环境。

2.2 方式二交互式

2.2.1 spark shell

spark-shell是通过得到sparksession然后调用sql方法执行hive的sql。
得到支持Hive的Spark版本。
(1)测试已装的Spark版本是否支持Hive
spark2.3.0
scala> import org.apache.spark.sql.hive.HiveContext
import org.apache.spark.sql.hive.HiveContext
也就是spark可以识别org.apache.spark.sql.hive.HiveContext,这就说明你当前电脑上的Spark版本不包含Hive支持。
(2)配置依赖
为了让Spark能够访问Hive,需要把Hive的配置文件hive-site.xml拷贝到Spark的conf目录下。同时需要导入相关的依赖包。

#cp /usr/local/hive/conf/hive-site.xml /usr/local/spark/conf/
 #cp /usr/local/hive/lib/*.jar /usr/local/spark/jars/#spark-shell
 scala> import org.apache.spark.sql.hive.HiveContext
 scala> spark.sql(“use dbtest”)
 scala> spark.sql(“show tables”).show()
 scala> spark.sql(“select * from pl”).show()

hive中PARTITION hive中partition by_hive中PARTITION_05

hive中PARTITION hive中partition by_spark_06

2.2.2 spark sql

在Spark SQL中可以直接执行SQL操作Hive。

#spark-sql
 spark-sql> use dbtest;
 spark-sql> show tables;
 spark-sql> select * from pl;
 spark-sql> select count(*) from pl;
 spark-sql> quit;退出


运行过程中,会输出大量的日志信息。

3 异常解决

(1)全局临时表

hive中PARTITION hive中partition by_hive中PARTITION_07

不影响使用,可以不做处理。