简单的将数据存入数据仓库hive,并操作。
有一个用户user.txt
01#bingbing#23
02#liying#20
03#mali#24
04#xiaoli#18
05#wangli#20
06#mumu#23
要求:
1.将数据存入数据仓库users.myuser(id,name,age),需要将数据放在hdfs的/data下面
2.查找年龄最大的三个人,年龄相同再按照姓名排序
第一题:
-
先将user.txt放到Linux的/boot目录下
-
在hdfs上创建/data
[root@hd01 ~]# hdfs dfs -mkdir /data
查看是否成功创建
[root@hd01 ~]# hdfs dfs -ls /
-
将user.txt上传到hdfs
[root@hd01 ~]# hdfs dfs -put /root/user.txt /data
查看是否创建成功
[root@hd01 ~]# hdfs dfs -ls /data
-
在hive上创建库
hive> create database users;
-
使用库
hive> use users
-
创建表
hive> create external table if not exists myuser(
> id int,
> name string,
> age int
> )
> row format delimited fields terminated by ‘#’
> lines terminated by ‘\n’
> location ‘/data1/myuser’;
-
加载数据
hive> load data inpath ‘/data/user.txt’ into table myuser;
-
查看是否成功
hive> load data inpath ‘/data/user.txt’ into table myuser;
第二题:
hive> select * from users.myuser order by age desc,name limit 3;