1. 程式人生 > >hive 命令整理

hive 命令整理

  1. 啟動
hive
  1. 資料庫操作
create database database_name; -- 新建資料庫
creat database if not exists -- 新建資料庫 database_name;
show databases; -- 檢視資料庫
show databases like 'h.*'; -- 檢視資料庫
use default;    --使用哪個資料庫
create table test3 like test2; --只是複製了表結構,並不會複製內容
create table test2 as select name,addr from test1;
--複製表結構的同時,把內容也複製過來了,需要執行mapreduce show tables; --檢視該資料庫中的所有表 show tables ‘*t*’; --支援模糊查詢 SHOW TABLES IN DbName; --檢視指定資料庫中的所有表 describe formatted(可選) tab_name; --查看錶的結構及表的路徑 describe database database_name; --檢視資料庫的描述及路徑 creat database database_name location '路徑'; --修改資料庫的路徑 drop database if
exists database_name;
--刪除空的資料庫 drop database if exists database_name cascade; --先刪除資料庫中的表再刪除資料庫 show partitions t1; --查看錶有哪些分割槽 alter table table_name rename to another_name; --修改表名 drop table t1 CASCADE(可選,忽略錯誤); --刪除表t1 drop table if exists CASCADE --刪除資料庫的時候,不允許刪除有資料的資料庫,如果資料庫裡面有資料則會報錯。如果要忽略這些內容,則在後面增加CASCADE
關鍵字,則忽略報錯,刪除資料庫。 t1;
--如果存在表t1,刪除表t1 load data inpath '/root/inner_table.dat' into table t1; --移動hdfs中資料到t1表中 load data local inpath '/root/inner_table.dat' into table t1; --上傳本地資料到hdfs中 !ls; --查詢當前linux資料夾下的檔案 dfs -ls /; --查詢當前hdfs檔案系統下 '/'目錄下的檔案 set hive.cli.print.current.db=true; --顯示地展示當前使用的資料庫 set hive.cli.print.header=true; --Hive顯示列頭
  1. 匯入
向管理表中載入資料:
Hive沒有行級別的插入、刪除、更新的操作,那麼往表裡面裝資料的唯一的途徑就是使用一種“大量”的資料裝載操作,或者僅僅將檔案寫入到正確的目錄下面。
overwrite關鍵字:
    load data local inpath '${env:HOME}/目錄'
    overwrite(可選) into table table_name
    partition (分割槽);
-- 如果沒有使用overwrite,則會再拷貝一份資料,不會覆蓋原來的資料。
  1. 匯出
hadoop fs -cp source_path target_path
insert……directory……
e.g insert overwrite local directory '/tmp/目錄'     -- 這裡指定的路徑也可以是全URL路徑
  1. 退出
quit;     --退出hive
exit;    --exit會影響之前的使用,所以需要下一句kill掉hadoop的程序
hadoop job -kill jobid
  1. 檔案執行hive SQL
-- 控制檯執行
hive -f sql_path;
e.g hive -f /path/to/file/xxxx.hql;
-- hive shell 執行
source sql_path;
e.g source /path/to/file/test.sql;
-- 一次使用命令
hive -e "SQL語句";
e.g.  $ hive -e "select * from mytable limit 3";
  1. 建表語句
CREATE [TEMPORARY] [EXTERNAL] TABLE [IF NOT EXISTS] [db_name.]table_name    -- (Note: TEMPORARY available in Hive 0.14.0 and later)
  [(col_name data_type [COMMENT col_comment], ...)]
  [COMMENT table_comment]
  [PARTITIONED BY (col_name data_type [COMMENT col_comment], ...)]
  [CLUSTERED BY (col_name, col_name, ...) [SORTED BY (col_name [ASC|DESC], ...)] INTO num_buckets BUCKETS]
  [SKEWED BY (col_name, col_name, ...)                  -- (Note: Available in Hive 0.10.0 and later)]
     ON ((col_value, col_value, ...), (col_value, col_value, ...), ...)
     [STORED AS DIRECTORIES]
  [
   [ROW FORMAT row_format] 
   [STORED AS file_format]
     | STORED BY 'storage.handler.class.name' [WITH SERDEPROPERTIES (...)]  -- (Note: Available in Hive 0.6.0 and later)
  ]
  [LOCATION hdfs_path]
  [TBLPROPERTIES (property_name=property_value, ...)]   -- (Note: Available in Hive 0.6.0 and later)
  [AS select_statement];   -- (Note: Available in Hive 0.5.0 and later; not supported for external tables)
  1. 查詢表資料
hive> select * from employees;
OK
tony    1338    ["a1","a2","a3"]        {"k1":1.0,"k2":2.0,"k3":3.0}    {"street":"s1","city":"s2","state":"s3","zip":4}
mark    5453    ["a4","a5","a6"]        {"k4":4.0,"k5":5.0,"k6":6.0}    {"street":"s4","city":"s5","state":"s6","zip":6}
ivy     323     ["a7","a8","a9"]        {"k7":7.0,"k8":8.0,"k9":9.0}    {"street":"s7","city":"s8","state":"s9","zip":9}
Time taken: 10.204 seconds, Fetched: 3 row(s)

查樹組
hive> select subordinates[1]  from employees;
Total MapReduce CPU Time Spent: 2 seconds 740 msec
OK
a2
a5
a8
查map
hive> select deductions["k2"]  from employees;

OK
2.0
NULL
NULL
Time taken: 75.812 seconds, Fetched: 3 row(s)

查結構體
hive> select address.city  from employees;
Total MapReduce CPU Time Spent: 2 seconds 200 msec
OK
s2
s5
s8
Time taken: 75.311 seconds, Fetched: 3 row(s)

select * 不執行mapreduce,只進行一個本地的查詢。
而select 某個欄位 生成一個job,執行mapreduce。

  1. 執行
nohup hive -f insert.sql >log.log &