往hive表中插入数据以及导出数据

转载:https://blog.csdn.net/qq_26442553/article/details/80380590

转载:https://blog.csdn.net/weixin_43681796/article/details/106537339

https://blog.csdn.net/hao495430759/article/details/80529456

 

1、load data 导入数据到hive中

1.将本地数据文件导入到hive非分区表中,如下文件可以是个目录,会导入目录中所有的文件
load data local inpath '/home/robot/'
overwrite into table fdm_sor.personinfo
2.将本地数据文件导入到hive分区表中
load data local inpath '/home/robot/'
overwrite into table fdm_sor.personinfo
partition(country='china',city='nanjing')

 

注意文件格式需要跟建表时指定的一致

 

3、如果文件是在hdfs里的,去掉local即可
注意:
1.inpath里只要填目录即可,不用具体到文件,会加载目录下所有问题,但该目录下不能再有子目录,否则报错。
2.overwrite 可以不加,加的话会将表中所有数据覆盖掉(分区表只覆盖当前分区数据),into talbe 将数据追加到表中。
3.into talbe 如果表里数据已经存在了,会再次到导入,底层文件存储会给同文件名加序列号然后存储。
3.将分布式文件系统上的数据导入的hive中,比如讲hdfs上数据导入到hive中
load data inpath '/user/robot/'
overwrite into table fdm_sor.personinfo
注意:去掉local,则默认的路径是分布式文件系统上的路径,如hdfs上的路径。

2、从本地

2、通过查询insert ....select的形式往hive中导入数据


1.通过查询将数据覆盖导入的分区表中(或者用into追加结果,往动态分区表中插入数据,请参考本系列其他博客。)
insert overwrite table fdm_sor.personinfo
partition(statis_date='${staits_date}'
select a.id,a.name,b.address
from person a left join address b
on a.id = b.id

2.多次插入,从一张表中读数据,下面这种方式效率最高,只需要扫描一次表即可。注意中间没有分号;
from T_DEDUCT_SIGN_D_external t
insert into table t1
select 123 ,sign_no string,null
insert into table t2
select 345 ,null ,bp_no string
insert into table t3
select 678 ,sign_no string,bp_no string
where t.statis_date = '20180101';

 可能出现的错误以及解决

问题:

    记录一次Hive导入数据找不到文件的错误

    load data local inpath '/data/test/ftp/test.txt' into table test;

    FAILED: SemanticException Line 1:23 Invalid path ''/data/test/ftp/test.txt'': No files matching path file:/data/test/ftp/test.txt

    报错找不到文件,确定当前机器确实存在这个文件。

环境:实存在这个文件。在Hive的cli端执行load语句,加载测试文件;Hive版本2.3.7,Hadoop版本2.x

问题分析:可以参考这篇博客https://blog.csdn.net/hao495430759/article/details/80529456 

When using the JDBC driver, the command executes on the HiveServer2 side. The file is evaluated to locally exist on the server, which is not true in your case (it exists on the local client program machine).

Try instead to load the file to HDFS first, and use a HDFS URI in the LOAD DATA statement to make the server find it.

从上面的解释可知, hive导入数据语句  load data [local] inpath ,是一个服务器端的指令,它是在服务器端执行。因此指定local时表明加载的文件为本地文件,但是这里的local,在hive中指的是 hiveserver 服务所在的机器,而不是hivecli 或 beeline客户端所在的机器(生产环境大都是 hiveserver 和 hivecli不在同一个机器)。

问题解决:

    1)将文件上传到metastore服务端,在metastore服务端进行操作

    将文件上传到metastore服务端

    scp /data/test/ftp/test.txt node003:/data/test/ftp/

    登陆metastore服务端

    ssh node003

     登陆beeline后执行load语句

     beeline -u jdbc:hive2://node003:10000 -n root

     load data local inpath '/data/test/ftp/test.txt' into table test;

    查询结果

     select * from test;

    2)将文件上传到hdfs,通过load进行加载

   重新打开一个shell窗口,上产物文件到HDFS

    hdfs dfs -put /data/test/ftp/test.txt /data/test/RAW/

   在已经登陆beeline的窗口加载数据

    load data inpath '/data/test/RAW/test.txt' into table test;

   查看结果

    select * from test;

posted on 2020-11-05 14:27  该用户很懒  阅读(3158)  评论(0编辑  收藏  举报