1 什麼是hive?mysql
支持SQL語義的大規模數據分析,離線/數據倉庫應用,工具sql
HIVE是創建在hadoop上的數據倉庫基礎構架app
2 hive的元數據存儲在傳統的RDBMS中,通常爲mysql工具
緣由:訪問這些metadata,咱們想要「很低的延遲」,而存在hdfs中是沒法知足的oop
元數據都是在HQL語句編譯的時候,被生成一個xml文件存儲在hdfs中,而後運行mapreduce時傳遞給mapper和reducerspa
HIVE數據類型:orm
tinyintxml
smallintip
inthadoop
bigint
string
boolean
float
double
array / map / struct
3 hive表分爲4類
內部表:table -----------------------------------一個相應的目錄
eg: create table liming(id int, name string) row format delimited fields terminated by ',' stored as textfile;
外部表:external table-------------------------建表時指定一個指向實際數據的路徑location
分區表:partition table------------------------對應表下的一個目錄,全部的partiton的數據都存儲在對應的目錄中
eg: create table c_02(id int, ip string) partitioned by(dt string) row format delimited fields terminated by ',' stored as textfile;
桶表 : bucket table--------------------------對指定的列計算hash,根據hash值切分數據
4 hive導入數據
load data local inpath '/home/dwapp/liming/a.txt' overwrite into table liming;
load data local inpath '/home/dwapp/liming/a.txt' overwrite into table liming partition(dt='2014-05-10');
load data local inpath '/jrdata/zhangxia/tmp.csv' overwrite into tabl dev.tmp_jr_edm;
5 hive 分區操做
添加分區
alter table c_02 add partition(dt='20140202') locatition '/group/alid/hive/part20140202' partition(dt='20140303') location '/group/alid/hive/part20140202'
刪除分區
alter table c_02 drop partition(dt='20140202');
查詢指定分區數據
select * from c_02 where dt='20140202';
查詢指定分區數據追加到某文件
hive -e "select * from c_02 where dt='20140202' ;" > /jrdata/zx/c_02.txt
修改表名、字段
alter table old_tablename rename to new_tablename;
alter table table_name change col_old col_new col_type;
alter table table_name replace columns(in_data string);
添加字段
alter table table_name add columns(col_name data_type);
6 show 操做
查看錶名: show tables;
show tables 'page.*';
show tables '.*view';
查看錶中全部partition
show partitions page_view;
查看錶結構:describe page_view;
查看有限行內容:select a.id from table_name limit 3;
7 insert
插入到表
insert overwrite table table_name select a.id from table_name group by a.id;
導入文件到本地
insert overwrite local directory '/tmp/local_out' select * from table_name;
導出文件到hdfs
insert overwrite directory '/user/admin/20140617/a' select * from c_02 where dt='20140202';
8 drop
刪除一個內部表的同時是刪除表的元數據和數據
刪除一個外部表,只刪除元數據而保留數據