主頁 >  其他 > hive 學習筆記

hive 學習筆記

2022-02-07 07:08:53 其他

一、官網和檔案地址

Hive 官網地址

hive官網

檔案查看地址

檔案地址

二、Hive 常用互動命令

(1)“-e”不進入 hive 的互動視窗執行 sql 陳述句

bin/hive -e "select id from student;"

(2)-f”執行腳本中 sql 陳述句

bin/hive -f /opt/module/hive/datas/hivef.sql 

(3)退出 hive 視窗

hive(default)>exit; 
hive(default)>quit;

(4)在 hive cli 命令視窗中如何查看 hdfs 檔案系統

hive(default)>dfs -ls /; 

三、Hive 資料型別

(1)基本資料型別

Hive 資料型別

Java 資料型別

長度

例子

TINYINT

byte

1byte 有符號整數

20

SMALINT

short

2byte 有符號整數

20

INT

int

4byte 有符號整數

20

BIGINT

long

8byte 有符號整數

20

BOOLEAN

boolean

布爾型別,true 或者false

TRUE FALSE

FLOAT

float

單精度浮點數

3.14159

DOUBLE

double

雙精度浮點數

3.14159

STRING

string

字符系列,可以指定字 符集,可以使用單引號或者雙 引號,

‘ now is the time ’ “for all good men”

TIMESTAMP

時間型別

BINARY

位元組陣列

(2)集合資料型別

資料型別

描述

語法示例

STRUCT

和 c 語言中的 struct 類似,都可以通過“點”符號訪 問元素內容,例如,如果某個列的資料型別是 STRUCT{first STRING, last STRING},那么 1 個元素可以通過欄位.first 參考

struct()

例 如 struct<street:string, city:string>

MAP

MAP 是一組鍵-值對元組集合,使用陣串列示法可以 訪問資料,例如,如果某個列的資料型別是 MAP,其中鍵

->值對是’first’->’John’和’last’->’Doe’,那么可以 通過欄位名[last]獲取最后一個元素

map()

例如 map<string, int>

ARRAY

陣列是一組具有相同型別和名稱的變數的集合,這些 變數稱為陣列的元素,每個陣列元素都有一個編號,編號從 零開始,例如,陣列值為[‘John’, ‘Doe’],那么 2

元素可以通過陣列名[1]進行參考

Array()

例如 array<string>

四、DDL 資料定義

(1)創建資料庫

// 資料庫在 HDFS 上的默認存盤路徑是/user/hive/warehouse/*.db
CREATE DATABASE [IF NOT EXISTS] database_name [COMMENT database_comment]
[LOCATION hdfs_path]
[WITH DBPROPERTIES (property_name=property_value, ...)];

eg: 創建一個資料庫,指定資料庫在 HDFS 上存放的位置,
create database db_hive2 location '/db_hive2.db';

(2)查詢資料庫

hive> show databases; 	
// 過濾顯示查詢的資料庫
hive> show databases like 'db_hive*'; 
// 查看資料庫詳情
hive> desc database db_hive;
// 顯示資料庫詳細資訊,extended
desc database extended db_hive;

(3)修改資料庫

// 用戶可以使用 ALTER DATABASE 命令為某個資料庫的 DBPROPERTIES 設定鍵-值對屬性值, 來描述這個資料庫的屬性資訊,
hive (default)> alter database db_hive
set dbproperties('createtime'='20220130');

(4)洗掉資料庫

// 洗掉空資料庫
hive>drop database db_hive2; 	
// 如果洗掉的資料庫不存在,最好采用 if exists 判斷資料庫是否存在
hive> drop database if exists db_hive2;
// 如果資料庫不為空,可以采用 cascade 命令,強制洗掉
hive> drop database db_hive cascade;

(5)創建表

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]
[TBLPROPERTIES (property_name=property_value, ...)] [AS select_statement]

