更改包含数据的Hive外部表中的列名

时间:2019-03-25 02:44:45

标签: hadoop hive hiveql

我有一列insert_process_id,我想将其重命名为process_id。该外部表为实木复合地板文件格式。请告知如何重命名此列。

2 个答案:

答案 0 :(得分:0)

您可以使用以下语法

ALTER TABLE table_name [PARTITION partition_spec] CHANGE [COLUMN] col_old_name col_new_name column_type
  [COMMENT col_comment] [FIRST|AFTER column_name] [CASCADE|RESTRICT];

由于这是一个外部表,因此您可以删除该表并通过特定的更改重新创建。

答案 1 :(得分:0)

您可以如下更改列名。

语法:

ALTER TABLE name CHANGE column_name col_spec .. 

示例:

  1. 我创建了如下表。

    CREATE EXTERNAL Table IF NOT EXISTS Patient_external(
    PatientID int, Name String, City String, Number String )
    COMMENT 'Data about patient from Apollo Hospital'
    ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
    STORED AS PARQUET 
    LOCATION '/user/training/external/data' ;
    
  2. 将数据加载到表中

    LOAD DATA LOCAL INPATH 
    '/home/cloudera/Desktop/HiveTraining/Dataset/patient_details' INTO TABLE Patient_external; 
    
  3. 描述表格

    describe formatted Patient_external;
    
    
    
    col_name                data_type               comment             
    
    **patientid**               int                                         
    name                    string                                      
    city                    string                                      
    number                  string                                      
    
    Detailed Table Information       
     Database:              default                  
     Owner:                 cloudera                 
     CreateTime:            Mon Mar 25 10:57:23 PDT 2019     
     LastAccessTime:        UNKNOWN                  
     Protect Mode:          None                     
     Retention:             0                        
     Location:          
      hdfs://quickstart.cloudera:8020/user/training/external/data    
     Table Type:            EXTERNAL_TABLE           
     Table Parameters:       
            COLUMN_STATS_ACCURATE   false               
            EXTERNAL                TRUE                
            comment                 Data about patient from Apollo Hospital
            numFiles                0                   
           numRows              -1                  
           rawDataSize          -1                  
         totalSize              0                   
    transient_lastDdlTime   1553536643          
    
      Storage Information        
         SerDe Library:          
          org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe    
         InputFormat:            
              org.apache.hadoop.hive.ql.io.parquet.MapredParquetInputFormat  
         OutputFormat:           
             org.apache.hadoop.hive.ql.io.parquet.MapredParquetOutputFormat  
    
  4. 将列名从PatientID更改为Patient_ID

         Alter Table Patient_external change PatientID Patient_ID Int; 
    
  5. 现在描述表格

        describe formatted Patient_external;
    
     col_name               data_type               comment             
    
    **patient_id**              int                                         
    name                    string                                      
    city                    string                                      
    number                  string                                      
    
    Detailed Table Information       
     Database:              default                  
     Owner:                 cloudera                 
     CreateTime:            Mon Mar 25 10:57:23 PDT 2019     
     LastAccessTime:        UNKNOWN                  
     Protect Mode:          None                     
     Retention:             0                        
     Location:          
      hdfs://quickstart.cloudera:8020/user/training/external/data    
     Table Type:            EXTERNAL_TABLE           
     Table Parameters:       
            COLUMN_STATS_ACCURATE   false               
            EXTERNAL                TRUE                
            comment                 Data about patient from Apollo Hospital
            numFiles                0                   
           numRows              -1                  
           rawDataSize          -1                  
         totalSize              0                   
    transient_lastDdlTime   1553536643          
    
      Storage Information        
         SerDe Library:          
          org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe    
         InputFormat:            
              org.apache.hadoop.hive.ql.io.parquet.MapredParquetInputFormat  
         OutputFormat:           
             org.apache.hadoop.hive.ql.io.parquet.MapredParquetOutputFormat