Hive的基本操作(創建與修改)

必備知識

數據類型

基本類型

類型寫法
字符char, varchar, string?
整數tinyint, smallint, int?, bigint?
小數float, double, numeric(m,n), decimal(m,n)?
布爾值boolean?
時間date?, timestamp?

復雜類型(集合類型)

1、數組:array<T>	面向用戶提供的原始數據做結構化映射樣例: [] / |156,1778,42,138|	=> 描述同一個維度數據2、鍵值對:map<K,V>		樣例: |LogicJava:88,mysql:89|3、結構體:struct<name1:value1,name2:value2,....>樣例: 類json格式【以{}開頭結尾,且結構穩定】 => 結構化數據

【創建】表操作

一:hive建表【基本語法】

語法組成

組成一:建表 = 基本格式 + 行格式 + 額外處理
組成二:上傳數據
*基本格式
create table if not exists TABLE_NAME(FIELD_NAME DATA_TYPE,FIELD_NAME DATA_TYPE,....
)[comment '描述備注']
*行格式
形式一:row format delimited
1、應用場景:面向文本,非結構化與半結構化數據2、模擬數據:123,張三,16853210211116,true,26238.5,閱讀;跑步;唱歌,java:98;mysql:54,province:南京;city:江寧3、案例演示:create table if not exists TABLE_NAME(id int,name string,time bigint,isPartyMember boolean,hobby array<string>,scores map<string,int>,address struct<province:string,city:string>)row format delimitedfields terminated by ','collection items terminated by ';'map keys terminated by ':'lines terminated by '\n'4、講解:fields terminated by ','				列分隔符【字段: id,name...】collection items terminated by ';'		集合項內部間的分隔符map keys terminated by ':'				鍵值對[map]分隔符lines terminated by '\n'				行分隔符【默認,一般可以省略】
形式二:row format serde ‘CLASS_PATH’
1、應用場景:面向結構化數據,即:結構清晰的數據2、CLASS_PATH有以下幾種選擇:選擇一:CSV【簡單類型】數據呈現:"1","2","Football""2","2","Soccer""3","2","Baseball & Softball"代碼:create table if not exists TABLE_NAME(id string,page string,word string)row format serde 'org.apache.hadoop.hive.serde2.OpenCSVSerde'with serdeproperties('separatorChar'=',','quoteChar'='"','escapeChar'='\\')選擇二:regex【正則】數據呈現:123,張三,16853210211116,true,26238.5,閱讀;跑步;唱歌,java:98;mysql:54,province:南京;city:江寧代碼:create table if not exists TABLE_NAME(id int,name string,time bigint,isPartyMember boolean,hobby array<string>,scores map<string,int>,address struct<province:string,city:string>)row format serde 'org.apache.hadoop.hive.serde2.RegexSerDe'with serdeproperties('input.regex'='^(//d+),(.*?),(//d+),(true|false),(\\d+\\.?\\d+?)$')選擇三:JsonSerDe數據呈現:{"name":"henry","age":22,"gender":"male","phone":"18014499655"}代碼:create table if not exists json(name string,age int,gender string,phone string)row format serde 'org.apache.hive.hcatalog.data.JsonSerDe'
*額外處理
1、store【存儲】基本語法:stored as '存儲格式'存儲格式:textfile?,orc,parquet,sequencefile,...案例:stored as textfile2、tblproperties【表屬性】(通用):案例【實際情況具體分析】:tblproperties('skip.header.line.count'='1'			【跳過表頭,即:第一行】...)
*上傳數據入表
方法一【不建議用】:hdfs dfs -put employee.txt /hive312/warehouse/yb12211.db/inner_table_employee方法二【有校驗過程】:?需知:local :表示數據在虛擬機本地缺少local :表示數據在hdfs上overload :覆蓋缺少overload :追加第一種【本地虛擬機】:load data local inpath '/root/file/employee.txt'overwrite into table yb12211.inner_table_employee;第二種【hdfs】:load data inpath '/hive_data/hive_cha01/employee/employee.txt'overwrite into table yb12211.inner_table_employee;方法三【只用于【外部表】】:?基本格式:location 'hdfs中存放文件的【目錄】的路徑'			外部掛載

針對性實踐操作

