将查询的数据插入表格
<inesrt overwrite == inesrt into> insert overwrite table employee partition(month=9) select id,name from emp where month=9;
exception: FAILED: SemanticException [Error 10044]: Line 1:23 Cannot insert into target table because column number/types are different '10': Table insclause-0 has 2 columns, but query has 3 columns.
note:select 后面不能加上分区列,因为hive在插入的时候会默认插入已经指定的month字段,所以在查询的时候需要将分区字段去掉
根据内容进行动态分区:insert overwrite table emp partition(age) select id,name,age from emp_n;
会根据最后一个字段(age)进行产生分区信息,即使列名不一样也不影响
将指定分区的数据加载入目标表
insert overwrite table stu partition(classid=1,teacherid) select id,name ,teacherid from stu_tmp where classid=1;
如果分区信息指定了的话在select的映射区则不能出现该字段。静态分区的信息应该在动态分区的前面。
将查询的数据保存到本地
1、hive> insert [overwrite] local directory '/usr/data/hive' select * from mydb.stu; -- hive的保存方式<缺点:列值之间无分割符>
2、$> hive -e "select * from mydb.stu" > a.txt
3、$> hive -f "/usr/data/hive/hql/save.hql' > a.txt
使用SQL向表中插入数据
insert into page_ads select 'page-one', array(1,2,3);