Hive 基础 02-1 Hive CLI 命令行工具使用(准备阶段-建库、建表、导入数据、编写测试SQL脚本并上传HDFS)

157 阅读1分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第27天,点击查看活动详情

why

【为了测试CLI和Beeline命令行工具,这里先创建一个数据库和表并导入一些数据】

1. 创建数据库

# 创建数据库
hive> CREATE DATABASE IF NOT EXISTS hive_test
    > COMMENT 'hive database for test'
    > LOCATION '/tmp/hive/test'
    > WITH DBPROPERTIES ('create'='yuanzhengme');
OK
Time taken: 0.453 seconds

可以看到HDFS的test目录已被创建:

在这里插入图片描述 可以看到MySQL数据库的元数据也被录入了:

在这里插入图片描述

2. 创建表

# 创建表 【这里不再贴出hive的日志】
hive> use hive_test;
hive> create table if not exists hive_test.word_count(word STRING, count INT) row format delimited fields terminated by ',' lines terminated by '\n' stored as textfile;

3. 导入数据

WordCount.txt 文件内容如下:

[root@tcloud wordCount]# cat ./WordCount.txt
spark,3
hive,3
hadoop,2
kafka,1
hbase,1
# 导入数据 【这里不再贴出hive的日志】
hive> load data local inpath '/home/spark/testFile/wordCount/WordCount.txt' overwrite into table word_count;

验证一下:

可以看到hdfs是已经有WordCount.txt文件了:

在这里插入图片描述 可以看到MySQL数据库的元数据也被录入了:

在这里插入图片描述验证一下:

hive> select * from word_count;
OK
spark   3
hive    3
hadoop  2
kafka   1
hbase   1
Time taken: 5.871 seconds, Fetched: 5 row(s)

4. 编写测试SQL脚本并上传HDFS

-- 其中  hive_test.sql 内容如下
select * from hive_test.word_count;

添加到hdfs:

[root@tcloud ~]# hdfs dfs -put /home/hive/testFile/hive_test.sql /tmp/hive/test

可以看到hdfs目标文件已经有hive_test.sql文件了: 在这里插入图片描述 读取一下:

[root@tcloud ~]# hdfs dfs -cat /tmp/hive/test/hive_test.sql
select * from hive_test.word_count;