目录
【往期文章】【Hadoop和Hbase集群配置】3台虚拟机、jdk+hadoop+hbase下载和安装、环境配置和集群测试_hbase集群优化配置-CSDN博客
在开始前,先启动下面的几个(括号里是命令)
zookeeper(zkServer.sh start)
YARN(start-yarn.sh)
HDFS(start-dfs.sh)
NameNode(hdfs --daemon start namenode)
DataNode(hdfs --daemon start datanode)
JournalNode(hdfs --daemon start journalnode)
启动后jps查看

一、本地模式
1、安装MySQL
(1)安装wget工具,使用yum -y install wget,如下所示:

(2)下载MySQL源文件
wget http://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm

(3)安装下载Mysql源文件
yum -y install mysql80-community-release-el7-1.noarch.rpm

(4)通过yum工具安装MySQL
yum install mysql-community-server -y --nogpgcheck

(5)启动MySQL服务,检查MySQL服务状态
systemctl start mysqld
sudo systemctl status mysqld

检查MySQL进程是否正在运行,其中27308是MySQL进程的PID
ps -ef | grep mysqld

(6)MySQL安装完成,默认为root用户提供初始密码
查看该初始密码的命令:
grep 'temporary password' /var/log/mysqld.log
可以看到我的初始密码为:
Bb_*nk#gk5xw
2、登录MySQL
通过我的初始密码登录MySQL,mysql -uroot -pBb_*nk#gk5xw

3、修改密码
修改MySQL密码为Itcast@2024,并刷新配置,使修改密码操作生效
mysql> alter user 'root'@'localhost' identified by 'Itcast@2022';
mysql> FLUSH PRIVILEGES;

4、安装Hive
(1)下载Hive安装包apache-hive-1.2.2-bin.tar.gz,并在虚拟机的/export/software目录下执行rz命令上传Hive安装包

(2)ll命令查看安装包是否上传成功

(3)将Hive解压安装到目录/export/servers
Tar -zxvf /export/software/apache-hive-1.2.2-bin.tar.gz -C
/export/servers/

(4)在虚拟机的/export/servers/目录下将Hive安装目录重命名为hive-1.2.2
mv /export/servers/apache-hive-1.2.2-bin/ /export/servers/hive-1.2.2

(5)同步jar包
将hive-1.2.2中的guava-14.0.1.jar替换成hadoop-2.7.6中的guava-11.0.2.jar(在各自的lib里看该版本的具体jar包名)
cd /export/servers/hadoop-2.7.6/share/hadoop/common/lib/
cp guava-11.0.2.jar /export/servers/hive-1.2.2/lib/

将hadoop的jia包复制到hive-1.2.2下的lib目录中,
cp guava-11.0.2.jar /export/servers/hive-1.2.2/lib/


删除Hive中lib目录下的jar包
rm -fr /export/servers/hive-1.2.2/lib/guava-14.0.1.jar

5、配置Hive系统环境变量
编辑系统环境变量文件profile,添加新内容,再source初始化profile

export HIVE_HOME=/export/servers/hive-1.2.2
export PATH=PATH:HIVE_HOME/bin

6、初始化Derby数据库
cd /export/servers/hive-1.2.2
bin/schematool -initSchema -dbType derby

7、连接Hive用于测试

8、测试Hive
执行show databases;查看数据库列表

9、修改Hive配置文件
(1)在Hive下目录conf里创建Hive配置文件hive-site.xml,在该文件中添加如下内容

<configuration>
<property>
<name>javax.jdo.option.ConnectionURL</name>
<value>jdbc:mysql://localhost:3306/hive?createDatabaseIfNotExist=true </value>
</property>
<property>
<name>javax.jdo.option.ConnectionDriverName</name>
<value>com.mysql.cj.jdbc.Driver</value>
</property>
<property>
<name>javax.jdo.option.ConnectionUserName</name>
<value>root</value>
</property>
<property>
<name>javax.jdo.option.ConnectionPassword</name>
<value>Itcast@2024</value>
</property>
<property>
<name>hive.metastore.warehouse.dir</name>
<value>/user/hive_local/warehouse/</value>
</property>
</configuration>

(2)创建HDFS目录,用于存储Hive表和数据,例如创建/user/hive/warehouse
hdfs dfs -mkdir -p /user/hive/warehouse
授予Hive用户(例如,hadoop)和Hadoop组(例如,hadoop)写入和执行该目录的权限
hdfs dfs -chmod g+w /user/hive/warehouse
hdfs dfs -chown -R hadoop:hadoop /user/hive/warehouse

10、上传MySQL驱动包
在/export/servers/hive-1.2.2/lib下上传MySQL驱动包
mysql-connector-java-8.0.23.jar

11、初始化MySQL
schematool -initSchema -dbType mysql

检查MySQL服务的状态,sudo systemctl status mysqld

12、连接Hive用于启动服务

二、远程模式
1、启动MetaStore服务
cd /export/servers/hive-1.2.2/conf/
hive --service metastore

2、启动HiveServer2服务
打开Hadoop1新窗口启动HiveServer2服务
hive --service hiveserver2

3、在虚拟机hadoop2安装Hive
(1)上传Hive安装包
进入/export/sofeware,使用rz命令上传Hive安装包,然后解压到目录/export/servers,ll查看。
tar -zxvf /export/software/apache-hive-1.2.2-bin.tar.gz -C /export/servers/



(2)重命名Hive安装目录
进入hadoop2的/export/servers/目录,修改Hive目录名为hive-1.2.2

