天天看點

HIVE

hive源資料預設存儲在derby資料庫中,不支援多用戶端通路,是以需要将源資料存儲在mysql中,才支援多用戶端通路。主要架構如下:

HIVE

hive解析成mr的過程:

 hive通過給使用者提供一系列互動接口,接受到使用者的指令(sql語句),結合源資料(metastore),經過driver内的解析器、編譯器、優化器、執行器轉換成maoreduce(将sql轉換成抽象文法樹ast的解析器,将ast編譯成邏輯執行計劃的編譯器,在對邏輯執行計劃進行優化的優化器,最後将邏輯執行計劃轉換成mapreduce),送出給hadoop中執行,最後将執行傳回的結果輸出到使用者互動接口。

2.hive與傳統資料庫的差別

hive和資料庫除了使用者類型的查詢語言外,無其他相似

  1.存儲位置:hive資料是存儲在hdfs上。資料庫儲存在塊裝置或本地檔案系統

  2.資料更新:hive不建議對資料改寫。資料庫通常需要經常修改

  3.執行引擎:hive通過mapreduce來實作。資料庫用自己的執行引擎

  4.執行速度:hive執行延遲高,但它資料規模遠超過資料庫處理能力時,hive的并行計算能力就展現了優勢,資料庫執行延遲較低

  5.資料規模:hive大規模的資料計算,資料庫能支援的資料規模較小

  6.擴充性:hive建立在Hadoop上,随Hadoop的擴充性。資料庫由于ACID語義的嚴格限制,擴充有限

    A(atomicity)原子性:事務操作要麼全部做完,要麼不做。

    C(consistency)一緻性:事務運作期間,資料對象依然完整性

    I(isolation)獨立性:并發事務之間不會互相影響

    D(druability)持久性:一旦事務送出後,修改将永久儲存在資料庫上

3.Hive内部表和外部表的差別

  1.存儲:外部表資料又HDFS管理;内部表資料又Hive自身管理

  2.存儲:外部表資料存儲位置由自己指定(沒有指定lication則在預設位址下建立);内部表資料存儲在hive.metastore.warehouse.dir(預設在/usr/hive/warehouse)

  3.建立:被external修飾的就是外部表;沒被修飾的是内部表

  4.删除:删除外部表僅僅删除中繼資料;删除内部表會删除中繼資料和存儲資料

4.Hive中order by,sort by ,distribute by 和cluster by的差別

  1.order by :對資料進行全局排序,隻有一個reduce工作

  2.sort by:每個mapreduce中進行排序,一般和distribute by使用,且distribute by寫在sort by前面,當mapred.reduce.tasks=1時,效果和order by一樣

  3.distribute by:類似mr的partition,對key進行分區,結合sort by實作分區排序

  4.cluster by:當distribute by和sort by的字段相同時,可以使用cluster by代替,但cluster by隻能是升序,不能指定排序規則

  注意:在生産環境中order by使用的少,容易造成記憶體溢出(oom)

     生産環境中distribute by 和sort by用的多

5.row_number(),rank()和dense_rank()的差別

  row_number():根據查詢結果的順序計算排序,多用于分頁查詢

  rank():排序相同時序号重複,總序數不變

  dense_rank():排序相同的序号重複時,總序數減少

HIVE

6.hive中常用的系統函數

  1.date_add(str,n),date_sub(str,n) 加減時間

  2.next_day(to_date(str,'mo') 周名額相關,擷取str下周一日期

  3.date_format*=(str,'yyyy') 根據格式整理日期

  4.last_day(to_date(str)) 求當月最後一天日期

  5.collect_set(col)收集資料傳回一個以逗号分隔的字元串數組

  6.get_json_object(jsonstr,;$.object') 解析json,使用 $.object擷取對象值

  7.nvl(str,replace) 空字段指派,str為空傳回replace值;兩個都為空則傳回null

7.hive如何實作分區

  1.建表:create tbale tablename(col1 string) partitioned  by (col2 string);

  2.添加分區:alter table tablename add partition(col2='202101')

  3.删除分區:alter table tablename drop partition(col2='202101')

8.hive導入資料的五種方式

  1.load:可以從本地或hdfs上導入,本地是copy,hdfs是移動

  本地:load data local inpath '/root/student.txt' into table student;

  hdfs:load data inpath '/usr/hive/data/student.txt' into table student;

  2.insert:往表裡插入

  insert into table student values(1,'zhangsan');

  3.as select方式,根據查詢結果建立表并插入資料

  create table if not exists stu1 as select id,name from student;

  4.location,建立表并指定資料的路徑

  create external if not exists stu2 like student lication '/usr/hive/warehouse/student/student.txt';

  5.import,先從hive上使用export導出再導入

  import table stu3 from /usr/export/student'

9.hive導出資料的五種方式

  1.insert 查詢結果導出到本地或hdfs

  insert overwrite local directory '/root/insert/student' select id ,name from student;

  insert overwrite directory '/usr/insert/student' select id, name from student;

  2.hadoop指令導出本地

  hive> dfs -get /usr/hive/warehouse/student/00000_0  /root/hadoop/student.txt

  3.hive shell指令導出

  $ bin/hive -e 'select id,name from student;' >/root/hadoop/student.txt

  4.export導出到hdfs

  hive>export table student to '/usr/export/student';

  5.sqoop導出

10.自定義udf,udtf函數

用udf函數解析公共字段,用udtf函數解析事件字段

自定義udf:繼承udf,重寫evaluate方法

自定義udtf:繼承genericudtf,重寫3個方法:initialize(自定義輸出的列名和類型),process(将結果傳回forward(result)),close

hive自定義函數包括三種udf、udaf、udtf

  udf(user-defined-function) 一進一出

  udaf(user- defined aggregation funcation) 聚集函數,多進一出。count/max/min

  udtf(user-defined table-generating functions)  一進多出,如lateral view explore()

  使用方式 :在hive會話中add 自定義函數的jar檔案,然後建立function繼而使用函數

不要為了追逐,而忘記當初的樣子。