1、jdbc:mysql://localhost:3306/hive?createDatabaseIfNotExist=true&characterEncoding=UTF-8&useSSL=false
2、desc (formatted) 表名;
可以查看表的描述
3、文件以逗號分隔,重命名csv結尾,可以用Excel打開
4、Linux下有一個wc -l 文件名,看文件內容數量
5、外部表,出現空值,同樣內容放到外部表,出現空值,而放在分區表,卻全部顯示
刪除內部表(管理表)即把內容也會刪除
7)“-e”不進入 hive 的交互窗口執行 sql 語句
[hadoop@master hive]$ bin/hive -e "select id from student;"
8、“-f”執行腳本中 sql 語句
(1)在/opt/module/datas 目錄下創建 hivef.sql 文件
[hadoop@master datas]$ touch hivef.sql
文件中寫入正確的 sql 語句
select *from student;
(2)執行文件中的 sql 語句
[hadoop@master hive]$ bin/hive -f /opt/module/datas/hivef.sql
(3)執行文件中的 sql 語句並將結果寫入文件中
[hadoop@master hive]$ bin/hive -f /opt/module/datas/hivef.sql >
/opt/module/datas/hive_result.txt
9、退出 hive 窗口:
exit:先隱性提交數據,再退出; quit:不提交數據,退出;
10在 hive cli 命令窗口中如何查看 hdfs 文件系統
hive(default)>dfs -ls /;
11)在 hive cli 命令窗口中如何查看 hdfs 本地系統
hive(default)>! ls /opt/module/datas;
12)查看在 hive 中輸入的所有歷史命令
(1)進入到當前用戶的根目錄/root 或/home/atguigu
(2)查看. hivehistory 文件
[hadoop@master ~]$ cat .hivehistory
13)在 hive-site.xml 文件中添加如下配置信息,就可以實現顯示當前數據庫,以及查詢 表的頭信息配置。
<property>
<name>hive.cli.print.header</name>
<value>true</value>
</property>
<property>
<name>hive.cli.print.current.db</name>
<value>true</value>
</property>
配置前

配置后

14)參數的配置三種方式
(1)配置文件方式 默認配置文件:hive-default.xml 用戶自定義配置文件:hive-site.xml cp hive-default.xml hive-site.xml
注意:用戶自定義配置會覆蓋默認配置。另外,Hive 也會讀入 Hadoop 的配置,因 為 Hive 是作為 Hadoop 的客戶端啟動的,Hive 的配置會覆蓋 Hadoop 的配置。配置文件 的設定對本機啟動的所有 Hive 進程都有效。
(2)命令行參數方式
啟動 Hive 時,可以在命令行添加-hiveconf param=value 來設定參數。 例如
bin/hive -hiveconf mapred.reduce.tasks=10;
注意:僅對本次 hive 啟動有效 查看參數設置:
hive (default)> set mapred.reduce.tasks;
(3)參數聲明方式
可以在 HQL 中使用 SET 關鍵字設定參數 例如:
hive (default)> set mapred.reduce.tasks=100; 注意:僅對本次 hive 啟動有效。
查看
hive (default)> set mapred.reduce.tasks;
上述三種設定方式的優先級依次遞增。即配置文件<命令行參數<參數聲明。注意某些 系統級的參數,例如 log4j 相關的設定,必須用前兩種方式設定,因為那些參數的讀取在會 話建立以前已經完成了。
15Hive 數據類型



