一、DDL數(shù)據(jù)定義語言
1、建庫
1)數(shù)據(jù)庫結(jié)構(gòu)
默認(rèn)的數(shù)據(jù)庫叫做default,存儲于HDFS的:/user/hive/warehouse
用戶自己創(chuàng)建的數(shù)據(jù)庫存儲位置:/user/hive/warehouse/database_name.db
2)創(chuàng)建數(shù)據(jù)庫
create (database|schema) [if not exists] database_name
[comment database_comment]
[location hdfs_path]
[with dbproperties (property_name=property_value, ...)];
- comment:數(shù)據(jù)庫的注釋說明語句
- location:指定數(shù)據(jù)庫在HDFS存儲位置,默認(rèn)/user/hive/warehouse/dbname.db
- with dbproperties:用于指定一些數(shù)據(jù)庫的屬性配置。
3)刪除數(shù)據(jù)庫
drop (database|schema) [if exists] database_name [restrict|cascade];
默認(rèn)行為是RESTRICT
,這意味著僅在數(shù)據(jù)庫為空時(shí)才刪除它。
若要?jiǎng)h除帶有表的數(shù)據(jù)庫(不為空的數(shù)據(jù)庫),可以使用CASCADE
。
2、建表
1)建表語法樹
create table [if not exists] [db_name.]table_name
(col_name data_type [comment col_comment], ... )
[comment table_comment]
[row format delimited …];
-- 創(chuàng)建數(shù)據(jù)庫并切換使用
create database if not exists itheima;
use itheima;
-- ddl create table
create table t_archer(
id int comment "ID",
name string,
hp_max int,
mp_max int,
attack_max int,
defense_max int,
attack_range string,
role_main string,
role_assist string
)
row format delimited
fields terminated by "\t";
2)指定分隔符
ROW FORMAT DELIMITED
:用于指定字段之間等相關(guān)的分隔符才能正確的讀取解析數(shù)據(jù)。
建表時(shí)如果沒有row format
語法指定分隔符,則采用默認(rèn)分隔符(‘\001’)。
3)Hive注釋信息中文亂碼解決
-- 注意 下面sql語句是需要在MySQL中執(zhí)行 修改Hive存儲的元數(shù)據(jù)信息(metadata)
use hive3;
show tables;
alter table hive3.COLUMNS_V2 modify column COMMENT varchar(256) character set utf8;
alter table hive3.TABLE_PARAMS modify column PARAM_VALUE varchar(4000) character set utf8;
alter table hive3.PARTITION_PARAMS modify column PARAM_VALUE varchar(4000) character set utf8 ;
alter table hive3.PARTITION_KEYS modify column PKEY_COMMENT varchar(4000) character set utf8;
alter table hive3.INDEX_PARAMS modify column PARAM_VALUE varchar(4000) character set utf8;
3、show語法
Show相關(guān)的語句可以幫助用戶查詢相關(guān)信息
-- 1、顯示所有數(shù)據(jù)庫 SCHEMAS和DATABASES的用法 功能一樣
show databases;
show schemas;
-- 2、顯示當(dāng)前數(shù)據(jù)庫所有表
show tables;
SHOW TABLES [IN database_name]; --指定某個(gè)數(shù)據(jù)庫
-- 3、查詢顯示一張表的元數(shù)據(jù)信息
desc formatted t_team_ace_player;
二、DML數(shù)據(jù)操縱語言
1、Load 加載
在Hive中建表成功之后,就會在HDFS上創(chuàng)建一個(gè)與之對應(yīng)的文件夾,文件夾名是表名
文件夾父路徑:/user/hive/warehouse/xxx.db
文件夾父路徑是由參數(shù)hive.metastore.warehouse.dir控制
Load語法:
load data [local] inpath 'filepath' [overwrite] into table tablename;
- filepath:表示待移動數(shù)據(jù)的路徑。
- 指定local:將在本地文件系統(tǒng)(Hiveserver2服務(wù)所在機(jī)器)中查找文件路徑。
- 沒有指定local:直接使用這個(gè)URI。
2、Insert 插入
插入數(shù)據(jù)會觸發(fā)MapReduce,慢死算了…??
insert + select:將后面查詢返回的結(jié)果作為內(nèi)容插入到指定表中,需要保證查詢結(jié)果列的數(shù)目和需要插入數(shù)據(jù)表格的列數(shù)目一致。
insert into table student_from_insert select num,name from student;
三、DQL數(shù)據(jù)查詢語言
就是正常的sql語言 ??
四、Hive 函數(shù)
1、函數(shù)分類
UDF:普通函數(shù),一進(jìn)一出
UDAF:聚合函數(shù),多進(jìn)一出
UDTF:表生成函數(shù),一進(jìn)多出文章來源:http://www.zghlxwxcb.cn/news/detail-725015.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-725015.html
2、內(nèi)置函數(shù)
1)字符串函數(shù)
-- Hive 常用的內(nèi)置函數(shù)
show functions;
describe function extended count;
-- String Functions 字符串函數(shù)
select length("itcast");
select reverse("itcast");
select concat("angela","baby");
-- 帶分隔符字符串連接函數(shù):concat_ws(separator, [string | array(string)]+)
select concat_ws('.', 'www', array('itcast', 'cn'));
-- 字符串截取函數(shù):substr(str, pos[, len]) 或者 substring(str, pos[, len])
select substr("angelababy",-2); --pos是從1開始的索引,如果為負(fù)數(shù)則倒著數(shù)
select substr("angelababy",2,2);
-- 分割字符串函數(shù): split(str, regex)
-- split針對字符串?dāng)?shù)據(jù)進(jìn)行切割 返回是數(shù)組array 可以通過數(shù)組的下標(biāo)取內(nèi)部的元素 注意下標(biāo)從0開始的
select split('apache hive', ' ');
select split('apache hive', ' ')[0];
select split('apache hive', ' ')[1];
2)日期函數(shù)
-- Date Functions 日期函數(shù)
-- 獲取當(dāng)前日期: current_date
select current_date();
-- 獲取當(dāng)前UNIX時(shí)間戳函數(shù): unix_timestamp
select unix_timestamp();
-- 日期轉(zhuǎn)UNIX時(shí)間戳函數(shù): unix_timestamp
select unix_timestamp("2011-12-07 13:01:03");
-- 指定格式日期轉(zhuǎn)UNIX時(shí)間戳函數(shù): unix_timestamp
select unix_timestamp('20111207 13:01:03','yyyyMMdd HH:mm:ss');
-- UNIX時(shí)間戳轉(zhuǎn)日期函數(shù): from_unixtime
select from_unixtime(1618238391);
select from_unixtime(0, 'yyyy-MM-dd HH:mm:ss');
-- 日期比較函數(shù): datediff 日期格式要求'yyyy-MM-dd HH:mm:ss' or 'yyyy-MM-dd'
select datediff('2012-12-08','2012-05-09');
-- 日期增加函數(shù): date_add
select date_add('2012-02-28',10);
-- 日期減少函數(shù): date_sub
select date_sub('2012-01-1',10);
3)數(shù)學(xué)函數(shù)
-- Mathematical Functions 數(shù)學(xué)函數(shù)
-- 取整函數(shù): round 返回double類型的整數(shù)值部分 (遵循四舍五入)
select round(3.1415926);
-- 指定精度取整函數(shù): round(double a, int d) 返回指定精度d的double類型
select round(3.1415926,4);
-- 取隨機(jī)數(shù)函數(shù): rand 每次執(zhí)行都不一樣 返回一個(gè)0到1范圍內(nèi)的隨機(jī)數(shù)
select rand();
-- 指定種子取隨機(jī)數(shù)函數(shù): rand(int seed) 得到一個(gè)穩(wěn)定的隨機(jī)數(shù)序列
select rand(3);
4)條件函數(shù)
-- Conditional Functions 條件函數(shù)
-- 使用之前課程創(chuàng)建好的student表數(shù)據(jù)
select * from student limit 3;
-- if條件判斷: if(boolean testCondition, T valueTrue, T valueFalseOrNull)
select if(1=2,100,200);
select if(sex ='男','M','W') from student limit 3;
-- 空值轉(zhuǎn)換函數(shù): nvl(T value, T default_value)
select nvl("allen","itcast");
select nvl(null,"itcast");
-- 條件轉(zhuǎn)換函數(shù): CASE a WHEN b THEN c [WHEN d THEN e]* [ELSE f] END
select case 100 when 50 then 'tom' when 100 then 'mary' else 'tim' end;
select case sex when '男' then 'male' else 'female' end from student limit 3;
到了這里,關(guān)于【大數(shù)據(jù)】Hive SQL語言(學(xué)習(xí)筆記)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!