案例一:/*1|henry|1.81|1995-03-18|江蘇,南京,玄武,北京東路68號|logicjava:88,javaoop:76,mysql:80,ssm:82|beauty,money,joke2|arill|1.59|1996-7-30|安徽,蕪湖,南山,西湖東路68號|logicjava:79,javaoop:58,mysql:65,ssm:85|beauty,power,sleeping3|mary|1.72|1995-09-02|山東,青島,長虹,天山東路68*/drop table if exists students;create table if not exists students(number int,name string,height decimal(3,2),birthday date,house struct<province:string,city:string,district:string,street:string>,scores map<string,int>,hobby array<string>)row format delimitedfields terminated by "|"collection items terminated by ","map keys terminated by ":"stored as textfile;load data inpath '/zhou/students.txt'overwrite into table zhou.students;案例二:/*user_id,auction_id,cat_id,cat1,property,buy_mount,day
786295544,41098319944,50014866,50022520,21458:86755362;13023209:3593274;10984217:21985;122217965:3227750;21477:28695579;22061:30912;122217803:3230095,2,123434123*/drop table if exists sam_mum_baby_trade;create external table if not exists sam_mum_baby_trade(user_id bigint,auction_id bigint,cat_id bigint,cat1 bigint,property map<bigint,bigint>,buy_mount int,day bigint)row format delimitedfields terminated by ","collection items terminated by ";"map keys terminated by ":"stored as textfiletblproperties ('skip.header.line.count'='1');load data inpath '/zhou/sam_mum_baby_trade.csv'into table zhou.sam_mum_baby_trade;案例三:/*"1","2","Football""2","2","Soccer""3","2","Baseball & Softball"*/drop table if exists categories;create table if not exists categories(id string,page string,word string)row format serde 'org.apache.hadoop.hive.serde2.OpenCSVSerde'with serdeproperties('separatorChar'=',','quoteChar'='"','escapeChar'='\\')stored as textfile;load data inpath '/zhou/categories.csv'overwrite into table zhou.categories;select * from categories;案例四:/*{"name":"henry","age":22,"gender":"male","phone":"18014499655"}*///Jsondrop table if exists json;create table if not exists json(name string,age int,gender string,phone string)row format serde 'org.apache.hive.hcatalog.data.JsonSerDe'stored as textfile;load data inpath '/zhou/json.log'overwrite into table zhou.json;案例五:/*125;男;2015-9-7 1:52:22;1521.84883;男;2014-9-18 5:24:42;6391.45652;女;2014-5-4 5:56:45;9603.79*/create external table if not exists test1w(user_id int,user_gender string,order_time timestamp,order_amount decimal(6,2))row format serde 'org.apache.hadoop.hive.serde2.RegexSerDe'with serdeproperties('input.regex'='(\\d+);(.*?);(\\d{4}-\\d{1,2}-\\d{1,2} \\d{1,2}:\\d{1,2}:\\d{1,2});(\\d+\.?\\d+?)')stored as textfilelocation '/zhou/test1w';select * from test1w;

二:hive建表【高階語法】

1:CTAS

【本質】:在原有表的基礎上查詢并創建新表
基本語法:create table if not exists NEW_TABLE_NAME as select ... from OLD_TABLE_NAME ...
案例:原有的表:hive_ext_regex_test1w語句:create table if not exists hive_ext_test_before2015 asselect * from hive_ext_regex_test1wwhere year(order_time)<=2015;

2:CTE

【本質】:對表進行層層篩選,最終形成新表
基本語法:as with....select...
案例:場景:2015年之前的所有數據 以及 2015年之后男性5個以上訂單數或5w以上訂單總額的訂單數據。原有的表:hive_ext_regex_test1w語句:create table hive_test_before2015_and_male_over5or5w aswithbefore2015 as (select * from hive_ext_regex_test1wwhere year(order_time)<=2015),agg_male_over5or5w as (select user_idfrom hive_ext_regex_test1wwhere year(order_time)>2015 and user_gender='男'group by user_idhaving count(*)>=5 or sum(order_amount)>=50000),male_over5or5w as (select A.*from  hive_ext_regex_test1w Ainner join agg_male_over5or5w Bon year(A.order_time)>2015 and A.user_id=B.user_id)select * from before2015union all							【注意:union all => 將表并在一起且不去重】select * from male_over5or5w;

3:CTL

【本質】:復制原表的表結構
基本語法:create table NEW_TABLE_NAME like OLD_TABLE_NAME;
案例:create table hive_test1w_like like hive_ext_regex_test1w;

