91偷拍精品一区二区三区_欧美亚洲免费在线_日本不卡高清视频_中文字幕无码一区二区视频_日韩毛片无码一区二区三区,将夜 猫腻 小说,欢乐颂第二季,如何发布网络小说

?
    開發技術 / Technology
    您的當前位置:網站首頁 > 行業洞察 > 開發技術

    一個簡單數據處理例子

    日期:2015年1月29日  作者:zhjw  來源:互聯網    點擊:948

      1、Pig數據模型

        Bag:表

        Tuple:行,記錄

        Field:屬性

        Pig不要求同一個Bag里面的各個Tuple有相同數量或相同類型的Field

      2、Pig Lating常用語句

        1)LOAD:指出載入數據的方法

        2)FOREACH:逐行掃描進行某種處理

        3)FILTER:過濾行

        4)DUMP:把結果顯示到屏幕

        5)STORE:把結果保存到文件

      3、簡單例子:

        假如有一份成績單,有學號、語文成績、數學成績,屬性之間用|分隔,如下:

    20130001|80|90
    20130002|85|96
    20130003|60|70
    20130004|74|86
    20130005|65|98

      1)把文件從本地系統上傳到Hadoop

    [coder@h1 hadoop-0.20.2]$ bin/hadoop dfs -put /home/coder/score.txt in

      查看是否上傳成功:

    [coder@h1 hadoop-0.20.2]$ bin/hadoop dfs -ls /user/coder/in
    Found 1 items
    -rw-r--r--   2 coder supergroup         75 2013-04-20 14:33 /user/coder/in/score.txt

      2)載入原始數據,使用LOAD

    grunt> scores = LOAD 'hdfs://h1:9000/user/coder/in/score.txt' USING PigStorage('|') AS (num:int,Chinese:int,Math:int);

      輸入文件是:'hdfs://h1:9000/user/coder/in/score.txt'

      表名(Bag):scores

      從輸入文件讀取數據(Tuple)時以 | 分隔

      讀取的Tuple包含3個屬性,分別為學號(num)、語文成績(Chinese)和數學成績(Math),這三個屬性的數據類型都為int

      3)查看表的結構

    grunt> DESCRIBE scores;
    scores: {num: int,Chinese: int,Math: int}

      4)假如我們需要過濾掉學號為20130005的記錄

    grunt> filter_scores = FILTER scores BY num != 20130005;

      查看過濾后的記錄

    grunt> dump filter_scores;
    (20130001,80,90)
    (20130002,85,96)
    (20130003,60,70)
    (20130004,74,86)

      5)計算每個人的總分

    grunt> totalScore = FOREACH scores GENERATE num,Chinese+Math;

      查看結果:

    grunt> dump totalScore;

     

    (20130001,170)
    (20130002,181)
    (20130003,130)
    (20130004,160)
    (20130005,163)

      

      6)將每個人的總分結果輸出到文件

    grunt> store totalScore into 'hdfs://h1:9000/user/coder/out/result' using PigStorage('|');

      查看結果:

    復制代碼
    [coder@h1 ~]$ hadoop dfs -ls /user/coder/out/result
    Found 2 items
    drwxr-xr-x   - coder supergroup          0 2013-04-20 15:54 /user/coder/out/result/_logs
    -rw-r--r--   2 coder supergroup         65 2013-04-20 15:54 /user/coder/out/result/part-m-00000
    [coder@h1 ~]$ ^C
    [coder@h1 ~]$ hadoop dfs -cat /user/coder/out/result/*
    20130001|170
    20130002|181
    20130003|130
    20130004|160
    20130005|163
    cat: Source must be a file.
    [coder@h1 ~]$ 
    復制代碼

     


      再看一個小例子:

      有一批如下格式的文件:

    zhangsan#123456#zhangsan@qq.com
    lisi#434dfdds#lisi@126.com
    wangwu#ffere233#wangwu@163.com
    zhouliu#fgrtr43#zhouliu@139.com

      每行記錄有三個字段:賬號、密碼、郵箱,字段之間以#號分隔,現在要提取這批文件中的郵箱。

      

      1)上傳文件到hadoop

    [coder@h1 hadoop-0.20.2]$ bin/hadoop dfs -put data.txt in

     

    [coder@h1 hadoop-0.20.2]$ bin/hadoop dfs -ls /user/coder/in
    Found 1 items
    -rw-r--r--   2 coder supergroup        122 2013-04-24 20:34 /user/coder/in/data.txt
    [coder@h1 hadoop-0.20.2]$ 

      2)載入原始數據文件

    grunt> T_A = LOAD '/user/coder/in/data.txt' using PigStorage('#') as (username:chararray,password:chararray,email:chararray);

      3)取出email字段

    grunt> T_B = FOREACH T_A GENERATE email;

      4)把結果輸出到文件

    grunt> STORE T_B INTO '/user/coder/out/email'

      5)查看結果

    [coder@h1 hadoop-0.20.2]$ bin/hadoop dfs -cat /user/coder/out/email/*
    zhangsan@qq.com
    lisi@126.com
    wangwu@163.com
    zhouliu@139.com
    cat: Source must be a file.

     

    主站蜘蛛池模板: 呼图壁县| 冷水江市| 满城县| 昌黎县| 吕梁市| 西充县| 南召县| 马关县| 铜山县| 长乐市| 绵竹市| 靖州| 曲阜市| 台南市| 马鞍山市| 吴江市| 玉山县| 封丘县| 竹溪县| 曲阜市| 寻甸| 明光市| 油尖旺区| 鹤峰县| 宝山区| 灌南县| 和平县| 三都| 磐石市| 汤阴县| 洪江市| 房产| 富锦市| 连城县| 集贤县| 垣曲县| 合江县| 靖州| 龙岩市| 高雄市| 永吉县|