hivefans icon

hbase jruby

hivefans | PRO | 08/23/13 04:06:10 AM UTC | 0 ⭐ | 893 👁️ | Never ⏰ | []
Ruby |

1.16 KB

|

None

|

0 👍

/

0 👎

# Operating hBase by JRuby script.
# $ hbase org.jruby.Main a.rb
# http://wiki.apache.org/hadoop/Hbase/JRuby
 
include Java
import org.apache.hadoop.hbase.HBaseConfiguration
import org.apache.hadoop.hbase.client.Put
import org.apache.hadoop.hbase.client.Get
import org.apache.hadoop.hbase.client.Scan
import org.apache.hadoop.hbase.client.HTable
import org.apache.hadoop.hbase.util.Bytes
 
# 設定情報の読み取り
conf = HBaseConfiguration.create
conf.add_resource "/etc/hbase/conf/hbase-default.xml"
conf.add_resource "/etc/hbase/conf/hbase-site.xml"
 
# 操作対象の情報
key = Bytes.toBytes("row-by-jruby-client")
val = Bytes.toBytes("val")
 
# テーブル操作のためのクラス
table = HTable.new(conf, "sample")
 
# データの挿入
op_put = Put.new(key)
family = Bytes.toBytes("data")
column = Bytes.toBytes("column")
op_put.add(family, column, val)
table.put(op_put)
 
# データの取得
op_get = Get.new(key)
r = table.get(op_get)
puts r.to_s
 
# データのスキャン
scan = Scan.new
scanner = table.get_scanner(scan)
begin
  while (row = scanner.next()) do
    puts row.to_s
  end
ensure
  scanner.close
end

Comments