文章目录
一、准备数据文件
创建文件score.txt在主目录下,并写入内容
vim score.txt
李小双 89 78 94 96 87
王丽霞 94 80 86 78 80
吴雨涵 90 67 95 92 60
张晓红 87 76 90 79 59
陈燕文 97 98 92 88 86
用cat 命令查看内容
在hdfs下创建文件目录
hdfs dfs -mkdir -p /hivescore/input/
在hdfs下创建命令要用-mkdir
,-p
用于多级目录创建
上传文件到hdfs
目录下
hdfs dfs -put score.txt /hivescore/input/
二、实现步骤
1、我们需要启动Hive Metastore服务,这是Hive的元数据存储服务
hive --service metastore &
2、执行命令hive
,看到命令提示符hive>
表示进入hive
成功
3、创建Hive表,加载HDFS数据文件
create table t_score ( name string, chinese int, math int, english int, physics int, chemistry int ) row format delimited fields terminated by ' ';
在MySQL的hive数据库的TBLS表里可以查看内部表t_score对应的记录
select TBL_ID,DB_ID,TBL_NAME,TBL_TYPE from metastore.TBLS;
加载成绩数据文件到内部表t_score,在hive
中执行以下命令
load data inpath '/hivescore/input/score.txt' into table t_score;
查看成绩表全部记录,执行命令
select * from t_score;
统计总分和平均分
SELECT CONCAT('(', name, ', ', total_score, ', ', average_score, ')') AS result
FROM (
SELECT name, SUM(chinese + math + english + physics + chemistry) AS total_score, AVG(chinese + math + english + physics + chemistry) AS average_score
FROM t_score
GROUP BY name
) AS subquery;