hive學習第四章:DDL數據操作

目錄

 

一、數據導入

向表中裝在數據(Load)

通過查詢語句向表中插入數據(Insert)

查詢語句中創建表並加載數據(As Select)

創建表時通過Location指定加載數據路徑

import數據類型指定hive表中

二、數據導出

 insert導出

hadoop命令導出到本地

hive shell命令導出

export導出到hdfs上

 三、清除表中的數據(truncate)


正文

一、數據導入

  • 向表中裝在數據(Load)

  1. 語法
    load data [local] inpath '/opt/module/datas/student.txt' [overwrite] into table student [partition(partcoll=val1, ...)]
    
    /*
    1.load data:表示加載數據
    2.local:表示從本地加載數據到hive表,否則從hdfs加載數據到hive表
    3.inpath:表示加載數據的路徑
    4.overwrite:表示覆蓋表中已有的數據,否則表示追加
    5.into table:表示加載到哪張表
    6.student:表示具體的表
    */

     

  • 通過查詢語句向表中插入數據(Insert)

  1. 創建一張分區表
    create table student(id int, name string)
    partitioned by (month string) row format delimited fields
    terminated by '\t'

     

  2. 基本插入數據
    insert into table student partition(month='202004') values(1,'wangwu')

     

  3. 基本模式插入(根據單張表查詢結果)
    insert overwrite table student partition(month='202004') select id,name from student where month='202004'

     

  4. 多插入模式(根據多張表查詢結果)
    from student 
    insert overwrite table student partition(month='202004') select id, name where month='202004'
    insert overwrite table student partition(month='202005') select id, name where month='202005'
    

     

  • 查詢語句中創建表並加載數據(As Select)

    //根據查詢結果創建表
    create table if not exists student3
    as select id, name from student

     

  • 創建表時通過Location指定加載數據路徑

  1. 創建表,並指定在hdfs上的位置
    create table if not exists student5(
    id int, name string
    )
    row format delimited fields terminated by '\t'
    location '/user/hive/warehouse/student5'

     

  2. 上傳數據到hdfs上
    dfs -put /opt/module/datas/student.txt /user/hive/warehouse/student5

     

  • import數據類型指定hive表中

    import table student2 partition(month='202004') from '/user/hive/warehouse/export/student'

    注意:先用export導出後,再將數據導入

二、數據導出

  •  insert導出

  1. 將查詢的結果導出到本地
    insert overwrite local directory '/opt/module/datas/export/student' select * from student

     

  2. 將查詢的結果格式化導出到本地
    insert overwrite local directory '/opt/module/datas/export/student1' 
    ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' select * from student

     

  3. 將查詢的結果導出到hdfs上(沒有local)
    insert overwrite directory '/user/qx/student2'
    ROW FORMAT DELIMITED FIELD TERMINATED BY '\t'
    select * from student

     

  • hadoop命令導出到本地

    dfs -get /user/hive/warehouse/student/month=202004/000000_0 /opt/module/datas/export/student3.txt

     

  • hive shell命令導出

    bin/hive -e 'select * from default.student;' > /opt/module/datas/export/student4.txt

     

  • export導出到hdfs上

    export table default.student to 'user/hive/warehouse/export/student'

     

 三、清除表中的數據(truncate)

truncate table student

注意:truncate只能刪除管理表,不能刪除外部表中數據

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章