hive學習3(hive基本操作)


hive基本操作

hive的數據類型

1)基本數據類型

  • TINYINT,SMALLINT,INT,BIGINT
  • FLOAT/DOUBLE
  • BOOLEAN
  • STRING

2)復合類型

  • ARRAY:一組有序字段。字段的類型必須相同,例Array(1,2)
  • MAP:一組無序的鍵/值對。鍵的類型必須是原子的,值可以是任何類型,同一個映射的鍵的類型必須相同,值得類型也必須相同。例Map('a',1,'b',2)
  • STRUCT:一組命名的字段。字段類型可以不同。例Struct('a',1,1,0)

hive 默認分隔符

 \n :分隔行【lines terminated by '\t'】
 ^A :分隔字段,顯示編碼用\001【fields terminated by '\001'】
 ^B :分隔復合類型的元素,顯示編碼用\002【collectionitems terminated by '\002'】
 ^C :分隔map元素的key和value,顯示編碼用\003【map keys terminated by '\003'】

DDL操作

Create/Drop/Alter/Use Database

創建數據庫【Create Database】

CREATE (DATABASE|SCHEMA) [IF NOT EXISTS] database_name
  [COMMENT database_comment]
  [LOCATION hdfs_path]
  [WITH DBPROPERTIES (property_name=property_value, ...)];

實例

hive> create database if not exists wujiadong
    > comment 'the first database'
    > location '/hive_test'
    > with dbproperties('creator'='wujiadong','data'='2016-11-13')

查看所有數據庫【show databases】

hive> show databases;
hive> show databases like "w.*";  #正則匹配

刪除數據庫【Drop Database】

DROP (DATABASE|SCHEMA) [IF EXISTS] database_name [RESTRICT|CASCADE];

實例

hive> drop database wujiadong1;
hive> drop database if exists wujiadong1 cascade; #刪除代表的數據庫

修改數據庫【Alter Database】

ALTER (DATABASE|SCHEMA) database_name SET DBPROPERTIES (property_name=property_value, ...);

ALTER (DATABASE|SCHEMA) database_name SET OWNER [USER|ROLE] user_or_role; 

實例

hive> alter database  wujiadong set dbproperties ('edit_by'='wjd'); 修改數據庫屬性,但不能刪除或重置

使用數據庫【Use Database】

USE database_name;
USE DEFAULT;

實例

hive> use wujiadong;
hive> use default;

顯示某個數據庫信息【desc database】

hive> desc database wujiadong;

Create/Drop/Truncate Table

創建表【create table】

