成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

你會Hive表的基本操作嗎?

大數據
create table語句遵從sql語法習慣,只不過Hive的語法更靈活。例如,可以定義表的數據文件存儲位置,使用的存儲格式等。

[[381808]]

本文轉載自微信公眾號「Java大數據與數據倉庫」,作者柯同學。轉載本文請聯系Java大數據與數據倉庫公眾號。   

1. 創建表

create table語句遵從sql語法習慣,只不過Hive的語法更靈活。例如,可以定義表的數據文件存儲位置,使用的存儲格式等。

  1. create table if not exists test.user1( 
  2. name string comment 'name'
  3. salary float comment 'salary'
  4. address struct<country:string, city:string> comment 'home address' 
  5. comment 'description of the table' 
  6. partitioned by (age int
  7. row format delimited fields terminated by '\t' 
  8. stored as orc; 

沒有指定external關鍵字,則為管理表,跟mysql一樣,if not exists如果表存在則不做操作,否則則新建表。comment可以為其做注釋,分區為age年齡,列之間分隔符是\t,存儲格式為列式存儲orc,存儲位置為默認位置,即參數hive.metastore.warehouse.dir(默認:/user/hive/warehouse)指定的hdfs目錄。

2. 拷貝表

使用like可以拷貝一張跟原表結構一樣的空表,里面是沒有數據的。

  1. create table if not exists test.user2 like test.user1; 

3. 查看表結構

通過desc [可選參數] tableName命令查看表結構,可以看出拷貝的表test.user1與原表test.user1的表結構是一樣的。

  1. hive> desc test.user2; 
  2. OK 
  3. name                    string                  name                 
  4. salary                  float                   salary               
  5. address                 struct<country:string,city:string>  home address         
  6. age                     int                                          
  7.  
  8. # Partition Information          
  9. # col_name                data_type               comment              
  10.  
  11. age                     int          

也可以加formatted,可以看到更加詳細和冗長的輸出信息。

  1. hive> desc formatted test.user2; 
  2. OK 
  3. # col_name                data_type               comment              
  4.  
  5. name                    string                  name                 
  6. salary                  float                   salary               
  7. address                 struct<country:string,city:string>  home address         
  8.  
  9. # Partition Information          
  10. # col_name                data_type               comment              
  11.  
  12. age                     int                                          
  13.  
  14. # Detailed Table Information          
  15. Database:               test                      
  16. Owner:                  hdfs                      
  17. CreateTime:             Mon Dec 21 16:37:57 CST 2020      
  18. LastAccessTime:         UNKNOWN                   
  19. Retention:              0                         
  20. Location:               hdfs://nameservice2/user/hive/warehouse/test.db/user2     
  21. Table Type:             MANAGED_TABLE             
  22. Table Parameters:          
  23.     COLUMN_STATS_ACCURATE   {\"BASIC_STATS\":\"true\"
  24.     numFiles                0                    
  25.     numPartitions           0                    
  26.     numRows                 0                    
  27.     rawDataSize             0                    
  28.     totalSize               0                    
  29.     transient_lastDdlTime   1608539877           
  30.  
  31. # Storage Information          
  32. SerDe Library:          org.apache.hadoop.hive.ql.io.orc.OrcSerde     
  33. InputFormat:            org.apache.hadoop.hive.ql.io.orc.OrcInputFormat   
  34. OutputFormat:           org.apache.hadoop.hive.ql.io.orc.OrcOutputFormat      
  35. Compressed:             No                        
  36. Num Buckets:            -1                        
  37. Bucket Columns:         []                        
  38. Sort Columns:           []                        
  39. Storage Desc Params:          
  40.     field.delim             \t                   
  41.     serialization.format    \t            

4. 刪除表

這跟sql中刪除命令drop table是一樣的:

  1. drop table if exists table_name; 

對于管理表(內部表),直接把表徹底刪除了;對于外部表,還需要刪除對應的hdfs文件才會徹底將這張表刪除掉,為了安全,通常hadoop集群是開啟回收站功能的,刪除外表表的數據就在回收站,后面如果想恢復也是可以恢復的,直接從回收站mv到hive對應目錄即可。

5. 修改表

大多數表屬性可以通過alter table來修改。

5.1 表重命名

  1. alter table test.user1 rename to test.user3; 

5.2 增、修、刪分區

增加分區使用命令alter table table_name add partition(...) location hdfs_path

  1. alter table test.user2 add if not exists 
  2. partition (age = 101) location '/user/hive/warehouse/test.db/user2/part-0000101' 
  3. partition (age = 102) location '/user/hive/warehouse/test.db/user2/part-0000102' 

修改分區也是使用alter table ... set ...命令

  1. alter table test.user2 partition (age = 101) set location '/user/hive/warehouse/test.db/user2/part-0000110' 

刪除分區命令格式是alter table tableName drop if exists partition(...)

  1. alter table test.user2 drop if exists partition(age = 101) 

5.3 修改列信息

可以對某個字段進行重命名,并修改位置、類型或者注釋:

修改前:

  1. hive> desc user_log; 
  2. OK 
  3. userid                  string                                       
  4. time                    string                                       
  5. url                     string             

修改列名time為times,并且使用after把位置放到url之后,本來是在之前的。

  1. alter table test.user_log 
  2. change column time times string 
  3. comment 'salaries' 
  4. after url; 

再來看表結構:

  1. hive> desc user_log; 
  2. OK 
  3. userid                  string                                       
  4. url                     string                                       
  5. times                   string                  salaries             

time -> times,位置在url之后。

5.4 增加列

hive也是可以添加列的:

  1. alter table test.user2 add columns ( 
  2. birth date comment '生日'
  3. hobby string comment '愛好' 
  4. ); 

5.5 刪除列

刪除列不是指定列刪除,需要把原有所有列寫一遍,要刪除的列排除掉即可:

  1. hive> desc test.user3; 
  2. OK 
  3. name                    string                  name                 
  4. salary                  float                   salary               
  5. address                 struct<country:string,city:string>  home address         
  6. age                     int                                          
  7.  
  8. # Partition Information          
  9. # col_name                data_type               comment              
  10.  
  11. age                     int               

如果要刪除列salary,只需要這樣寫:

  1. alter table test.user3 replace columns( 
  2. name string, 
  3. address struct<country:string,city:string> 
  4. ); 

這里會報錯:

  1. FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. Replacing columns cannot drop columns for table test.user3. SerDe may be incompatible 

這張test.user3表是orc格式的,不支持刪除,如果是textfile格式,上面這種replace寫法是可以刪除列的。通常情況下不會輕易去刪除列的,增加列倒是常見。

5.6 修改表的屬性

可以增加附加的表屬性,或者修改屬性,但是無法刪除屬性:

  1. alter table tableName set tblproperties( 
  2.     'key' = 'value' 
  3. ); 

舉例:這里新建一張表:

  1. create table t8(time string,country string,province string,city string) 
  2. row format delimited fields terminated by '#'  
  3. lines terminated by '\n'  
  4. stored as textfile; 

這條語句將t8表中的字段分隔符'#'修改成'\t';

  1. alter table t8 set serdepropertyes('field.delim'='\t'); 

 

責任編輯:武曉燕 來源: Java大數據與數據倉庫
相關推薦

2019-05-07 15:49:27

AI人工智能藝術

2010-07-13 10:40:30

唐駿

2021-08-19 15:36:09

數據備份存儲備份策略

2010-09-03 12:20:54

數據庫

2021-06-04 09:09:05

FlutterFuchsia操作系統

2020-10-26 15:18:01

大數據APP軟件

2024-03-29 12:50:00

項目分層模型

2021-04-14 06:53:52

C# 修飾符 Public

2021-04-16 15:02:11

CAP理論分布式

2022-03-15 08:36:46

遞歸查詢SQL

2024-02-22 08:31:26

數據恢復工具MySQL回滾SQL

2012-06-20 15:01:25

iOS開發

2023-02-27 10:45:16

2012-06-20 10:47:25

Team Leader

2012-04-24 09:54:14

WiFi

2019-05-29 13:59:03

GitHub開源搜索功能

2024-05-14 08:19:54

2021-03-10 18:07:58

協議調試 Modbus

2023-12-04 07:09:53

函數遞歸python
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 激情一区二区三区 | 国产精品一区一区 | 中文字幕在线一区 | 激情六月丁香婷婷 | 日韩成人在线免费观看 | 九色porny自拍视频 | 久久久男人的天堂 | 天天色天天射天天干 | 麻豆天堂| 国产中文视频 | 成人黄色电影免费 | 亚洲毛片在线观看 | 欧美群妇大交群中文字幕 | 天天干夜夜操 | 黄色大片免费网站 | 亚洲午夜精品久久久久久app | 亚洲视频欧美视频 | 欧美日韩在线视频一区 | 在线一级片| 久久久久网站 | 日韩激情视频一区 | 久久久久一区 | 国产午夜精品理论片a大结局 | 天堂一区二区三区 | 国产成人精品一区二区三 | 黄免费在线 | 久久久久久免费毛片精品 | 久久久久亚洲精品 | 99色在线视频 | 中文字幕精品一区 | 亚洲日本一区二区三区四区 | 日韩成人高清在线 | 亚洲一区不卡 | 一级一级一级毛片 | 久久亚洲综合 | 国产视频第一页 | 免费观看视频www | 91成人精品| 精品久久不卡 | 久久久久久国产 | 91欧美 |