Hive添加数据以及导出数据 hive阶段必掌握sql

添加数据

普通表添加数据

格式:
 	insert into 表名 values(数据1,数据2,数据3);
例:
 	insert into stu values("1","2",3);

分区表添加数据

格式:
 	insert into 表名 partition(分区字段='分区值') values(数据1,数据2,数据3);
例:
 	insert into stu partition(month ='201807')  values("1","2",3);

通过load方式加载数据

local :本地获取文件(无local 则是hdfs中获取文件)
overwrite :覆盖之前数据(无overwrite 则不覆盖之前数据)
格式:
  	 load data local inpath ‘数据路径’ overwrite into table 表名;
例:
  load data local inpath '/export/score.csv' overwrite into table score partition(month='201806');

通过查询方式加载数据

overwrite :覆盖之前数据(无overwrite 则不覆盖之前数据,关键字overwrite 必须要有)
格式:
 insert overwrite table 表名 需要查询的表及其sql语句;
例:
 insert overwrite table score partition(month = '201806') select s_id,c_id,s_score from score;

多插入模式

介绍:常用于实际生产环境当中,将一张表拆开成两部分或者多部分
创建主表并添加数据:
   create table score(s_id string,c_id string,s_score int) partitioned by (month string) row format delimited fields terminated by '\t';
   load data local inpath '/export/servers/hivedatas/score.csv' overwrite into table score partition(month='201806');
创建第一部分表:
  create table score_first( s_id string,c_id  string) partitioned by (month string) row format delimited fields terminated by '\t' ;
创建第二部分表:
 create table score_second(c_id string,s_score int) partitioned by (month string) row format delimited fields terminated by '\t';
分别给第一部分与第二部分表加载数据:
 from score
 insert overwrite table score_first partition(month='201806') select s_id,c_id
 insert overwrite table score_second partition(month = '201806')  select c_id,s_score;  

创建表时通过location指定加载数据路径

格式:
  create external table 表名(列,列类型) row format delimited fields terminated by '\t' location '数据路径';
例:
   create external table score6 (s_id string,c_id string,s_score int) row format delimited fields terminated by '\t' location '/myscore6';

导出数据

将查询的结果导出到本地

格式:
 	 insert overwrite local directory '本地路径'  指定查询语句;
例:
  	 insert overwrite local directory '/export/servers/exporthive/a' select * from score;

将查询的结果格式化导出到本地

介绍:
  collection items terminated by '#'   对集合类型使用#来进行分割
格式:
 	 insert overwrite local directory '本地路径' row format delimited fields terminated by '\t' collection items terminated by '指定分隔符' 指定查询语句;
例:
  	 insert overwrite local directory '/export/servers/exporthive' row format delimited fields terminated by '\t' collection items terminated by '#' select * from student;

将查询的结果导出到HDFS上(不加local)

格式:
 	 insert overwrite directory '本地路径'  指定查询语句;
例:
  	 insert overwrite local directory '/export/servers/exporthive/a' select * from score;

Hadoop命令导出到本地

格式:
  	 dfs -get hdfs  数据表文件  本地路径;
例:
     dfs -get /export/servers/exporthive/000000_0 /export/servers/exporthive/local.txt;

hive shell 命令导出

格式:
  	hive -e "指定数据库指定数据库表的sql语句"  >  本地路径;
例:
         hive -e "select * from myhive.score;" > /export/servers/exporthive/score.txt

export导出到HDFS上

格式:
  export table 表名 to 'HDFS路径';
例:
  export table score to '/export/exporthive/score';
  • 10
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值