【Hive数据仓库】Hive摆设、Hive数据库操作(增编削查)、表操作(内部表、
目录一、本地模式
1、安装MySQL
2、登录MySQL
3、修改密码
4、安装Hive
5、配置Hive系统情况变量
6、初始化Derby数据库
7、连接Hive用于测试
8、测试Hive
9、修改Hive配置文件
10、上传MySQL驱动包
11、初始化MySQL
12、连接Hive用于启动服务
二、远程模式
1、启动MetaStore服务
2、启动HiveServer2服务
3、在假造机hadoop2安装Hive
4、修改Hive配置文件
5、配置Hive系统情况变量
6、连接MetaStore服务
7、连接HiveServer2服务
三、Hive数据库操作
1、在Hive中创建数据库hive_db
2、查看数据库hive_db的属性信息
3、修改数据库hive_db的属性信息
4、删除数据库
四、表操作
1、创建表
(1)创建内部表
(2)创建外部表
(3)创建分区表
(4)创建桶表
2、查看表
(1)查看当前数据库所有表
(2)查看表的详细结构信息
3、修改表
4、删除表
5、修改分区表的分区
(1)添加分区
(2)重命名分区
(3)删除分区
【往期文章】【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查看
https://i-blog.csdnimg.cn/direct/4d4b64a79d4042edb18675378f26ace3.png
一、本地模式
1、安装MySQL
(1)安装wget工具,使用yum -y install wget,如下所示:
https://i-blog.csdnimg.cn/direct/061bb9c7a1a04342887889fd3f722896.png
(2)下载MySQL源文件
wget http://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm
https://i-blog.csdnimg.cn/direct/989e08d518fe4ffca4a4b1c52948fdc1.png
(3)安装下载Mysql源文件
yum -y install mysql80-community-release-el7-1.noarch.rpm
https://i-blog.csdnimg.cn/direct/1cc25242d1ce4c2593cdec0010d0c345.png
(4)通过yum工具安装MySQL
yum install mysql-community-server -y --nogpgcheck
https://i-blog.csdnimg.cn/direct/ac920811feb44da0bfa9f80194a4b785.png
(5)启动MySQL服务,检查MySQL服务状态
systemctl start mysqld
sudo systemctl status mysqld
https://i-blog.csdnimg.cn/direct/f63d9499b783408492b2759c94bccc54.png
检查MySQL进程是否正在运行,其中27308是MySQL进程的PID
ps -ef | grep mysqld
https://i-blog.csdnimg.cn/direct/6597cf2461454bef97a86e303f06413c.png
(6)MySQL安装完成,默认为root用户提供初始密码
查看该初始密码的命令:
grep 'temporary password' /var/log/mysqld.log
可以看到我的初始密码为:
Bb_*nk#gk5xwhttps://i-blog.csdnimg.cn/direct/f56c0fe7410b49b78cd30e16adce22ba.png
2、登录MySQL
通过我的初始密码登录MySQL,mysql -uroot -pBb_*nk#gk5xw
https://i-blog.csdnimg.cn/direct/9beeae4eb8f7421e88adc90aebf07d24.png
3、修改密码
修改MySQL密码为Itcast@2024,并革新配置,使修改密码操作生效
mysql> alter user 'root'@'localhost' identified by 'Itcast@2022';
mysql> FLUSH PRIVILEGES;
https://i-blog.csdnimg.cn/direct/09dbfcd02ff747149460eca145cfe42e.png
4、安装Hive
(1)下载Hive安装包apache-hive-1.2.2-bin.tar.gz,并在假造机的/export/software目录下实行rz命令上传Hive安装包
https://i-blog.csdnimg.cn/direct/18f5c92e85564ff0b99715a10500aa61.png
(2)ll命令查看安装包是否上传成功
https://i-blog.csdnimg.cn/direct/6763a0f68bac43eea222bc73032941a4.png
(3)将Hive解压安装到目录/export/servers
Tar -zxvf /export/software/apache-hive-1.2.2-bin.tar.gz -C
/export/servers/
https://i-blog.csdnimg.cn/direct/a92e03be53ca4b74b040c6a4b942a67a.png
(4)在假造机的/export/servers/目录下将Hive安装目录重命名为hive-1.2.2
mv /export/servers/apache-hive-1.2.2-bin/ /export/servers/hive-1.2.2
https://i-blog.csdnimg.cn/direct/00d75dac666a4e479223ea3f415c7136.png
(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/
https://i-blog.csdnimg.cn/direct/bc1b89d71ac444ff9df100164fea6de7.png
将hadoop的jia包复制到hive-1.2.2下的lib目录中,
cp guava-11.0.2.jar /export/servers/hive-1.2.2/lib/
https://i-blog.csdnimg.cn/direct/cd933fd7e3514c99b5315356c978bd63.png
https://i-blog.csdnimg.cn/direct/ad293948ddad4172a44cbde5ac1cb829.png
删除Hive中lib目录下的jar包
rm -fr /export/servers/hive-1.2.2/lib/guava-14.0.1.jar
https://i-blog.csdnimg.cn/direct/2f4011c73db0429aa69c99a76a93914c.png
5、配置Hive系统情况变量
编辑系统情况变量文件profile,添加新内容,再source初始化profile
https://i-blog.csdnimg.cn/direct/bba33b3402024cfa981c898013871c16.png
export HIVE_HOME=/export/servers/hive-1.2.2
export PATH=$PATH:$HIVE_HOME/bin
https://i-blog.csdnimg.cn/direct/3dfb7da21cd146d5af77a18956f879b9.png
6、初始化Derby数据库
cd /export/servers/hive-1.2.2
bin/schematool -initSchema -dbType derby
https://i-blog.csdnimg.cn/direct/73f5ebd539fa4ed682d7c16ab72c5537.png
7、连接Hive用于测试
https://i-blog.csdnimg.cn/direct/d96e4f96563442bd989515975224876e.png
8、测试Hive
实行show databases;查看数据库列表
https://i-blog.csdnimg.cn/direct/71e0d408c7da4867901b278ae8687e69.png
9、修改Hive配置文件
(1)在Hive下目录conf里创建Hive配置文件hive-site.xml,在该文件中添加如下内容
https://i-blog.csdnimg.cn/direct/8fc0b1b93ec543a987840a5513f7dde0.png
<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>
https://i-blog.csdnimg.cn/direct/9f05c056476f473dabf2cfb1a93dee6d.png
(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
https://i-blog.csdnimg.cn/direct/5e629052d45948bf9e6a71bfb135f4ce.png
10、上传MySQL驱动包
在/export/servers/hive-1.2.2/lib下上传MySQL驱动包
mysql-connector-java-8.0.23.jar
https://i-blog.csdnimg.cn/direct/e9b1182e206142ed98047db40243aa00.png
11、初始化MySQL
schematool -initSchema -dbType mysql
https://i-blog.csdnimg.cn/direct/659f9ae93f9243c6aa789d6be30413f3.png
检查MySQL服务的状态,sudo systemctl status mysqld
https://i-blog.csdnimg.cn/direct/2d419626208e43a19618fdf941ae6df3.png
12、连接Hive用于启动服务
https://i-blog.csdnimg.cn/direct/37ad85bfd9d74584a2d5a3b49a5710ad.png
二、远程模式
1、启动MetaStore服务
cd /export/servers/hive-1.2.2/conf/
hive --service metastore
https://i-blog.csdnimg.cn/direct/fb45058fb9654d518635237a8c8feae8.png
2、启动HiveServer2服务
打开Hadoop1新窗口启动HiveServer2服务
hive --service hiveserver2
https://i-blog.csdnimg.cn/direct/e21e73731adb457faf7bce63f4c828fc.png
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/
https://i-blog.csdnimg.cn/direct/7a2bb1c81cbc4da1881f15909fee4e23.png
https://i-blog.csdnimg.cn/direct/6152efe4535a4e609ab2273a14334dac.png
https://i-blog.csdnimg.cn/direct/6ef3cd20419c430b89aeeb1a2f17880b.png
(2)重命名Hive安装目录
进入hadoop2的/export/servers/目录,修改Hive目录名为hive-1.2.2
https://i-blog.csdnimg.cn/direct/c3b1118dd15a4e088910d5007c248247.png
(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
https://i-blog.csdnimg.cn/direct/bce44ebd4d1b4a598ec0b40455d84b5a.png
4、修改Hive配置文件
进入假造机Hadoop2的Hive安装目录下conf目录,创建Hive配置文件hive-site.xml,在该文件中添加如下内容。
vi hive-site.xml
https://i-blog.csdnimg.cn/direct/76df3ec1373c4f05b73aae8d4662e8db.png
<?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>
https://i-blog.csdnimg.cn/direct/cc7b10d665c6450faf3873e564348a45.png
5、配置Hive系统情况变量
进入/export/servers配置系统情况变量文件profile,添加如下新内容,然后初始化系统变量
vi /etc/profile
https://i-blog.csdnimg.cn/direct/5ba5e66751154a09ac419e60c15d67b7.png
export HIVE_HOME=/export/servers/hive-1.2.2
export PATH=$PATH:$HIVE_HOME/bin
https://i-blog.csdnimg.cn/direct/5a611a3019404093851679dcb838bc74.png
6、连接MetaStore服务
cd /export/servers/hive-1.2.2/lib
hive
https://i-blog.csdnimg.cn/direct/a9fb4091f3134f3c894f45c3983a4abd.png
7、连接HiveServer2服务
在假造机Hadoop2实行如下命令连接HiveServer2服务(在Hadoop1的第二个窗口启动过HiveServer2服务)
beeline -u jdbc:hive2://hadoop1:10000 -n root
https://i-blog.csdnimg.cn/direct/f395ab31b59d429fb118c86a1ccc97b3.png
成功连接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');
https://i-blog.csdnimg.cn/direct/a9b4395b9c4741f7a97705d551e401fc.png
2、查看数据库hive_db的属性信息
describe database extended hive_db;
https://i-blog.csdnimg.cn/direct/16bbe119698b4c70876f48dbb282a8ba.png
3、修改数据库hive_db的属性信息
将属性author和date的属性值修改为lisi和2022-07-02
alter database hive_db set dbproperties ('author'='lisi','date'='2022-07-02');
https://i-blog.csdnimg.cn/direct/0a2d5ac1699b489994c1eef6d5dfdf0e.png
4、删除数据库
drop database hive_db;
https://i-blog.csdnimg.cn/direct/008e842c07fe448f9521b57a83f8b639.png
四、表操作
1、创建表
先创建数据库itcast
create database if not exists itcast;
https://i-blog.csdnimg.cn/direct/ff8008858bc649e494559382ad411dcf.png
(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");
https://i-blog.csdnimg.cn/direct/69e2f12087c2483ea2e968f9a6c53917.png
(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/';
https://i-blog.csdnimg.cn/direct/6a7b0506e26b4ed08ce51b7e69e33da4.png
(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';
https://i-blog.csdnimg.cn/direct/5602f6830a754d639cbb2a205b0d41ab.png
(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/';
https://i-blog.csdnimg.cn/direct/91f6893b0d464ef7b6b24e4285751441.png
2、查看表
(1)查看当前数据库所有表
use itcast;
show tables;
https://i-blog.csdnimg.cn/direct/3db099852e504d6fab5cac8ef0fc25b5.png
(2)查看表的详细结构信息
查看数据库itcast中表managed_table的详细结构信息。
desc formatted itcast.managed_table;
https://i-blog.csdnimg.cn/direct/f2bc46287020411cb6fa54d388e0d66d.png
3、修改表
(1)将数据库itcast中表external_table重命名为external_table_new。
alter table itcast.external_table rename to external_table_new;
https://i-blog.csdnimg.cn/direct/3566807035b04ea88d1f5ed1e2241a11.png
(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;
https://i-blog.csdnimg.cn/direct/25ca9895463347ad9ef8fc8b6c4bb357.png
(3)向数据库itcast中的表external_table_new添加字段staff_gender,指定该字段的数据范例为string。
alter table itcast.external_table_new add columns (staff_gender STRING);
https://i-blog.csdnimg.cn/direct/db66760b2d9b44f08bcb1c1fa10c99e9.png
4、删除表
删除数据库itcast中的表external_table_new,语句如下:
drop table itcast.external_table_new;
https://i-blog.csdnimg.cn/direct/51c15ad1cfc546f295d82589b2a75831.png
5、修改分区表的分区
(1)添加分区
为数据库itcast的分区表partitioned_table添加分区city=Nanjing。
alter table itcast.partitioned_table add partition(city="Nanjing");
查看分区表包罗的分区:
show partitions itcast.partitioned_table;
https://i-blog.csdnimg.cn/direct/e83fe4fe255c4ebaa1a9f6fdc820d032.png
(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;
https://i-blog.csdnimg.cn/direct/3b81ef87ed54478c9a97d1c58ed120a6.png
(3)删除分区
删除分区表partitioned_table的分区city=Chongqing:
alter table itcast.partitioned_table drop if exists partition(city="Chongqing");
查看分区表包罗的分区:
show partitions itcast.partitioned_table;
https://i-blog.csdnimg.cn/direct/66c16c5d9293411d931122ae1166fc75.png
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]