Executing Java methods from HBase Shell

 

HBase Shell is written in JRuby. As JRuby runs within Java Virtual Machine (JVM) , it is very easy to execute Java methods from HBase Shell. HBase ships with many Java utility classes; the ability to execute Java methods from HBase Shell makes it possible to import and use these utilities directly from HBase Shell.

 

HBase Shell 使用JRuby编写。JRuby运行在Java虚拟机内(JVM),使的在HBase Shell 中运行java程序变得非常简单,HBase附带了许多Java工具类;我们可以直接从HBase shell中引入并使用java的类库。

 

首先确保hbase启动正常,查看进程,运行jps,保证这三个进程正常启动

19467 HMaster
19744 HRegionServer
19394 HQuorumPeer

启动hbase shell

> hbase shell

 

查看加载的类库:

jruby-1.7.19 :008 > $LOAD_PATH
 => [#<Pathname:/usr/hbase-cdh/lib/ruby>, "file:/usr/hbase-cdh/lib/jruby-complete-1.6.8.jar!/META-INF/jruby.home/lib/ruby/site_ruby/1.8", "file:/usr/hbase-cdh/lib/jruby-complete-1.6.8.jar!/META-INF/jruby.home/lib/ruby/site_ruby/shared", "file:/usr/hbase-cdh/lib/jruby-complete-1.6.8.jar!/META-INF/jruby.home/lib/ruby/1.8", "."]

 

相应的jar包已经在加载路径中,在程序中可以直接引用,在shell中直接输入如下内容:

 

include Java

import org.apache.hadoop.hbase.HBaseConfiguration
import org.apache.hadoop.hbase.HColumnDescriptor
import org.apache.hadoop.hbase.HConstants
import org.apache.hadoop.hbase.HTableDescriptor
import org.apache.hadoop.hbase.client.HBaseAdmin
import org.apache.hadoop.hbase.client.HTable
import org.apache.hadoop.io.Text

 


conf = HBaseConfiguration.new
tablename = "test_links"

desc = HTableDescriptor.new(tablename)
desc.addFamily(HColumnDescriptor.new("core"))
desc.addFamily(HColumnDescriptor.new("meta"))
desc.addFamily(HColumnDescriptor.new("stats"))

 

在hbase中创建表
admin = HBaseAdmin.new(conf)
if admin.tableExists(tablename)
   admin.disableTable(tablename)
   admin.deleteTable(tablename)
end

admin.createTable(desc)

tables = admin.listTables
p [:tables, tables]

 

以上是在hbase shell中运行的结果,下面我们来来看如何直接使用jruby脚本来访问hbase,不同的是我们需要手动的去加载相应的jar包,才能正常的引入,完整代码如下:

 

include Java

hbase_home = '/usr/hbase-cdh'
jar_paths = Dir.glob(File.join(File.dirname("#{hbase_home}/lib/"),"lib","*.jar"))

jar_paths.each {|jar| require jar}

import org.apache.hadoop.hbase.HBaseConfiguration
import org.apache.hadoop.hbase.HColumnDescriptor
import org.apache.hadoop.hbase.HConstants
import org.apache.hadoop.hbase.HTableDescriptor
import org.apache.hadoop.hbase.client.HBaseAdmin
import org.apache.hadoop.hbase.client.HTable
import org.apache.hadoop.io.Text

conf = HBaseConfiguration.new
tablename = "test_links"

desc = HTableDescriptor.new(tablename)
desc.addFamily(HColumnDescriptor.new("core"))
desc.addFamily(HColumnDescriptor.new("meta"))
desc.addFamily(HColumnDescriptor.new("stats"))

admin = HBaseAdmin.new(conf)
if admin.tableExists(tablename)
   admin.disableTable(tablename)
   admin.deleteTable(tablename)
end

admin.createTable(desc)

tables = admin.listTables
p [:tables, tables]

 

测试:

jruby -S jruby_connect_hbase.rb

 

[:tables, org.apache.hadoop.hbase.HTableDescriptor['mac-history', {NAME => 'info', DATA_BLOCK_ENCODING => 'FAST_DIFF', BLOOMFILTER => 'ROW', REPLICATION_SCOPE => '0', VERSIONS => '1', COMPRESSION => 'NONE', MIN_VERSIONS => '0', TTL => 'FOREVER', KEEP_DELETED_CELLS => 'false', BLOCKSIZE => '16384', IN_MEMORY => 'false', BLOCKCACHE => 'true'}, 'shop-blacklist', {NAME => 'info', DATA_BLOCK_ENCODING => 'FAST_DIFF', BLOOMFILTER => 'ROW', REPLICATION_SCOPE => '0', VERSIONS => '1', COMPRESSION => 'NONE', MIN_VERSIONS => '0', TTL => 'FOREVER', KEEP_DELETED_CELLS => 'false', BLOCKSIZE => '16384', IN_MEMORY => 'false', BLOCKCACHE => 'true'}, 'shop-daily-counter', {NAME => 'info', DATA_BLOCK_ENCODING => 'FAST_DIFF', BLOOMFILTER => 'ROW', REPLICATION_SCOPE => '0', VERSIONS => '50', COMPRESSION => 'NONE', MIN_VERSIONS => '0', TTL => 'FOREVER', KEEP_DELETED_CELLS => 'false', BLOCKSIZE => '16384', IN_MEMORY => 'false', BLOCKCACHE => 'true'}, 'shop-daily-profile', {NAME => 'info', DATA_BLOCK_ENCODING => 'FAST_DIFF', BLOOMFILTER => 'ROW', REPLICATION_SCOPE => '0', VERSIONS => '1', COMPRESSION => 'NONE', MIN_VERSIONS => '0', TTL => 'FOREVER', KEEP_DELETED_CELLS => 'false', BLOCKSIZE => '16384', IN_MEMORY => 'false', BLOCKCACHE => 'true'}, 'test_links', {NAME => 'core', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROW', REPLICATION_SCOPE => '0', VERSIONS => '1', COMPRESSION => 'NONE', MIN_VERSIONS => '0', TTL => 'FOREVER', KEEP_DELETED_CELLS => 'false', BLOCKSIZE => '65536', IN_MEMORY => 'false', BLOCKCACHE => 'true'}, {NAME => 'meta', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROW', REPLICATION_SCOPE => '0', VERSIONS => '1', COMPRESSION => 'NONE', MIN_VERSIONS => '0', TTL => 'FOREVER', KEEP_DELETED_CELLS => 'false', BLOCKSIZE => '65536', IN_MEMORY => 'false', BLOCKCACHE => 'true'}, {NAME => 'stats', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROW', REPLICATION_SCOPE => '0', VERSIONS => '1', COMPRESSION => 'NONE', MIN_VERSIONS => '0', TTL => 'FOREVER', KEEP_DELETED_CELLS => 'false', BLOCKSIZE => '65536', IN_MEMORY => 'false', BLOCKCACHE => 'true'}]@89c9ec7]


Executing Java methods from HBase Shell
 

 

你可能感兴趣的:(hbase,jruby)