Hive之DDL数据操作
生活随笔
收集整理的這篇文章主要介紹了
Hive之DDL数据操作
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Hive之DDL數據操作
目錄
1. 數據導入
1. 向表中裝載數據(Load)
hive> load data [local] inpath ‘/opt/module/datas/student.txt’ [overwrite] into table student [partition (partcol1=val1,…)];
(1)load data:表示加載數據
(2)local:表示從本地加載數據到hive表(復制);否則從HDFS加載數據到hive表(移動)
(3)inpath:表示加載數據的路徑
(4)overwrite into:表示覆蓋表中已有數據,否則表示追加
(5)into table:表示加載到哪張表
(6)student:表示具體的表
(7)partition:表示上傳到指定分區
2.實操案例
(0)創建一張表
(1)加載本地文件到hive
hive (default)> load data local inpath '/opt/module/datas/student.txt' into table default.student;(2)加載HDFS文件到hive中
上傳文件到HDFS
加載HDFS上數據
hive (default)> load data inpath '/user/centos/hive/student.txt' into table default.student;(3)加載數據覆蓋表中已有的數據
上傳文件到HDFS
加載數據覆蓋表中已有的數據
hive (default)> load data inpath '/user/centos/hive/student.txt' overwrite into table default.student;2 通過查詢語句向表中插入數據(Insert)
3. 查詢語句中創建表并加載數據(As Select)
詳見4.5.1章創建表。
根據查詢結果創建表(查詢的結果會添加到新創建的表中)
4. 創建表時通過Location指定加載數據路徑
1.創建表,并指定在hdfs上的位置
hive (default)> create table if not exists student5(id int, name string)row format delimited fields terminated by '\t'location '/user/hive/warehouse/student5';2.上傳數據到hdfs上
hive (default)> dfs -put /opt/module/datas/student.txt /user/hive/warehouse/student5;3.查詢數據
hive (default)> select * from student5;5.1.5 Import數據到指定Hive表中
注意:先用export導出后,再將數據導入。
2. 數據導出
1. Insert導出
2. Hadoop命令導出到本地
hive (default)> dfs -get /user/hive/warehouse/student/month=201709/000000_0 /opt/module/datas/export/student3.txt;3. Hive Shell 命令導出
基本語法:(hive -f/-e 執行語句或者腳本 > file)
[centos@hadoop102 hive]$ bin/hive -e 'select * from default.student;' >/opt/module/datas/export/student4.txt;4. Export導出到HDFS上
(defahiveult)>export table default.student to'/user/hive/warehouse/export/student';5. Sqoop導出
后面再學習。
3. 清除表中數據
注意:Truncate只能刪除管理表,不能刪除外部表中數據
hive (default)> truncate table student;總結
以上是生活随笔為你收集整理的Hive之DDL数据操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Hive常见面试问题(持续更新)
- 下一篇: Hive查询操作