MySQL的密碼是:123456
1、hive創建標准表(以后均可以按照這樣創建):
create [external] table [if not exists] records
(year STRING [comment "year備注消息"],
temperature INT [comment "temperature 備注消息"],
quality INT [comment "quality 備注消息"]
)
[comment "表records備注消息"]
[partitioned by (col_name,col_name, ... )]
[clustered by (col_name,....) into nums buckets]
[ sorted by (col_name [ASC | DESC],...) into num_buckets BUCKETS ]
row format delimited fields terminated by ‘\t’;
[stored as file_format]
[location hdfs_path]
解析:第一行聲明一個records表,包括三列(3個字段):year,temperature,quality。分別指定數據類型為:string,int,int。//第一行解析與MySQL創建表一樣
第二行是HiveQL所特有,數據文件中每一行都是由制表符分隔的文本。若將文本導入由本地或者hdfs導入hive時必須按照此格式即每行三個字段,每個字段由制表符分隔,每行由換行符分隔
注意:row format delimited 分隔符設置的開始語句
fields terminated by ‘\t’ 設置字段與字段之間的分隔符,此時使用制表符分隔
collection items terminated by "." 設置一個復雜類型(array,struct)字段中的各個item之間的分隔符,此時用點表示
map keys terminated by "." 設置復雜類型map字段的key和value之間的分隔符,此時用電表示
lines terminated by 設置行與行之間的分隔符
例如:row format delimited
fields terminated by '\t'
collection items terminated by ','
map keys terminated by ':'
lines terminated by '\n';
OK
Time taken: 0.287 seconds
ROW FORMAT DELIMITED 必須在其它分隔設置之前,也就是分隔符設置語句的最前
LINES TERMINATED BY必須在其它分隔設置之后,也就是分隔符設置語句的最后,否則會報錯
PS:表名records在hdfs中其實是目錄名;
2、將數據文件導入hive
load data local inpath “/home/hdc/sample.txt”
overwrite into table records;
解析:將本地文件sample導入records表中,其中overwrite可加可不加,如果加表示hive刪除表對應目錄中已有的所有文件,再將sample.txt文件送到records下;
若不加表示hive簡單的把sample.txt文件加入目錄(若此目錄有同名的文件則自動修改文件存儲)。
3、--將HDFS中 /input/student.txt 導入到t3(此種方法是移動即student.txt文件將在t3文件夾下)
load data inpath '/input/student.txt' overwrite into table t3;
4、將數據導入分區表:
load data local inpath '/home/hdc/data_1.txt' into table partition_table partition(gender='M');
5、導出數據本地或者hdfs上:
insert overwrite local directory ‘/home/hdc’ //除掉local,把路徑改成hdfs上的路徑就可以導入到hdfs上
select * from student
6、通過查詢語句向表中插入輸入(從另外一個中的數據復制到另外一個表,兩個表的字段要相同,並且其分區字段也相同即兩表結構要一樣)
靜態插入:
insert into table employee //insert overwrite table emplyee
partition(country='US',state='OR')
select * from staged_emplyee se
where se.cnty='US' and se.st='OR';
動態插入(以select語句后面的兩個字段為分區字段):
insert into table employee
partition(country,state)
select ....,se.cnty,se.st
from staged_emplyee se;
可以混合使用靜態插入和動態插入(靜態分區間鍵必須在動態分區鍵的前面)
insert overwrite table emplyee
partition(country='US',state)
select ....,se.cnty,se.st
from staged_emplyee se
where se.cnty='US'
7.創建外部表及其指定數據位置
create table stu(name string,score int)
row format delimited fields terminated by '\t'
location '/data'(外部表關聯hdfs上的文件)
8.外部表與內部表的關系
1)未被external修飾的是內部表【managed table】,被external修飾的為外部表【external table】。
2)內部表數據由Hive自身管理,外部表數據由HDFS管理。
3)內部表數據存儲在hive.metastore.warehouse.dir【默認:/user/hive/warehouse】,外部表數據存儲位置由用戶自己決定。
4)刪除內部表會直接刪除元數據【metadata】及存儲數據,刪除外部表僅僅刪除元數據,HDFS上的文件不會被刪除。
5)對內部表的修改會直接同步到元數據,而對外部表的表結構和分區進行修改,則需要修改【MSCK REPAIR TABLE table_name】。