CREATE [EXTERNAL] TABLE [IF NOT EXISTS] table_name 
   [(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] 
   [ROW FORMAT row_format] 
   [STORED AS file_format] 
   [LOCATION hdfs_path]

注釋

  • CREATE TABLE 創建一個指定名字的表。如果相同名字的表已經存在,則拋出異常;用戶可以用 IF NOT EXISTS 選項來忽略這個異常
  • EXTERNAL關鍵字可以讓用戶創建一個外部表,在建表的同時指定一個指向實際數據的路徑(LOCATION),Hive 創建內部表時,會將數據移動到數據倉庫指向的路徑;若創建外部表,僅記錄數據所在的路徑,不對數據的位置做任何改變。在刪除表的時候,內部表的元數據和數據會被一起刪除,而外部表只刪除元數據,不刪除數據
  • LIKE 允許用戶復制現有的表結構,但是不復制數據
ROW FORMAT 
DELIMITED [FIELDS TERMINATED BY char] [COLLECTION ITEMS TERMINATED BY char] 
        [MAP KEYS TERMINATED BY char] [LINES TERMINATED BY char] 
   | SERDE serde_name [WITH SERDEPROPERTIES (property_name=property_value, property_name=property_value, ...)]
  • 用戶在建表的時候可以自定義 SerDe 或者使用自帶的 SerDe。如果沒有指定 ROW FORMAT 或者 ROW FORMAT DELIMITED,將會使用自帶的 SerDe。在建表的時候,用戶還需要為表指定列,用戶在指定表的列的同時也會指定自定義的 SerDe,Hive通過 SerDe 確定表的具體的列的數據
STORED AS 
SEQUENCEFILE|TEXTFILE|RCFILE
  • 如果文件數據是純文本,可以使用 STORED AS TEXTFILE。如果數據需要壓縮,使用 STORED AS SEQUENCEFILE。

實例

hive> create table student(id int,name string) row format delimited fields terminated by '\t'; 創建表

hive> create  table if not exists student1 like student; 創建一個和表一樣模式的表

hive> create table if not exists mytable(sid int,sname string)
    >  row format delimited fields terminated by '\005' 
    >  stored as textfile; 創建內部表
    
hive> create external table if not exists pageview(
    >  pageid int,
    >  page_url string comment 'the page url'
    > )
    > row format delimited fields terminated by ','
    > location 'hdfs://192.168.220.144:9000/user/hive/warehouse'; 創建外部表
    
hive> create table student_p(id int,name string,sexex string,age int,dept string)
    >  partitioned by(part string)
    >  row format delimited fields terminated by ','
    >  stored as textfile;    創建分區表

查看表信息【describe】

hive> desc student;

hive> desc pageview.pageid; 查看表某列的信息

刪除表【drop table】

hive> drop table if exists student;

Alter Table/Partition/Column

ALTER TABLE table_name RENAME TO new_table_name; 重命名

ALTER TABLE table_name ADD [IF NOT EXISTS] partition_spec [ LOCATION 'location1' ] partition_spec [ LOCATION 'location2' ] ...
partition_spec:
: PARTITION (partition_col = partition_col_value, partition_col = partiton_col_value, ...) 增加分區

ALTER TABLE table_name DROP partition_spec, partition_spec,...  刪除分區

ALTER TABLE table_name [PARTITION partition_spec] SET LOCATION "new location"; 修改表或分區路徑



ALTER TABLE table_name ADD|REPLACE COLUMNS (col_name data_type [COMMENT col_comment], ...)  增加列
ALTER TABLE table_name CHANGE [COLUMN] col_old_name col_new_name column_type [COMMENT col_comment] [FIRST|AFTER column_name] 更新列




實例

hive> alter table student1 rename to student; 重命名

hive> alter table student_p add if not exists
    >  partition(part='a')  
    >  partition(part='b'); 增加分區
    
hive> alter table student add if not exists
    > partition(year=2011,month=1,day=1) location 'logs/2011/01/01'
    > partition(year=2011,month=1,day=2) location 'logs/2011/01/02';    增加分區

hive> alter table student_p drop partition(part='b'); 刪除分區
hive> ALTER TABLE log_messages DROP IF EXISTS PARTITION (year=2011,month=1,day=4); 刪除分區

hive> show partitions student_p; #顯示表分區


hive> alter table student add columns (gender string comment 'student  gender'); 增加列
hive> alter table student replace columns(id int, age int, name string); 替換所有字段


修改表屬性

hive> ALTER TABLE log_messages SET TBLPROPERTIES(
    > 'wujiadong');

修改存儲屬性

hive> ALTER TABLE log_messages
    > PARTITION(year=2011,month=1,day=1)
    > SET FILEFORMAT SEQUENCEFILE;


外部表【external】

hive> CREATE EXTERNAL TABLE IF NOT EXISTS  stocks5(
    >   home string,
    >   symbol string, 
    >   ymd string, 
    >   price_open float, 
    >   price_high float,
    >   price_low float, 
    >   price_close float, 
    >   volume int, 
    >   price_adj_close float  
    >   )
    > ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
    > LOCATION '/data/stocks';

自定義存儲格式

hive> CREATE TABLE employees(
    > name STRING,
    > salary FLOAT,
    > subordinates ARRAY<string>,
    > deductions MAP<STRING,FLOAT>,
    > address STRUCT<street:STRING,city:STRING,state:STRING,zip:INT>)
    > ROW FORMAT DELIMITED
    > FIELDS TERMINATED BY '\001'
    > COLLECTION ITEMS TERMINATED BY '\002'
    > MAP KEYS TERMINATED BY '\003'
    > LINES TERMINATED BY '\n'
    > STORED AS TEXTFILE;

DML操作

Loading files into tables

LOAD DATA [LOCAL] INPATH 'filepath' [OVERWRITE] INTO TABLE tablename [PARTITION (partcol1=val1, partcol2=val2 ...)]

注釋

  • Load 操作只是單純的復制/移動操作,將數據文件移動到 Hive 表對應的位置

  • filepath
    相對路徑,例如:project/data1
    絕對路徑,例如:/user/hive/project/data1
    包含模式的完整 URI,列如:hdfs://namenode:9000/user/hive/project/data1

  • LOCAL關鍵字
    如果指定了 LOCAL, load 命令會去查找本地文件系統中的 filepath。
    如果沒有指定 LOCAL 關鍵字,則根據inpath中的uri 查找文件

  • OVERWRITE關鍵字
    如果使用了 OVERWRITE關鍵字,則目標表(或者分區)中的內容會被刪除,然后再將filepath指向的文件/目錄中的內容添加到表/分區中
    如果目標表(分區)已經有一個文件,並且文件名和 filepath 中的文件名沖突,那么現有的文件會被新文件所替代。

創建內部表students
hive> create table students(id int,name string,gender string)
    > row format delimited fields terminated by ','
    > stored as textfile;

創建外部表students1
hive> create external table students1(id int,name string,gender string)
    > row format delimited fields terminated by ','
    > stored as textfile;

本地導入數據
hive> load data local inpath "/root/hive_test/students.txt" into table students;
加入overwrite 會先刪除之前存在的數據在插入數據
hive> load data local inpath "/root/hive_test/students.txt" overwrite into table students;

HDFS導入數據
[root@spark1 hive_test]# hadoop fs -put /root/hive_test/students.txt  /hive_test/   #先將將文件上傳到hdfs    
hive> load data inpath '/hive_test/students.txt' into table students; 數據導入hive表中
hive> load data inpath '/hive_test/students.txt' into table students1; 數據導入外部表中

查詢是否導入成功
hive> select * from students;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM