hive的指令操作及內外臨時表的創建


2、數據庫: OLTP //online transaction process ,在線事務處理


3、drop databases  if exists  mybase //刪除數據庫


4、show tables //顯示錶


5、create  database  mybase //mysql 創建庫


6、create table test(id int ,name varchar(20)); //創建列表


7、select id from test        //查看錶中的內容


8、兩張表合成:select a.*,b.* form tbls a,columms_v2 b where a.tbl_id = b.tbl_id 


9、show databases; //顯示數據庫


10、在/soft/hive/bin/下執行:hive 後再執行:create database if not exists mybase;


11、用自己的庫的話就執行:use mybase ;   ------>記得加分號

show tables; ——————》顯示錶信息


12、創建表:create table test(id int , name varchar(20));


13、查看錶的結構:desc test


14、往數據庫裏面放數據:insert into test(id,name)values(1,'tom');


15、select * form test      //查看錶中所有的內容




具體流程是:創建庫:create database mysbase ------>用那個庫use mybase  ------->創建表create table test(id int ,name varchar(20))




-----------------------------------------------------


表的填寫信息及創建語法:

1、create  table if not exists employee(edi int ,nam String,salary String, destination String)


2、comment ‘employee details’



4、 row format delimited  fields terminated by ‘\t’ //這行的格式分隔如何 :(1  tom  12 )


5、lines terminated by ‘\n’ //換行1  tom  12

     2  tod  13


6、stored as textfile;   //存儲爲文本文件



dfs -lsr / ; //查看它們的目錄結構


-------------------------------------------------------------------------------------------

desc[ribe] database mybase ; //查看數據庫信息

alter database mybase set dbproperties ('created'='xpc'); //修改數據庫,增加數據庫屬性

desc database extended mybase ; //顯示數據庫擴展信息,不能格式化輸出

desc extended default.test0 ; //顯示錶擴展信息

desc formatted default.test0 ; //顯示錶格式化信息

desc extended test ; //顯示錶的擴展信息

create database mybase location '/x/x/x/x' //指定數據庫存放hdfs位置

create table default.test0 like mybase.test2 ; //複製表結構


load data local ... //上傳本地文件到hdfs

load data '/x/x/x' into table xx //移動hdfs文件系統上的數據文件。

insert into mybase.test2 select * from default.test0 where id > 1204 ; //複製表數據


create table mybase.test3 as select * from default.test0 ; //複製表(表結構 + 數據)

select all id,name from test2 ; //查詢所有記錄(不去重)

select distinct id,name from test2 ; //查詢所有記錄(去重)


--------------------------------------------------------

拷貝一張已經存在的表模式

create table if ont exists mydb.employees2 like mydb.employees


-------------------------------------------


如何從本地插進文本文件:

load data local inpath '/home/user/sample.txt' overwrite into table tset;

----------------------------------------------------------

外部表的創建步驟:

1、create database mybase ;  //創建庫

2、use  mybase  ;          //用庫

3、create external table etest(id int ,name string,age int) row format delimited fields terminated by '\t' stored as textfile ;//創建外部表


4、查看錶的參數: desc formatted etest ; 查看有 多少張表:show tables  看錶的結構select * from etest ;


5、創建臨時表create tempporary table temp(id int ,name string,age int) row format delimited fields terminated by '\t' stored as textfile ;


6、CTAS:create table as select 


7、truncate  :刪除所有的行,只能用於內部的表


8、修改表分隔符:alter table table_name set serdeproperties (‘field,delimi’= ',')


9、修改表的位置:alter table 表名 set location 'hdfs:/xxx/x/表名'



分區表

------------

[手動分區--靜態分區]

1.預先定義分區列和存儲數據到表的子目錄下。

2.創建分區表

create table xxx(...) partitioned by (year int ,month int) row format ...;


3.給分區表添加多個分區(手動分區)

alter table partest add partition (year=2016,month=4) partition (year=2016,month=5);

4.顯示指定表的分區

show partitions partest ;

5.刪除分區

ALTER TABLE partest DROP IF EXISTS PARTITION (year=2014, month=11);

6.加載數據到指定的分區目錄下。

LOAD DATA LOCAL INPATH '..sample.txt' OVERWRITE INTO TABLE partest PARTITION (year=2016, month=4);

單是查詢2016年4月份的數據:select * from  表名 where year=2016 and month = 4 ;


7.啓動動態分區(默認是開啓的)

SET hive.exec.dynamic.partition=true; //啓動動態分區.

//默認是strict,是嚴格模式,至少要指定一個分區類,通過如下指令關閉嚴格模式。

SET hive.exec.dynamic.partition.mode=nonstrict; //設置分區模式,非嚴格.

8.測試動態分區

insert into table partest partition(year,month) select id,name,age,year,month from test ;



























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