createexternaltable if notexists student_ptn(id int,name string,sex string,age int) partitioned by(department string) row format delimited fields terminated by',';
4)创建一个分桶表
相当于mapreduce的分区的 key.hash%tasks
一个目录下创建不同的文件
1 2 3 4 5 6 7 8
createexternaltable if notexists student_buk (id int,name string,sex string,age int,department string) clustered by (id) sorted by (age asc) into3 buckets row format delimited fields terminated by',';
桶的个数:3 id%3 指定分桶字段:id 按照age asc 升序排序
5)进行表复制
关键字:like 只会复制表结构 不会复制表数据
内部表还是外部表取决于表的自身的属性
1
createtable if notexists student_copy like student_external; //没有external,内部表
6)ctas进行建表
查询语句需要存储为一个临时表
create table ..... as select .... from .....where .....
表的修改操作
修改表名
alter table student_copy rename to stu_copy;
修改表的列信息
增加列
alter table stu_copy add columns (salary double);
替换列:将所有的列替换为新给的列
alter table stu_copy replace columns(test01 int,test02 string);
修改列
修改列名
修改列的类型:类型修改的时候 小类型—大类型
alter table stu_copy change test01 aa01 int;
alter table stu_copy change aa01 aa01 string;
alter table stu_copy change aa01 aa01 int;不支持String转为int
load data local inpath '/home/hadoop/datas/student.txt' into table student_managed;
load data local inpath '/home/hadoop/datas/student.txt' overwrite into table student_managed;
从hdfs进行数据加载
1
load data inpath '/hive_in/student.txt'intotable student_managed;
报错: FAILED: SemanticException [Error 10096]: Dynamicpartition strict mode requires at least onestaticpartition column. To turn this off set hive.exec.dynamic.partition.mode=nonstrict 修改配置文件的值: set hive.exec.dynamic.partition.mode=nonstrict;
1 2 3 4 5 6 7 8 9 10 11 12 13
修改插入语句: insertintotable student_ptn partition(department) select id,name,sex,age,department from student_managed; 按照部门进行分区
create table if not exists student_ptn01(id int,name string,age int) partitioned by(sex string,department string) row format delimited fields terminated by ',';
分桶字段 id 排序:age 升序 桶1:000000_0 id---int id%3=0 95004 张立 男 19 IS 95007 易思玲 女 19 MA 95019 邢小丽 女 19 IS 95010 孔小涛 男 19 CS 95001 李勇 男 20 CS 95022 郑明 男 20 MA 95013 冯伟 男 21 CS 123 zss 男 34 IS
桶2:000001_0 id%3=1 95017 王风娟 女 18 IS 95011 包小柏 男 18 MA 95008 李娜 女 18 CS 95005 刘刚 男 18 MA 95002 刘晨 女 19 IS 95014 王小丽 女 19 CS 95020 赵钱 男 21 IS 1234 zss 男 34 IS
桶3:000002_0 id%3=2 95021 周二 男 17 MA 95015 王君 男 18 MA 95009 梦圆圆 女 18 MA 95018 王一 女 19 IS 95012 孙花 女 20 CS 95003 王敏 女 22 MA 95006 孙庆 男 23 CS
分桶的字段不是数值 name.hashCode%3
在hive的1.2版本中 向分桶表插入数据的时候 需要手动设定reducetask的个数的
1 2 3 4 5 6 7
In order to change the average load for a reducer (in bytes): set hive.exec.reducers.bytes.per.reducer=<number> 设置每一个reduvetask的吞吐量 In order to limit the maximum number of reducers: set hive.exec.reducers.max=<number> 设置的是reducetask的最大的个数 1009 In order to set a constant number of reducers: set mapreduce.job.reduces=<number> 设置reducetask个数的 -1 -1 根据实际需求 分配reducetask个数
hive> insert into table test > partition (age='25') > select id, name, tel > from wyp; ##################################################################### 这里输出了一堆Mapreduce任务信息。这里省略 ##################################################################### Total MapReduce CPU Time Spent: 1 seconds 310 msec OK Time taken: 19.125 seconds