【修改】表操作

提前需知

1、查看表字段基本信息:desc 表名;2、查看表字段詳細信息:desc formatted 表名;	=> 由此可查看表中可修改的屬性3、查看建表流程:show create 表名;

基本語法

alter table TABLE_NAMErename to NEW_NAME;set tblproperties('key'='value')		-- 修改表屬性:包括各種分隔符,SerDe,...ser fileformat FORMAT;					-- 修改文件格式change old_name new_name TYPE;			-- 修改字段名column(field_name TYPE)					-- 添加列

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/45061.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/45061.shtml
英文地址,請注明出處:http://en.pswp.cn/web/45061.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

從頭開始搭建一套Elasticsearch集群

前言 剛開始使用ES接觸的就是rpm或者是云上提供的ES服務&#xff0c;基本上開箱即用。特別是云上的ES服務&#xff0c;開局就是集群版本&#xff0c;提供的是優化后的參數配置、開箱即匹配訪問鑒權及常用插件&#xff0c;如無特殊需要基本上屏蔽了所有細節&#xff0c;直接可投…

深入了解 MySQL 的 EXPLAIN 命令

一、什么是 EXPLAIN 命令&#xff1f; EXPLAIN 命令用于顯示 MySQL 如何執行某個 SQL 語句&#xff0c;尤其是 SELECT 語句。通過 EXPLAIN 命令&#xff0c;可以看到查詢在實際執行前的執行計劃&#xff0c;這對于優化查詢性能至關重要。 二、EXPLAIN 的基本用法 要使用 EXP…

如何禁用鍵盤上的特定鍵或快捷方式?這里有詳細步驟

要禁用特定的鍵盤鍵或快捷鍵嗎&#xff1f;微軟官方應用程序Microsoft PowerToys使這項任務變得非常簡單。以下是使用Microsoft PowerToys中的鍵盤管理器禁用特定鍵或快捷方式的快速指南。 如果你還沒有安裝Microsoft PowerToys 如果你的設備上沒有安裝Microsoft PowerToys&a…

springboot上傳圖片

前端的name的值必須要和后端的MultipartFile 形參名一致 存儲本地

3.2、matlab單目相機標定原理、流程及實驗

1、單目相機標定流程及步驟 單目相機標定是通過確定相機的內部和外部參數,以便準確地在圖像空間和物體空間之間建立映射關系。下面是單目相機標定的流程及步驟: 搜集標定圖像:使用不同角度、距離和姿態拍攝一組標定圖像,并確保標定板(可以是棋盤格或者圓形標定板)完整可…

鴻蒙開發:Universal Keystore Kit(密鑰管理服務)【匿名密鑰證明(C/C++)】

匿名密鑰證明(C/C) 在使用本功能時&#xff0c;需確保網絡通暢。 在CMake腳本中鏈接相關動態庫 target_link_libraries(entry PUBLIC libhuks_ndk.z.so)開發步驟 確定密鑰別名keyAlias&#xff0c;密鑰別名最大長度為64字節&#xff1b;初始化參數集&#xff1a;通過[OH_Huk…

AcWing 667. 游戲時間

讀取兩個整數 A&#x1d434; 和 B&#x1d435;&#xff0c;表示游戲的開始時間和結束時間&#xff0c;以小時為單位。 然后請你計算游戲的持續時間&#xff0c;已知游戲可以在一天開始并在另一天結束&#xff0c;最長持續時間為 2424 小時。 如果 A&#x1d434; 與 B&…

css3 transform的旋轉和位移制作太陽花

css3 transform 實例展示知識點rotate 旋轉translate 位移transform: translate(300px,200px) rotate(90deg) 實例代碼 實例展示 知識點 transform的兩個屬性 rotate 旋轉 translate 位移 transform: translate(300px,200px) rotate(90deg) 實例代碼 <!DOCTYPE html&g…

flask 定時任務(APScheduler)使用current_app app_context()上下文

前言: 描述&#xff1a;flask定時任務調用的方法中使用了current_app.logger.info()記錄日志報錯 報錯代碼 raise RuntimeError(unbound_message) from None RuntimeError: Working outside of application context.This typically means that you attempted to use functiona…

IDEA中Git常用操作及Git存儲原理

Git簡介與使用 Intro Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Git是一款分布式版本控制系統&#xff08;VSC&#xff09;&#xff0c;是團隊合作開發…

