大數(shù)據(jù)操作中涉及到數(shù)據(jù)清洗步奏還是用腳本處理比較方便,下邊介紹一下pig加載hdfs文件后調(diào)用ruby腳本處理數(shù)據(jù),再返回?cái)?shù)據(jù)流至pig中處理的一個(gè)簡單案例。
注意:ruby的流式處理用到wukong這個(gè)gem包,相關(guān)下載:
https://github.com/mrflip/wukong
pig中加載分布式文件調(diào)用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;調(diào)用定義的外部程序tra_parser處理log對(duì)象。
Wukong.run ParseClick::Mapper, nil執(zhí)行完后,將ruby執(zhí)行結(jié)果回調(diào)pig接收。
store strmo into '$OUTFILE' using PigStorage('/t');做結(jié)果存儲(chǔ)持久化。
新聞熱點(diǎn)
疑難解答
圖片精選