Batch insert HBase using ruby through the Thrift Interface

Source: Internet
Author: User
Because fluentd is used to collect logs, you need to use ruby to insert data to hbase through the thrift interface. After searching online, you find that many of them insert a single data, no

Because fluentd is used to collect logs, you need to use ruby to insert data to hbase through the thrift interface. After searching online, you find that many of them insert a single data, no

Because fluentd is used to collect logs at work, you need to use ruby to insert data to hbase through the thrift interface. After searching online, you find that many of them insert a single data, there is no way to modify the code by referring to batch inserts written in 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.