eg:
create table if not exists student( id int, name string
)
row format delimited fields terminated by '\t' stored as textfile
location '/user/hive/warehouse/student';
字段解釋說明
(1)CREATE TABLE 創建一個指定名字的表,如果相同名字的表已經存在,則拋出例外; 用戶可以用 IF NOT EXISTS 選項來忽略這個例外,
(2)EXTERNAL 關鍵字可以讓用戶創建一個外部表,在建表的同時可以指定一個指向實 際資料的路徑(LOCATION),在洗掉表的時候,內部表的元資料和資料會被一起洗掉,而外 部表只洗掉元資料,不洗掉資料,
(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 確定表 的具體的列的資料,
SerDe 是 Serialize/Deserilize 的簡稱, hive 使用 Serde 進行行物件的序列與反序列化,
(8)STORED AS 指定存盤檔案型別 常用的存盤檔案型別:SEQUENCEFILE(二進制序列檔案)、TEXTFILE(文本)、RCFILE(列式存盤格式檔案)
如果檔案資料是純文本,可以使用 STORED AS TEXTFILE,如果資料需要壓縮,使用 STORED AS SEQUENCEFILE,
(9)LOCATION :指定表在 HDFS 上的存盤位置,
(10)AS:后跟查詢陳述句,根據查詢結果創建表,
(11)LIKE 允許用戶復制現有的表結構,但是不復制資料,
(6)修改表
// 修改表名
ALTER TABLE table_name RENAME TO new_table_name;
// 增加單個表磁區
hive (default)> alter table dept_partition add partition(day='20200404');
// 增加多個表磁區
hive (default)> alter table dept_partition add partition(day='20200405') partition(day='20200406');
// 洗掉單個磁區
hive (default)> alter table dept_partition drop partition (day='20200406');
// 同時洗掉多個磁區
hive (default)> alter table dept_partition drop partition (day='20200404'), partition(day='20200405');

(7)洗掉表

hive (default)> drop table dept; 	

五、DML 資料操作

(1)資料匯入 load data

hive> load data [local] inpath '資料的 path' [overwrite] into table
student [partition (partcol1=val1,…)];

(1)load data:表示加載資料
(2)local:表示從本地加載資料到 hive 表;否則從 HDFS 加載資料到 hive 表
(3)inpath:表示加載資料的路徑
(4)overwrite:表示覆寫表中已有資料,否則表示追加
(5)into table:表示加載到哪張表
(6)student:表示具體的表

(2)插入資料

// 基本模式插入(根據單張表查詢結果)
hive (default)> insert overwrite table student_par
select id, name from student where month='201709';
// 多表(多磁區)插入模式(根據多張表查詢結果),student 為具體的源表
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';

(3)創建表時通過 Location 指定加載資料路徑

1.上傳資料到 hdfs 上
hive (default)> dfs -mkdir /student;
hive (default)> dfs -put /opt/module/datas/student.txt /student;

2.創建表,并指定在 hdfs 上的位置
hive (default)> create external table if not exists student5( id int, name string)
row format delimited fields terminated by '\t' location '/student;

3.查詢資料
hive (default)> select * from student5; 

(4)Import 資料到指定 Hive 表中

hive (default)> import table student2
from '/user/hive/warehouse/export/student';

(5)資料匯出

1.Insert 匯出,將查詢的結果匯出到本地
hive (default)> insert overwrite local directory '/opt/module/hive/data/export/student'
select * from student;

2.將查詢的結果格式化匯出到本地
hive(default)>insert overwrite local directory '/opt/module/hive/data/export/student1'
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
select * from student;

3.將查詢的結果匯出到 HDFS 上(沒有 local)
hive (default)> insert overwrite directory '/user/atguigu/student2' ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
select * from student;

4.Hadoop 命令匯出到本地
hive (default)> dfs -get /user/hive/warehouse/student/student.txt
/opt/module/data/export/student3.txt;

5. Hive Shell 命令匯出
bin/hive -e 'select * from default.student;' > /opt/module/hive/data/export/student4.txt;

6. Export 匯出到 HDFS 上
hive (default)> export table default.student to 

(6)清除表中資料(Truncate)

// 注意:Truncate 只能洗掉管理表,不能洗掉外部表中資料
hive (default)> truncate table student; 	

六、查詢

官網查詢解釋

SELECT [ALL | DISTINCT] select_expr, select_expr, ...
  FROM table_reference
  [WHERE where_condition]
  [GROUP BY col_list]
  [ORDER BY col_list]
  [CLUSTER BY col_list
    | [DISTRIBUTE BY col_list] [SORT BY col_list]
  ]
 [LIMIT [offset,] rows]

七、磁區表和分桶表

(1)磁區表

1. 創建磁區表語法
hive (default)> create table dept_partition( deptno int, dname string, loc string)
partitioned by (day string)
row format delimited fields terminated by '\t';
注意:磁區欄位不能是表中已經存在的資料,可以將磁區欄位看作表的偽列,

2.加載資料到磁區表中,注意:磁區表加載資料時,必須指定磁區
hive (default)> load data local inpath
'/opt/module/hive/datas/dept_20200401.log' into table dept_partition partition(day='20200401');
hive (default)> load data local inpath
'/opt/module/hive/datas/dept_20200403.log' into table dept_partition
partition(day='20200403');

3.查詢磁區表中資料 單磁區查詢
hive (default)> select * from dept_partition where day='20200401'; 	

4.增加磁區
hive (default)> alter table dept_partition add partition(day='20200404');
hive (default)> alter table dept_partition add partition(day='20200405') partition(day='20200406');

5.洗掉磁區
hive (default)> alter table dept_partition drop partition (day='20200406');
hive (default)> alter table dept_partition drop partition (day='20200404'), partition(day='20200405');

6.查看磁區表有多少磁區
hive> show partitions dept_partition; 	

7.查看磁區表結構
hive> desc formatted dept_partition;

(2)二級磁區

// 創建二級磁區表
hive (default)> create table dept_partition2( deptno int, dname string, loc string)
partitioned by (day string, hour string)
row format delimited fields terminated by '\t';

// 加載資料到二級磁區表中
hive (default)> load data local inpath '/opt/module/hive/datas/dept_20200401.log' into table dept_partition2 partition(day='20200401', hour='12');

// 查詢磁區資料
hive (default)> select * from dept_partition2 where day='20200401' and hour='12';

(3)把資料直接上傳到磁區目錄上,讓磁區表和資料產生關聯的三種方式

方式一:上傳資料后修復 上傳
hive (default)> dfs -mkdir -p
/user/hive/warehouse/mydb.db/dept_partition2/day=20200401/hour=13; 
hive (default)> dfs -put /opt/module/datas/dept_20200401.log
/user/hive/warehouse/mydb.db/dept_partition2/day=20200401/hour=13;
查詢資料(查詢不到剛上傳的資料)
hive (default)> select * from dept_partition2 where day='20200401' and hour='13';
執行修復命令
hive> msck repair table dept_partition2; 	


方式二:上傳資料后添加磁區 上傳資料
hive (default)> dfs -mkdir -p
/user/hive/warehouse/mydb.db/dept_partition2/day=20200401/hour=14; 
hive (default)> dfs -put /opt/module/hive/datas/dept_20200401.log
/user/hive/warehouse/mydb.db/dept_partition2/day=20200401/hour=14;
執行添加磁區
hive (default)> alter table dept_partition2 add partition(day='201709',hour='14');
查詢資料
hive (default)> select * from dept_partition2 where day='20200401' and hour='14';


方式三:創建檔案夾后 load 資料到磁區 創建目錄
hive (default)> dfs -mkdir -p
/user/hive/warehouse/mydb.db/dept_partition2/day=20200401/hour=15;
上傳資料
hive (default)> load data local inpath '/opt/module/hive/datas/dept_20200401.log' into table dept_partition2 partition(day='20200401',hour='15');
查詢資料
hive (default)> select * from dept_partition2 where day='20200401' and hour='15';

(4)分桶表

// 創建分桶表
create table stu_buck(id int, name string) clustered by(id)
into 4 buckets
row format delimited fields terminated by '\t';

// 查看表結構
hive (default)> desc formatted stu_buck; 
Num Buckets:	4

// 匯入資料到分桶表中,load 的方式
hive (default)> load data inpath '/student.txt' into table stu_buck; 

分桶表操作需要注意的事項:
(1)reduce 的個數設定為-1,讓 Job 自行決定需要用多少個 reduce 或者將 reduce 的個 數設定為大于等于分桶表的桶數
(2)從 hdfs 中 load 資料到分桶表中,避免本地檔案找不到問題
(3)不要使用本地模式

八、函式

(1)查看系統自帶的函式
hive> show functions;
(2)顯示自帶的函式的用法
hive> desc function upper;
(3)詳細顯示自帶的函式的用法
hive> desc function extended upper;

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/423404.html

標籤:其他

上一篇:MySQL高級查詢

下一篇:SpringBoot專案集成全文搜索引擎Elasticsearch

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 網閘典型架構簡述

    網閘架構一般分為兩種:三主機的三系統架構網閘和雙主機的2+1架構網閘。 三主機架構分別為內端機、外端機和仲裁機。三機無論從軟體和硬體上均各自獨立。首先從硬體上來看,三機都用各自獨立的主板、記憶體及存盤設備。從軟體上來看,三機有各自獨立的作業系統。這樣能達到完全的三機獨立。對于“2+1”系統,“2”分為 ......

    uj5u.com 2020-09-10 02:00:44 more
  • 如何從xshell上傳檔案到centos linux虛擬機里

    如何從xshell上傳檔案到centos linux虛擬機里及:虛擬機CentOs下執行 yum -y install lrzsz命令,出現錯誤:鏡像無法找到軟體包 前言 一、安裝lrzsz步驟 二、上傳檔案 三、遇到的問題及解決方案 總結 前言 提示:其實很簡單,往虛擬機上安裝一個上傳檔案的工具 ......

    uj5u.com 2020-09-10 02:00:47 more
  • 一、SQLMAP入門

    一、SQLMAP入門 1、判斷是否存在注入 sqlmap.py -u 網址/id=1 id=1不可缺少。當注入點后面的引數大于兩個時。需要加雙引號, sqlmap.py -u "網址/id=1&uid=1" 2、判斷文本中的請求是否存在注入 從文本中加載http請求,SQLMAP可以從一個文本檔案中 ......

    uj5u.com 2020-09-10 02:00:50 more
  • Metasploit 簡單使用教程

    metasploit 簡單使用教程 浩先生, 2020-08-28 16:18:25 分類專欄: kail 網路安全 linux 文章標簽: linux資訊安全 編輯 著作權 metasploit 使用教程 前言 一、Metasploit是什么? 二、準備作業 三、具體步驟 前言 Msfconsole ......

    uj5u.com 2020-09-10 02:00:53 more
  • 游戲逆向之驅動層與用戶層通訊

    驅動層代碼: #pragma once #include <ntifs.h> #define add_code CTL_CODE(FILE_DEVICE_UNKNOWN,0x800,METHOD_BUFFERED,FILE_ANY_ACCESS) /* 更多游戲逆向視頻www.yxfzedu.com ......

    uj5u.com 2020-09-10 02:00:56 more
  • 北斗電力時鐘(北斗授時服務器)讓網路資料更精準

    北斗電力時鐘(北斗授時服務器)讓網路資料更精準 北斗電力時鐘(北斗授時服務器)讓網路資料更精準 京準電子科技官微——ahjzsz 近幾年,資訊技術的得了快速發展,互聯網在逐漸普及,其在人們生活和生產中都得到了廣泛應用,并且取得了不錯的應用效果。計算機網路資訊在電力系統中的應用,一方面使電力系統的運行 ......

    uj5u.com 2020-09-10 02:01:03 more
  • 【CTF】CTFHub 技能樹 彩蛋 writeup

    ?碎碎念 CTFHub:https://www.ctfhub.com/ 筆者入門CTF時時剛開始刷的是bugku的舊平臺,后來才有了CTFHub。 感覺不論是網頁UI設計,還是題目質量,賽事跟蹤,工具軟體都做得很不錯。 而且因為獨到的金幣制度的確讓人有一種想去刷題賺金幣的感覺。 個人還是非常喜歡這個 ......

    uj5u.com 2020-09-10 02:04:05 more
  • 02windows基礎操作

    我學到了一下幾點 Windows系統目錄結構與滲透的作用 常見Windows的服務詳解 Windows埠詳解 常用的Windows注冊表詳解 hacker DOS命令詳解(net user / type /md /rd/ dir /cd /net use copy、批處理 等) 利用dos命令制作 ......

    uj5u.com 2020-09-10 02:04:18 more
  • 03.Linux基礎操作

    我學到了以下幾點 01Linux系統介紹02系統安裝,密碼啊破解03Linux常用命令04LAMP 01LINUX windows: win03 8 12 16 19 配置不繁瑣 Linux:redhat,centos(紅帽社區版),Ubuntu server,suse unix:金融機構,證券,銀 ......

    uj5u.com 2020-09-10 02:04:30 more
  • 05HTML

    01HTML介紹 02頭部標簽講解03基礎標簽講解04表單標簽講解 HTML前段語言 js1.了解代碼2.根據代碼 懂得挖掘漏洞 (POST注入/XSS漏洞上傳)3.黑帽seo 白帽seo 客戶網站被黑帽植入劫持代碼如何處理4.熟悉html表單 <html><head><title>TDK標題,描述 ......

    uj5u.com 2020-09-10 02:04:36 more
最新发布
  • 2023年最新微信小程式抓包教程

    01 開門見山 隔一個月發一篇文章,不過分。 首先回顧一下《微信系結手機號資料庫被脫庫事件》,我也是第一時間得知了這個訊息,然后跟蹤了整件事情的經過。下面是這起事件的相關截圖以及近日流出的一萬條資料樣本: 個人認為這件事也沒什么,還不如關注一下之前45億快遞資料查詢渠道疑似在近日復活的訊息。 訊息是 ......

    uj5u.com 2023-04-20 08:48:24 more
  • web3 產品介紹:metamask 錢包 使用最多的瀏覽器插件錢包

    Metamask錢包是一種基于區塊鏈技術的數字貨幣錢包,它允許用戶在安全、便捷的環境下管理自己的加密資產。Metamask錢包是以太坊生態系統中最流行的錢包之一,它具有易于使用、安全性高和功能強大等優點。 本文將詳細介紹Metamask錢包的功能和使用方法。 一、 Metamask錢包的功能 數字資 ......

    uj5u.com 2023-04-20 08:47:46 more
  • vulnhub_Earth

    前言 靶機地址->>>vulnhub_Earth 攻擊機ip:192.168.20.121 靶機ip:192.168.20.122 參考文章 https://www.cnblogs.com/Jing-X/archive/2022/04/03/16097695.html https://www.cnb ......

    uj5u.com 2023-04-20 07:46:20 more
  • 從4k到42k,軟體測驗工程師的漲薪史,給我看哭了

    清明節一過,盲猜大家已經無心上班,在數著日子準備過五一,但一想到銀行卡里的余額……瞬間心情就不美麗了。最近,2023年高校畢業生就業調查顯示,本科畢業月平均起薪為5825元。調查一出,便有很多同學表示自己又被平均了。看著這一資料,不免讓人想到前不久中國青年報的一項調查:近六成大學生認為畢業10年內會 ......

    uj5u.com 2023-04-20 07:44:00 more
  • 最新版本 Stable Diffusion 開源 AI 繪畫工具之中文自動提詞篇

    🎈 標簽生成器 由于輸入正向提示詞 prompt 和反向提示詞 negative prompt 都是使用英文,所以對學習母語的我們非常不友好 使用網址:https://tinygeeker.github.io/p/ai-prompt-generator 這個網址是為了讓大家在使用 AI 繪畫的時候 ......

    uj5u.com 2023-04-20 07:43:36 more
  • 漫談前端自動化測驗演進之路及測驗工具分析

    隨著前端技術的不斷發展和應用程式的日益復雜,前端自動化測驗也在不斷演進。隨著 Web 應用程式變得越來越復雜,自動化測驗的需求也越來越高。如今,自動化測驗已經成為 Web 應用程式開發程序中不可或缺的一部分,它們可以幫助開發人員更快地發現和修復錯誤,提高應用程式的性能和可靠性。 ......

    uj5u.com 2023-04-20 07:43:16 more
  • CANN開發實踐:4個DVPP記憶體問題的典型案例解讀

    摘要:由于DVPP媒體資料處理功能對存放輸入、輸出資料的記憶體有更高的要求(例如,記憶體首地址128位元組對齊),因此需呼叫專用的記憶體申請介面,那么本期就分享幾個關于DVPP記憶體問題的典型案例,并給出原因分析及解決方法。 本文分享自華為云社區《FAQ_DVPP記憶體問題案例》,作者:昇騰CANN。 DVPP ......

    uj5u.com 2023-04-20 07:43:03 more
  • msf學習

    msf學習 以kali自帶的msf為例 一、msf核心模塊與功能 msf模塊都放在/usr/share/metasploit-framework/modules目錄下 1、auxiliary 輔助模塊,輔助滲透(埠掃描、登錄密碼爆破、漏洞驗證等) 2、encoders 編碼器模塊,主要包含各種編碼 ......

    uj5u.com 2023-04-20 07:42:59 more
  • Halcon軟體安裝與界面簡介

    1. 下載Halcon17版本到到本地 2. 雙擊安裝包后 3. 步驟如下 1.2 Halcon軟體安裝 界面分為四大塊 1. Halcon的五個助手 1) 影像采集助手:與相機連接,設定相機引數,采集影像 2) 標定助手:九點標定或是其它的標定,生成標定檔案及內參外參,可以將像素單位轉換為長度單位 ......

    uj5u.com 2023-04-20 07:42:17 more
  • 在MacOS下使用Unity3D開發游戲

    第一次發博客,先發一下我的游戲開發環境吧。 去年2月份買了一臺MacBookPro2021 M1pro(以下簡稱mbp),這一年來一直在用mbp開發游戲。我大致分享一下我的開發工具以及使用體驗。 1、Unity 官網鏈接: https://unity.cn/releases 我一般使用的Apple ......

    uj5u.com 2023-04-20 07:40:19 more