算法學習筆記(8.3)-(0-1背包問題)

目錄 最常見的0-1背包問題&#xff1a; 第一步&#xff1a;思考每輪的決策&#xff0c;定義狀態&#xff0c;從而得到dp表 第二步&#xff1a;找出最優子結構&#xff0c;進而推導出狀態轉移方程 第三步&#xff1a;確定邊界條件和狀態轉移順序 方法一&#xff1a;暴力搜素…

KBS(Knowledge-Based Systems)期刊投稿記錄

記錄一些關鍵時間節點 2023.12.31 投稿 2024.01.30 返回審稿意見 2024.05.20 提交r1 2024.05.31 返回審稿意見(conditional accept)包括語言潤色 2024.06.09 提交r2&#xff0c;沒有使用愛思維爾的潤色 2024.06.10 with editor 2024.06.13 under review 2024.06.24 revise(折磨…

MFC之對話框--線寬/線型/顏色

文章目錄 線寬輸入實現優化無法記錄上一次線粗問題 線寬滑動實現實現選擇線類型實現顏色選擇總結 線寬輸入實現 優化無法記錄上一次線粗問題 線寬滑動實現 實現選擇線類型 實現顏色選擇 總結 1。創建新窗口&#xff08;dialog)會創建一個新的類&#xff0c;在類中實現窗口中的…

vue中父子傳遞屬性值

1、父傳子屬性值 自定義圖庫組件 在add.vue中應用tuku組件并給默認值 效果 2、 子傳父&#xff0c;逆向賦值 add.vue和第一問中一樣 修改tuku組件&#xff0c;傳值給add.vue 3、多個傳遞 效果&#xff1a; 點擊兩個修改按鈕后 4、使用defineModel簡化父子傳值 其他代碼跟…

【postgresql】時間函數和操作符

日期/時間操作符 加減操作符&#xff1a; 和 - 可以用于日期、時間、時間戳和時間間隔的加減操作。 SELECT 2024-01-01::date INTERVAL 1 day as "date"; ; -- 結果&#xff1a;2024-01-02SELECT 2024-01-01 12:00:00::timestamp - INTERVAL 2 hours as "…

概率論原理精解【2】

文章目錄 笛卡爾積任意笛卡爾積投影映射概述詳解一一、定義二、性質三、應用四、結論 詳解二定義與性質應用與意義示例結論 參考文獻 笛卡爾積 任意笛卡爾積 { A t , t ∈ T } \{A_t,t \in T\} {At?,t∈T}是一個集合族&#xff0c;其中T為一個非空指標集&#xff0c;稱 t ∈…

CSS上下懸浮特效

要實現一個上下懸浮的特效&#xff0c;可以使用CSS的keyframes規則和動畫屬性。以下是一個簡單的示例&#xff1a; 代碼示例 /* 定義一個名為floating的動畫 */ keyframes floating {0% {transform: translateY(0); /* 初始位置 */}50% {transform: translateY(-4px); /* 向上…

M1000 4G藍牙網關:高速穩定,賦能物聯網新體驗

桂花網M1000的4G移動網絡功能主要體現在以下幾個方面&#xff1a; 一、高速穩定的數據傳輸 高速率&#xff1a;M1000支持4G移動網絡&#xff0c;能夠實現高速的數據傳輸。根據4G網絡的技術標準&#xff0c;其理論上的最大下行速率可達到數百Mbps&#xff08;如TD-LTE在20MHz帶…

KALI使用MSF攻擊安卓設備

這期是kali使用MSF進行安卓滲透的保姆級別教程&#xff0c;話不多說&#xff0c;直接開始。 準備材料&#xff1a; 1.裝有kali的實體機或虛擬機&#xff08;這里用實體機進行演示&#xff09; 2.一臺安卓10.0以下的手機 打開kali&#xff0c;先用ifconfig查看自己的kali IP地址…

Python3極簡教程(一小時學完)下

目錄 PEP8 代碼風格指南 知識點 介紹 愚蠢的一致性就像沒腦子的妖怪 代碼排版 縮進 制表符還是空格 每行最大長度 空行 源文件編碼 導入包 字符串引號 表達式和語句中的空格 不能忍受的情況 其他建議 注釋 塊注釋 行內注釋 文檔字符串 版本注記 命名約定 …