16創建表
{
"name": "songsong",
"friends": ["bingbing" , "lili"] , "children": {
"xiao song": 18 , "xiaoxiao song": 19
}
"address": {
"street": "hui long guan" , "city": "beijing"
}
create table if not exists test( name string,
friends array<string>, children map<string, int>,
address struct<street:string, city:string>)
row format delimited fields terminated by ',' collection items terminated by '_'
map keys terminated by ':'
lines terminated by '\n';
字段解釋
row format delimited fields terminated by ',' -- 列分隔符
collection items terminated by '_' --MAP STRUCT 和 ARRAY 的分隔符(數據分割符號) map keys terminated by ':' -- MAP 中的 key 與 value 的分隔符
lines terminated by '\n'; -- 行分隔符
17導入數據
load data local inpath '/opt/module/datas/test.txt' into table test;
是本地導入,不是集群
select friends[1],children['xiao song'],address.city from test where
name="songsong";
18轉換(隱式轉換只能向上轉不能向下轉)
使用 CAST 操作顯示進行數據類型轉換,例如 CAST('1' AS INT)將把字符串'1' 轉換 成整數 1
19修改數據庫
數據庫的其他元數據信息都是不可更改的,包括數 據庫名和數據庫所在的目錄位置。
只能修改dbproperties屬性
hive (default)> alter database db_hive set dbproperties('createtime'='20170830');
在 mysql 中查看修改結果
hive> desc database extended db_hive;
20顯示數據庫
顯示數據庫
hive> show databases;
2)過濾顯示查詢的數據庫
hive> show databases like 'db_hive*'; OK
db_hive db_hive_1
顯示數據庫信息
hive> desc database db_hive;
OK
db_hive hdfs://hadoop102:8020/user/hive/warehouse/db_hive.db atguiguUSER
2)顯示數據庫詳細信息,extended
hive> desc database extended db_hive;
21刪除數據庫
如果數據庫不為空,可以采用 cascade 命令,強制刪除
hive> drop database db_hive cascade;
22創建表
語法
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]
解釋
字段解釋說明:
(1)CREATE TABLE 創建一個指定名字的表。如果相同名字的表已經存在,則拋出 異常;用戶可以用 IF NOT EXISTS 選項來忽略這個異常。
(2)EXTERNAL 關鍵字可以讓用戶創建一個外部表,在建表的同時指定一個指向實際 數據的路徑(LOCATION),Hive 創建內部表時,會將數據移動到數據倉庫指向的路 徑;若創建外部表,僅記錄數據所在的路徑,不對數據的位置做任何改變。在刪除表的 時候,內部表的元數據和數據會被一起刪除,而外部表只刪除元數據,不刪除數據。
(3)COMMENT:為表和列添加注釋。
(4)PARTITIONED BY 創建分區表
(5)CLUSTERED BY 創建分桶表
(6)SORTED BY 不常用
(7)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 確定表的具體的列的數據。
(8)STORED AS 指定存儲文件類型 常用的存儲文件類型:SEQUENCEFILE(二進制序列文件)、TEXTFILE(文本)、 RCFILE(列式存儲格式文件)
如果文件數據是純文本,可以使用 STORED AS TEXTFILE。如果數據需要壓縮, 使用 STORED AS SEQUENCEFILE。
(9)LOCATION :指定表在 HDFS 上的存儲位置。
(10)LIKE 允許用戶復制現有的表結構,但是不復制數據。
21普通創建表
。當我們 刪除一個管理表時,Hive 也會刪除這個表中數據。管理表不適合和其他工具共享數據。
create table if not exists student2( id int, name string
)
row format delimited fields terminated by '\t' stored as textfile
location '/user/hive/warehouse/student2';
根據查詢結果創建表(查詢的結果會添加到新創建的表中)
create table if not exists student3
as select id, name from student;
(3)根據已經存在的表結構創建表
create table if not exists student4 like student;
22查詢表
desc formatted student2;
23外部表
因為表是外部表,所有 Hive 並非認為其完全擁有這份數據。刪除該表並不會刪除掉這 份數據,不過描述表的元數據信息會被刪除掉。
create external table if not exists default.dept( deptno int,
dname string, loc int
)
row format delimited fields terminated by '\t';
本地導入
load data local inpath '/opt/module/datas/dept.txt' into table default.dept;
24分區表
create table dept_partition(deptno int, dname string, loc string) partitioned by (month string) row format delimited fields terminated by '\t';
加載數據
load data local inpath '/home/hadoop/dept.txt' into table default.dept_partition partition(month='201709');
select * from dept_partition where month='201709';
增加分區(多個)
alter table dept_partition add partition(month='201705') partition(month='201704');
刪除分區(多個)
alter table dept_partition drop partition (month='201705'), partition (month='201706');
查看分區 表
show partitions dept_partition;
查看分區表結構
hive>desc formatted dept_partition;
二級分區表
hive (default)> create table dept_partition2(
deptno int, dname string, loc string
)
partitioned by (month string, day string)
row format delimited fields terminated by '\t';
select * from dept_partition2 where month='201709' and day='13';
數據直接上傳到分區目錄上,讓分區表和數據產生關聯的兩種方式
(1)方式一:上傳數據后修復 上傳數據
hive (default)> dfs -mkdir -p
/user/hive/warehouse/dept_partition2/month=201709/day=12;
hive (default)> dfs -put /opt/module/datas/dept.txt
/user/hive/warehouse/dept_partition2/month=201709/day=12;
查詢數據(查詢不到剛上傳的數據)
hive (default)> select * from dept_partition2 where month='201709' and day='12';
執行修復命令
hive>msck repair table dept_partition2;
再次查詢數據
hive (default)> select * from dept_partition2 where month='201709' and day='12';
(2)方式二:上傳數據后添加分區
上傳數據
hive (default)> dfs -mkdir -p
/user/hive/warehouse/dept_partition2/month=201709/day=11;
hive (default)> create table dept_partition2(
deptno int, dname string, loc string
)
partitioned by (month string, day string)
row format delimited fields terminated by '\t';
hive (default)> dfs -put /opt/module/datas/dept.txt
/user/hive/warehouse/dept_partition2/month=201709/day=11;
執行添加分區
hive (default)> alter table dept_partition2 add partition(month='201709', day='11');
查詢數據
hive (default)> select * from dept_partition2 where month='201709' and day='11';
(3)方式三:上傳數據后 load 數據到分區 創建目錄
hive (default)> dfs -mkdir -p
/user/hive/warehouse/dept_partition2/month=201709/day=10;
上傳數據
hive (default)> load data local inpath '/opt/module/datas/dept.txt' into table dept_partition2 partition(month='201709',day='10');
查詢數據
hive (default)> select * from dept_partition2 where month='201709' and day='10';
25重命名表
alter table dept_partition2 rename to dept_partition3
26加載表
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)into table:表示加載到哪張表
(5)student:表示具體的表
(6)overwrite:表示覆蓋表中已有數據,否則表示追加
(7)partition:表示上傳到指定分區
27通過查詢語句向表中插入數據(Insert)
創建分區表
create table student(id string, name string) partitioned by (month string) row format delimited fields terminated by '\t';
2)基本插入數據
insert into table
student partition(month='201709')
values('1004','wangwu');
基本模式插入(根據單張表查詢結果)
hive (default)> insert overwrite table student partition(month='201708') select id, name from student where month='201709';
多插入模式(根據多張表查詢結果)
hive (default)> from student
insert overwrite table student partition(month='201707') select id, name where month='201709'
insert overwrite table student partition(month='201706') select id, name where month='201709';
根據查詢結果創建表(查詢的結果會添加到新創建的表中)
create table if not exists student3
as select id, name from student;
創建表時通過 Location 指定加載數據路徑
創建表,並指定在 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
