hive&&beeline 数据导入导出

hive&&beeline 数据导入导出

hive数据导入方式

1.从本地文件系统中导入数据到Hive表

echo "
create table test (id int, name string,age int, tel string)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ','
STORED AS TEXTFILE;
load data local inpath '/home/hadoop/add.txt' into table test;" | hive

注意:add.txt 字段分割符需要跟创建表字段的分割符一致

2.从HDFS上导入数据到Hive表

hadoop dfs -put /home/hadoop/add.txt /user/hive/add.txt

hive -e "load data inpath '/user/hive/add.txt' into table test;"

3.从别的表中查询出相应的数据并导入到Hive表中

echo "
set hive.exec.dynamic.partition.mode=nonstrict;
create table if not exists test1(
    id int, name string
    ,tel string)
    partitioned by(age int)
    ROW FORMAT DELIMITED
    FIELDS TERMINATED BY ','
    STORED AS TEXTFILE;
insert into table test1 partition(age)
select id, name, tel,age
from test;" |hive

4.创建表的时候通过从别的表中查询出相应的记录并插入到所创建的表中

hive -e "create table test2  as
     select id, name, tel
     from test;"

hive数据导出方式

1.导出到本地文件系统

hive -e "insert overwrite local directory '/home/hadoop/test' 
row format delimited
fields terminated by '\t'
select * from test;"

2.导出到HDFS中

hive -e "insert overwrite directory '/user/hive/' 
row format delimited
fields terminated by '\t'
select * from test;"

3.导出到Hive表

同数据导入方法3


beeline导入导出,需要考虑beeline连接到的hiveserver2的服务器才是你的本地,同时要注意实际是用的hive用户操作本地的,需要确认下hive用户是否有操作本地目录,文件的权限

可以考虑远程用beeline登陆的用户,作为hiveserver2实际操作的用户,如何配置可以找相关资料。后期会写一个hive权限相关的文章

或者本地目录文件给hive用户足够的权限

从权限的角度解决beeline数据导入导出问题,如果需要远程操作,那么hiveserver2得部署在远程

不考虑用户权限之类的问题,方式如下:

beeline 数据导入方式

除了从本地文件导入问题外,同hive方式
本地文件(先导入集群,再load):

   hadoop fs -put add.txt hdfs://hadoop01.homeopen.openpf:8020/user/hive/add.txt;
   beeline -u jdbc:hive2://hadoop03.homeopen.openpf:10000 -n root
-e "load data inpath '/user/hive/add.txt' into table test;"

注意:执行操作的服务器可以是任意集群外的节点,只要配置了hadoop,beeline命令即可

beeline 数据导出方式

除导出到本地外,方法同hive
导出本地文件(先beeline 导入hdfs,然后hdfs dfs -get)

 beeline -u jdbc:hive2://hadoop03.homeopen.openpf:10000 -n root -e "insert overwrite directory '/user/hive/' row format delimited fields terminated by '\t' select * from  test1"

hadoop dfs -get hdfs://hadoop01.homeopen.openpf:8020/user/hive/000000_0

你可能感兴趣的:(hive)