(3)同步jar包
将hive-1.2.2中的guava-14.0.1.jar替换成hadoop-2.7.6中的guava-11.0.2.jar(在各自的lib里看该版本的具体jar包名)
cd /export/servers/hadoop-2.7.6/share/hadoop/common/lib/
cp guava-11.0.2.jar /export/servers/hive-1.2.2/lib/
将hadoop的jia包复制到hive-1.2.2下的lib目录中,
cp guava-11.0.2.jar /export/servers/hive-1.2.2/lib/
删除Hive中lib目录下的jar包
rm -fr /export/servers/hive-1.2.2/lib/guava-14.0.1.jar

4、修改Hive配置文件
进入虚拟机Hadoop2的Hive安装目录下conf目录,创建Hive配置文件hive-site.xml,在该文件中添加如下内容。
vi hive-site.xml

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
<name>hive.metastore.uris</name>
<value>thrift://hadoop1:9083</value>
</property>
</configuration>

5、配置Hive系统环境变量
进入/export/servers配置系统环境变量文件profile,添加如下新内容,然后初始化系统变量
vi /etc/profile

export HIVE_HOME=/export/servers/hive-1.2.2
export PATH=PATH:HIVE_HOME/bin

6、连接MetaStore服务
cd /export/servers/hive-1.2.2/lib
hive

7、连接HiveServer2服务
在虚拟机Hadoop2执行如下命令连接HiveServer2服务(在Hadoop1的第二个窗口启动过HiveServer2服务)
beeline -u jdbc:hive2://hadoop1:10000 -n root

成功连接HiveServer2服务,并且进入到Beeline的命令行界面
三、Hive数据库操作
1、在Hive中创建数据库hive_db
定义数据库的描述信息为"This is my hive_db",指定数据库hive_db在HDFS存储数据的目录为/hive/hive_db,定义数据库hive_db的两个属性author和date,这两个属性的属性值分别为zhangsan和2022-07-01。语句如下:
create database if not exists hive_db comment 'This is my hive_db' location '/hive/hive_db' with dbproperties ('author'= 'zhangsan','date'='2022-07-01');

2、查看数据库hive_db的属性信息
describe database extended hive_db;

3、修改数据库hive_db的属性信息
将属性author和date的属性值修改为lisi和2022-07-02
alter database hive_db set dbproperties ('author'='lisi','date'='2022-07-02');

4、删除数据库
drop database hive_db;

四、表操作
1、创建表
先创建数据库itcast
create database if not exists itcast;

(1)创建内部表
在数据库itcast中创建内部表managed_table,语句如下:
create table if not exists
itcast.managed_table(
dept_id INT comment "This is deptid",
staff_id INT comment "This is staffid",
staff_name STRING comment "This is staffname",
staff_age INT comment "This is staffage",
salary FLOAT comment "This is staff salary",
hobby ARRAY<STRING> comment "This is staff hobby",
base_info MAP<STRING, INT> comment "Record height and weight",
person_info STRUCT<marry:STRING,children:STRING>)
row format delimited
fields terminated by ','
collection items terminated by '_'
map keys terminated by ':'
lines terminated by '\n'
tblproperties("comment"="This is a managed table");

(2)创建外部表
在数据库itcast中创建外部表external_table,语句如下
create external table if not exists
itcast.external_table(
staff_id INT comment "This is staffid",
staff_name STRING comment "This is staffname",
salary FLOAT comment "This is staff salary"
)
row format delimited
fields terminated by ','
lines terminated by '\n'
location '/hive/external_table/';

(3)创建分区表
在数据库itcast中创建分区表partitioned_table,语句如下:
create table if not exists
itcast.partitioned_table(
staff_id INT comment "This is staffid",
staff_name STRING comment "This is staffname",
staff_gender STRING
)
partitioned by(
city STRING COMMENT "User live in city"
)
row format delimited
fields terminated by ','
lines terminated by '\n';

(4)创建桶表
在数据库 itcast 中创建桶表 clustered_table,语句如下:
create external table if not exists
itcast.clustered_table(
id STRING,
name STRING,
gender STRING,
age INT,
dept STRING
)
clustered by (dept) sorted by (age desc) into 3 buckets
row format delimited
fields terminated by ','
lines terminated by '\n'
location '/hive/clustered_table/';

2、查看表
(1)查看当前数据库所有表
use itcast;
show tables;

(2)查看表的详细结构信息
查看数据库itcast中表managed_table的详细结构信息。
desc formatted itcast.managed_table;

3、修改表
(1)将数据库itcast中表external_table重命名为external_table_new。
alter table itcast.external_table rename to external_table_new;

(2)将数据库itcast中表external_table_new的字段staff_name修改为staff_username,并且将该字段的数据类型修改为varchar(30)。
alter table itcast.external_table_new change staff_name staff_username varchar(30);
查看修改前后external_table_new表内容:
desc itcast.external_table_new;

(3)向数据库itcast中的表external_table_new添加字段staff_gender,指定该字段的数据类型为string。
alter table itcast.external_table_new add columns (staff_gender STRING);

4、删除表
删除数据库itcast中的表external_table_new,语句如下:
drop table itcast.external_table_new;

5、修改分区表的分区
(1)添加分区
为数据库itcast的分区表partitioned_table添加分区city=Nanjing。
alter table itcast.partitioned_table add partition(city="Nanjing");
查看分区表包含的分区:
show partitions itcast.partitioned_table;

(2)重命名分区
将分区表partitioned_table的分区city=Nanjing重命名为city=Chongqing。
alter table itcast.partitioned_table partition(city="Nanjing") rename to partition(city="Chongqing");
查看分区表包含的分区:
show partitions itcast.partitioned_table;

(3)删除分区
删除分区表partitioned_table的分区city=Chongqing:
alter table itcast.partitioned_table drop if exists partition(city="Chongqing");
查看分区表包含的分区:
show partitions itcast.partitioned_table;
