大數(shù)據(jù)操作中涉及到數(shù)據(jù)清洗步奏還是用腳本處理比較方便,下邊介紹一下pig加載hdfs文件后調(diào)用ruby腳本處理數(shù)據(jù),再返回數(shù)據(jù)流至pig中處理的一個簡單案例。
log = load '$INFILE' using PigStorage('\t');
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');
require 'wukong'
require 'json'
require './click_tra.rb'
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
require 'date'
require './models.rb'
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