0


Hive的基本操作之数据导入表

  • 进入所要导数据的表所在的库:
hive> select current_database();
OK
test2
  • 建表
hive> create table test_user(
    > id int,
    > name string
    > )
    > row format delimited
    > fields terminated by ','            #以逗号作为列的分隔
    > lines terminated by '\n'            #以换行符\n作为行的分隔
    > stored as textfile;
OK
Time taken: 2.613 seconds
  • 在home目录下创建要导入的数据文件
[root@hadoop01 ~]# pwd
/root
[root@hadoop01 ~]# cat t_user.txt
1,Tom
2,Bob
3,Lily
4,Alice
  • 导入数据方式一

使用hdfs命令将linux本地数据文件上传至hdfs中我们创建的数据库下面的表目录下面:

[root@hadoop01 ~]# hdfs dfs -put t_user.txt /user/hive/warehouse/test2.db/test_user

这个目录我们可以从HDFS ui上找到我们的表对应的目录直接拷贝:

上传好之后我们刷新ui界面,可以看到已上传的数据文件:

回到hive客户端查询数据:

hive>
    >
    > select * from test_user;
OK
1       Tom
2       Bob
3       Lily
4       Alice
Time taken: 1.597 seconds, Fetched: 4 row(s)

它已经按照我们建表语句中的格式配置自动帮我们对数据进行行列格式化了。

  • 导入数据方式二

直接在hive客户端将linux系统本地文件上传至hive中:

hive> load data local inpath '/root/t_user.txt' into table test_user;

#在使用metastore的方式启动hive服务时,如果你是从hive客户端linux文件系统中上传文件,就加上local,如果是从hdfs的文件系统中上传数据文件,就不需要加local,inpath后面的路径参数就是你要上传的文件的路径,这个路径可以是相对路径也可以是绝对路径,但是如果你写相对路径,要特别注意这个相对路径是相对于你当前进入hive时所在的位置,也就是你从哪个路径的位置登创建的hive session,那么相对路径就是从这个位置算起的。
#如果使用hiveserver2/beeline的方式启动hive服务,那么上面的语句中的local指的就是hive服务端的文件系统了,对应的inpath后面的路径也应该是hive服务端中的文件路径。
#load data local inpath '/root/t_user.txt' into overwrite table test_user;
#如果表是已经存在的,且表中有数据,那么你在上传数据文件时可以加上overwrite来表示重写表中的数据,如果不写overwrite,则上传的数据会在表中原数据基础上进行追加。

由于这里我没有加上overwrite参数,所以在使用了方式一导入数据之后,这次再导入数据就相当于是追加数据:

hive> select * from test_user;
OK
1       Tom
2       Bob
3       Lily
4       Alice
1       Tom
2       Bob
3       Lily
4       Alice
Time taken: 0.228 seconds, Fetched: 8 row(s)

刷新ui也可以看到多了一个copy_1的文件,因为两次上传的文件名相同:

上述两种方式中,如果是将linux文件系统中的数据上传至hive时,本质是将linux文件系统中的数据文件拷贝一份放在hdfs的表目录下面,如果是将hdfs文件系统中的数据文件上传至hive时,本质是将hdfs文件系统中的该数据文件移动至hdfs的表目录下面。注意,一个是拷贝,一个是移动。

  • 导入数据方式三

将其他表中的数据导入至目标表中。

上述两种方式中一直在使用的是test_user这个表,现在再建一个新表:

hive> create table test_user_new(
    > id int,
    > name string
    > )
    > row format delimited
    > fields terminated by ','
    > lines terminated by '\n'
    > stored as textfile;

然后将test_user表中的数据插入到test_user_new表中:

hive> insert into table test_user_new select * from test_user;

可以看到数据已经全部都插入到了新表中:

hive> select * from test_user_new;
OK
1       Tom
2       Bob
3       Lily
4       Alice
1       Tom
2       Bob
3       Lily
4       Alice
  • 同时向多张表中插入数据

为了测试,新建了两个表:

hive> create table test_user_mutiple1(
    > id int,
    > name string
    > )
    > row format delimited
    > fields terminated by ','
    > lines terminated by '\n'
    > stored as textfile;

hive> create table test_user_mutiple2(
    > id int,
    > name string
    > )
    > row format delimited
    > fields terminated by ','
    > lines terminated by '\n'
    > stored as textfile;

插入数据:

hive> from test_user
    > insert into test_user_mutiple1 select *               #后面可以加where条件
    > insert into test_user_mutiple2 select * where id=2;   #指定满足条件的数据进行插入

查询插入结果:

hive>
    >
    > select * from test_user_mutiple1;
OK
1       Tom
2       Bob
3       Lily
4       Alice
1       Tom
2       Bob
3       Lily
4       Alice
Time taken: 0.17 seconds, Fetched: 8 row(s)
hive> select * from test_user_mutiple2;
OK
2       Bob
2       Bob
Time taken: 0.303 seconds, Fetched: 2 row(s)
  • 将表中的特定列数据插入至另一个表中

这里要测试的是将test_user表中的id列插入到另一个表中,所以新建一个表,这个表我们就只给一个id字段:

hive> create table test_user_column(id int);

插入数据:

hive> insert into test_user_column select id from test_user;

查询数据:

hive> select * from test_user_column;
OK
1
2
3
4
1
2
3
4
Time taken: 0.228 seconds, Fetched: 8 row(s)
  • 克隆表数据
hive> create table test_user_clone as select * from test_user;
#会将test_user表结构及表中的数据全部克隆过来

查询数据:

hive> select * from test_user_clone;
OK
1       Tom
2       Bob
3       Lily
4       Alice
1       Tom
2       Bob
3       Lily
4       Alice
  • 克隆表结构

使用like关键字,只会克隆表结构,不会将数据也克隆过来:

hive> create table test_user_structure like test_user;

查询数据:

hive> select * from test_user_structure;
OK
Time taken: 0.191 seconds

可以看到查询数据时是空表。

以上就是hive中导入数据时会用到的一些简单的实现方式。

标签: hive hadoop

本文转载自: https://blog.csdn.net/QYHuiiQ/article/details/124330980
版权归原作者 QYHuiiQ 所有, 如有侵权,请联系我们删除。

“Hive的基本操作之数据导入表”的评论:

还没有评论