手机版
你好,游客 登录 注册
背景:
阅读新闻

利用ruby通过Thrift接口批量插入HBase

[日期:2013-04-16] 来源:Linux社区  作者:jiedushi [字体:大 中 小]

因为工作中使用fluentd采集日志,需要使用ruby通过thrift接口插入数据到hbase中,网上搜索了一下,发现很多都是插入单个数据,没办法只好参考Python语言写的批量插入修改了代码,终于搞定。

$:.unshift File.expand_path('gen-rb', File.dirname(__FILE__))
require 'thrift'
require File.expand_path('gen-rb/hbase', File.dirname(__FILE__))
transport = Thrift::BufferedTransport.new(Thrift::Socket.new("localhost", "9090"))
transport.open
client = Apache::Hadoop::Hbase::Thrift::Hbase::Client.new(Thrift::BinaryProtocol.new(transport))

## Get Table meta data

def newpass( len )
    chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
    newpass = ""
    1.upto(len) { |i| newpass << chars[rand(chars.size-1)] }
    return newpass
end
columnvalue = "v"*10
batchmutations = []
starttime=Time.now
1000.times do |x|

  mutations=[]
  rowkey ="RK_" << Time.now.strftime('%Y%m%d_'<< newpass(15))
    5.times do |i|
        mutations << Apache::Hadoop::Hbase::Thrift::Mutation.new(:column => "f1:" << i.to_s,:value => columnvalue)
    end
    batchmutations << Apache::Hadoop::Hbase::Thrift::BatchMutation.new(:row => rowkey,:mutations => mutations)
end
client.mutateRows("testdb2", batchmutations)

linux
相关资讯       Ruby  Hbase  HBase Thrift 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款