ruby和pig处理流式文件实例

发布时间:2022-04-19 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了ruby和pig处理流式文件实例脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

大数据操作中涉及到数据清洗步奏还是用脚本处理比较方便,下边介绍一下pig加载hDFs文件后调用ruby脚本处理数据,再返回数据流至pig中处理的一个简单案例。

注意:ruby的流式处理用到wukong这个gem包,相关下载:
https://gIThub.COM/mrflip/wukong

pig中加载分布式文件调用ruby流式处理:

复制代码 代码如下:

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
&nbsp; 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

其中

strmo = stream log through tra_parser;调用定义的外部程序tra_parser处理log对象。
Wukong.run ParseClick::Mapper, nil执行完后,将ruby执行结果回调pig接收。
store strmo into '$OUTFILE' using PigStorage('\t');做结果存储持久化。

脚本宝典总结

以上是脚本宝典为你收集整理的ruby和pig处理流式文件实例全部内容,希望文章能够帮你解决ruby和pig处理流式文件实例所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:rubyruby