Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Hadoop Wiki" for change 
notification.

The following page has been changed by Tim Dysinger:
http://wiki.apache.org/hadoop/Hbase/JRuby

New page:
== Accessing HBase trunk from JRuby 1.1.1 ==

This page describes the process of connecting to HBase from JRuby.  The code 
mostly follows the [http://wiki.apache.org/hadoop/Hbase/FAQ#1 Can someone give 
an example of basic API-usage going against hbase?] example listed in the HBase 
FAQ.

== Start HBase ==

{{{
bin/hbase master start
}}}

== Get JRuby 1.1.1 ==

{{{
curl -O 
http://repo1.maven.org/maven2/org/jruby/jruby-complete/1.1.1/jruby-complete-1.1.1.jar
}}}
-or-
{{{
wget 
http://repo1.maven.org/maven2/org/jruby/jruby-complete/1.1.1/jruby-complete-1.1.1.jar
}}}

== Set your CLASSPATH ==

{{{
export CLASSPATH=`java -jar jruby-complete-1.1.1.jar -e "puts 
Dir.glob('{.,build,lib}/*.jar').join(':')"`
}}}


== The Code ==

Once you've got that set it's as simple as just translating the Java on the FAQ 
page to JRuby.

The code below creates a table, puts some data in it, fetches that data back 
out and then deletes the table.

Save and run the example as "helloworld.rb" and run it with 'jruby' from the 
base of HBase svn trunk.

{{{
java -jar jruby-complete-1.1.1.jar helloworld.rb
}}}


or open an interactive shell 'jirb' and paste it in

{{{
java -jar jruby-complete-1.1.1.jar -S jirb
}}}

{{{
#!/usr/bin/env jruby

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.hbase.io.BatchUpdate
import org.apache.hadoop.io.Text

conf = HBaseConfiguration.new
tablename = "test"
tablename_text = Text.new(tablename)
desc = HTableDescriptor.new(tablename)
desc.addFamily(HColumnDescriptor.new("content:"))
desc.addFamily(HColumnDescriptor.new("anchor:"))
admin = HBaseAdmin.new(conf)
if admin.tableExists(tablename_text)
  admin.disableTable(tablename_text)
  admin.deleteTable(tablename_text) 
end
admin.createTable(desc)
tables = admin.listTables
table = HTable.new(conf, tablename_text)
row = Text.new("row_x")
b = BatchUpdate.new(row)
b.put(Text.new("content:"), "some content")
table.commit(b)
data = table.get(row, Text.new("content:"))
data_str = java.lang.String(data, "UTF8")
print "The fetched row contains the value '#{data_str}'"
admin.deleteTable(desc.getName)
}}}

Reply via email to