乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      Hive學(xué)習(xí)之路 (十)Hive的高級操作

       HK123COM 2019-02-14

      目錄

      一、負(fù)責(zé)數(shù)據(jù)類型

      1、array

      2、map

      3、struct

      4、uniontype

      二、視圖

      1、Hive 的視圖和關(guān)系型數(shù)據(jù)庫的視圖區(qū)別

      2、Hive視圖的創(chuàng)建語句

      3、Hive視圖的查看語句

      4、Hive視圖的使用語句

      5、Hive視圖的刪除語句

      三、函數(shù)

      1、內(nèi)置函數(shù)

      2、自定義函數(shù)UDF

      (1) 簡單UDF示例

      (2) JSON數(shù)據(jù)解析UDF開發(fā)

      (3) Transform實現(xiàn)

      四、特殊分隔符處理

      1、使用RegexSerDe正則表達式解析

      2、通過自定義InputFormat處理特殊分隔符

      正文

      回到頂部

      一、負(fù)責(zé)數(shù)據(jù)類型

      1、array

      現(xiàn)有數(shù)據(jù)如下:

      1 huangbo guangzhou,xianggang,shenzhen a1:30,a2:20,a3:100 beijing,112233,13522334455,500

      2 xuzheng xianggang b2:50,b3:40 tianjin,223344,13644556677,600

      3 wangbaoqiang beijing,zhejinag c1:200 chongqinjg,334455,15622334455,20

      建表語句

      復(fù)制代碼

      use class;create table cdt( id int,  name string,  work_location array,  piaofang map,  address struct)  row format delimited  fields terminated by "\t"  collection items terminated by ","  map keys terminated by ":"  lines terminated by "\n";

      復(fù)制代碼

      導(dǎo)入數(shù)據(jù)

      0: jdbc:hive2://hadoop3:10000> load data local inpath "/home/hadoop/cdt.txt" into table cdt;

      查詢語句

      select * from cdt;

      select name from cdt;

      select work_location from cdt;

      select work_location[0] from cdt;

      select work_location[1] from cdt;

      2、map

      建表語句、導(dǎo)入數(shù)據(jù)同1

      查詢語句

      select piaofang from cdt;

      select piaofang["a1"] from cdt;

      3、struct

      建表語句、導(dǎo)入數(shù)據(jù)同1

      查詢語句

      select address from cdt;

      select address.location from cdt;

      4、uniontype

      很少使用

      參考資料:http://yugouai./blog/1849192

      回到頂部

      二、視圖

      1、Hive 的視圖和關(guān)系型數(shù)據(jù)庫的視圖區(qū)別

      和關(guān)系型數(shù)據(jù)庫一樣,Hive 也提供了視圖的功能,不過請注意,Hive 的視圖和關(guān)系型數(shù)據(jù)庫的數(shù)據(jù)還是有很大的區(qū)別:

      (1)只有邏輯視圖,沒有物化視圖;

      (2)視圖只能查詢,不能 Load/Insert/Update/Delete 數(shù)據(jù);

      (3)視圖在創(chuàng)建時候,只是保存了一份元數(shù)據(jù),當(dāng)查詢視圖的時候,才開始執(zhí)行視圖對應(yīng)的 那些子查詢

      2、Hive視圖的創(chuàng)建語句

      create view view_cdt as select * from cdt;

      3、Hive視圖的查看語句

      show views;desc view_cdt;-- 查看某個具體視圖的信息

      4、Hive視圖的使用語句

      select * from view_cdt;

      5、Hive視圖的刪除語句

      drop view view_cdt;

      回到頂部

      三、函數(shù)

      1、內(nèi)置函數(shù)

      具體可看http://www.cnblogs.com/qingyunzong/p/8744593.html

      (1)查看內(nèi)置函數(shù)

      show functions;

      (2)顯示函數(shù)的詳細(xì)信息

      desc function substr;

      (3)顯示函數(shù)的擴展信息

      desc function extended substr;

      2、自定義函數(shù)UDF

      當(dāng) Hive 提供的內(nèi)置函數(shù)無法滿足業(yè)務(wù)處理需要時,此時就可以考慮使用用戶自定義函數(shù)。

      UDF(user-defined function)作用于單個數(shù)據(jù)行,產(chǎn)生一個數(shù)據(jù)行作為輸出。(數(shù)學(xué)函數(shù),字 符串函數(shù))

      UDAF(用戶定義聚集函數(shù) User- Defined Aggregation Funcation):接收多個輸入數(shù)據(jù)行,并產(chǎn) 生一個輸出數(shù)據(jù)行。(count,max)

      UDTF(表格生成函數(shù) User-Defined Table Functions):接收一行輸入,輸出多行(explode)

      (1) 簡單UDF示例

      A. 導(dǎo)入hive需要的jar包,自定義一個java類繼承UDF,重載 evaluate 方法

      ToLowerCase.java

      復(fù)制代碼

      import org.apache.hadoop.hive.ql.exec.UDF;public class ToLowerCase extends UDF{         // 必須是 public,并且 evaluate 方法可以重載     public String evaluate(String field) {     String result = field.toLowerCase();    return result;     }      }

      復(fù)制代碼

      B. 打成 jar 包上傳到服務(wù)器

      C. 將 jar 包添加到 hive 的 classpath

      add JAR /home/hadoop/udf.jar;

      D. 創(chuàng)建臨時函數(shù)與開發(fā)好的 class 關(guān)聯(lián)起來

      0: jdbc:hive2://hadoop3:10000> create temporary function tolowercase as 'com.study.hive.udf.ToLowerCase';

      E. 至此,便可以在 hql 在使用自定義的函數(shù)

      0: jdbc:hive2://hadoop3:10000> select tolowercase('HELLO');

      (2) JSON數(shù)據(jù)解析UDF開發(fā)

      現(xiàn)有原始 json 數(shù)據(jù)(rating.json)如下

      {"movie":"1193","rate":"5","timeStamp":"978300760","uid":"1"}

      {"movie":"661","rate":"3","timeStamp":"978302109","uid":"1"}

      {"movie":"914","rate":"3","timeStamp":"978301968","uid":"1"}

      {"movie":"3408","rate":"4","timeStamp":"978300275","uid":"1"}

      {"movie":"2355","rate":"5","timeStamp":"978824291","uid":"1"}

      {"movie":"1197","rate":"3","timeStamp":"978302268","uid":"1"}

      {"movie":"1287","rate":"5","timeStamp":"978302039","uid":"1"}

      {"movie":"2804","rate":"5","timeStamp":"978300719","uid":"1"}

      {"movie":"594","rate":"4","timeStamp":"978302268","uid":"1"}

      現(xiàn)在需要將數(shù)據(jù)導(dǎo)入到 hive 倉庫中,并且最終要得到這么一個結(jié)果:

      該怎么做、???(提示:可用內(nèi)置 get_json_object 或者自定義函數(shù)完成)

      A. get_json_object(string json_string, string path)

      返回值: string  

      說明:解析json的字符串json_string,返回path指定的內(nèi)容。如果輸入的json字符串無效,那么返回NULL。  這個函數(shù)每次只能返回一個數(shù)據(jù)項。

      0: jdbc:hive2://hadoop3:10000> select get_json_object('{"movie":"594","rate":"4","timeStamp":"978302268","uid":"1"}','$.movie');

      創(chuàng)建json表并將數(shù)據(jù)導(dǎo)入進去

      0: jdbc:hive2://hadoop3:10000> create table json(data string);No rows affected (0.983 seconds) 0: jdbc:hive2://hadoop3:10000> load data local inpath '/home/hadoop/json.txt' into table json;No rows affected (1.046 seconds) 0: jdbc:hive2://hadoop3:10000>

      0: jdbc:hive2://hadoop3:10000> select . . . . . . . . . . . . . . .> get_json_object(data,'$.movie') as movie . . . . . . . . . . . . . . .> from json;

      B. json_tuple(jsonStr, k1, k2, ...)

      參數(shù)為一組鍵k1,k2……和JSON字符串,返回值的元組。該方法比 get_json_object 高效,因為可以在一次調(diào)用中輸入多個鍵

      復(fù)制代碼

      0: jdbc:hive2://hadoop3:10000> select . . . . . . . . . . . . . . .>   b.b_movie,. . . . . . . . . . . . . . .>   b.b_rate,. . . . . . . . . . . . . . .>   b.b_timeStamp,. . . . . . . . . . . . . . .>   b.b_uid   . . . . . . . . . . . . . . .> from json a . . . . . . . . . . . . . . .> lateral view json_tuple(a.data,'movie','rate','timeStamp','uid') b as b_movie,b_rate,b_timeStamp,b_uid;

      復(fù)制代碼

      (3) Transform實現(xiàn)

      Hive 的 TRANSFORM 關(guān)鍵字提供了在 SQL 中調(diào)用自寫腳本的功能。適合實現(xiàn) Hive 中沒有的 功能又不想寫 UDF 的情況

      具體以一個實例講解。

      Json 數(shù)據(jù): {"movie":"1193","rate":"5","timeStamp":"978300760","uid":"1"}

      需求:把 timestamp 的值轉(zhuǎn)換成日期編號

      1、先加載 rating.json 文件到 hive 的一個原始表 rate_json

      create table rate_json(line string) row format delimited; load data local inpath '/home/hadoop/rating.json' into table rate_json;

      2、創(chuàng)建 rate 這張表用來存儲解析 json 出來的字段:

      create table rate(movie int, rate int, unixtime int, userid int) row format delimited fields terminated by '\t';

      解析 json,得到結(jié)果之后存入 rate 表:

      復(fù)制代碼

      insert into table rate select get_json_object(line,'$.movie') as moive, get_json_object(line,'$.rate') as rate, get_json_object(line,'$.timeStamp') as unixtime, get_json_object(line,'$.uid') as userid from rate_json;

      復(fù)制代碼

      3、使用 transform+python 的方式去轉(zhuǎn)換 unixtime 為 weekday

      先編輯一個 python 腳本文件

      復(fù)制代碼

      ########python######代碼## vi weekday_mapper.py#!/bin/pythonimport sysimport datetimefor line in sys.stdin:  line = line.strip()  movie,rate,unixtime,userid = line.split('\t')  weekday = datetime.datetime.fromtimestamp(float(unixtime)).isoweekday() print '\t'.join([movie, rate, str(weekday),userid])

      復(fù)制代碼

      保存文件 然后,將文件加入 hive 的 classpath:

      hive>add file /home/hadoop/weekday_mapper.py;hive> insert into table lastjsontable select transform(movie,rate,unixtime,userid) using 'python weekday_mapper.py' as(movie,rate,weekday,userid) from rate;

      創(chuàng)建最后的用來存儲調(diào)用 python 腳本解析出來的數(shù)據(jù)的表:lastjsontable

      create table lastjsontable(movie int, rate int, weekday int, userid int) row format delimited fields terminated by '\t';

      最后查詢看數(shù)據(jù)是否正確

      select distinct(weekday) from lastjsontable;

      回到頂部

      四、特殊分隔符處理

      補充:hive 讀取數(shù)據(jù)的機制:

      1、 首先用 InputFormat<默認(rèn)是:org.apache.hadoop.mapred.TextInputFormat >的一個具體實 現(xiàn)類讀入文件數(shù)據(jù),返回一條一條的記錄(可以是行,或者是你邏輯中的“行”)

      2、 然后利用 SerDe<默認(rèn):org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe>的一個具體 實現(xiàn)類,對上面返回的一條一條的記錄進行字段切割

      Hive 對文件中字段的分隔符默認(rèn)情況下只支持單字節(jié)分隔符,如果數(shù)據(jù)文件中的分隔符是多 字符的,如下所示:

      01||huangbo

      02||xuzheng

      03||wangbaoqiang

      1、使用RegexSerDe正則表達式解析

      創(chuàng)建表

      create table t_bi_reg(id string,name string) row format serde 'org.apache.hadoop.hive.serde2.RegexSerDe' with serdeproperties('input.regex'='(.*)\\|\\|(.*)','output.format.string'='%1$s %2$s') stored as textfile;

      導(dǎo)入數(shù)據(jù)并查詢

      0: jdbc:hive2://hadoop3:10000> load data local inpath '/home/hadoop/data.txt' into table t_bi_reg;No rows affected (0.747 seconds) 0: jdbc:hive2://hadoop3:10000> select a.* from t_bi_reg a;

      2、通過自定義InputFormat處理特殊分隔符

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多