大數據操作中涉及到數據清洗步奏還是用腳本處理比較方便,下邊介紹一下pig加載hdfs文件后調用ruby腳本處理數據,再返回數據流至pig中處理的一個簡單案例。
注意:ruby的流式處理用到wukong這個gem包,相關下載:
https://github.com/mrflip/wukong
pig中加載分布式文件調用ruby流式處理:
define tracking_parser `/usr/ruby parse_click.rb --map` SHIP('parse_click.rb', 'click_tracking.rb');
strmo = stream log through tra_parser;
store strmo into '$OUTFILE' using PigStorage('/t');
module ParseClick
class Mapper < Wukong::Streamer::RecordStreamer
def before_stream
@bad_count = 0
end
def after_stream
raise RuntimeError, "Exceeded bad records : #{@bad_count}" if @bad_count > 10
end
def process *records
yield ClickTra.new(JSON.parse(records[2])).to_a
rescue => e
@bad_count += 1
warn "Bad record #{e}: #{records[2]}"
end
end
end
Wukong.run ParseClick::Mapper, nil
class ClickTra
output :ip
output :c_date
#output your other atrributes
def c_date
click_date.strftime("%Y%m%d").to_i
end
def ip
browser_ip.to_i
end
end
其中
strmo = stream log through tra_parser;調用定義的外部程序tra_parser處理log對象。
Wukong.run ParseClick::Mapper, nil執行完后,將ruby執行結果回調pig接收。
store strmo into '$OUTFILE' using PigStorage('/t');做結果存儲持久化。
新聞熱點
疑難解答
圖片精選