Accessing HBase from Scala

Scala can call Hbase classes very easily as other JVM based languages (Jython, Jruby, Groovy). Scala may be a better choice than say Jython as Jython 2.5.1 does not ship any more with a jythonc compiler command, and if you want to do Hbase mapreduce integration, you will need compiled classes.

Setting Your Classpath

Set your classpath as described in Hbase/Jython for instance getting it from:

ps auwx|grep java|grep org.apache.hadoop.hbase.master.HMaster|perl -pi -e "s/.*classpath //"

Using SBT

Your build.sbt file will need this inside of it for your Scala client to work

resolvers += "Apache HBase" at ""

resolvers += "Thrift" at ""

libraryDependencies ++= Seq(
    "org.apache.hadoop" % "hadoop-core" % "0.20.2",
    "org.apache.hbase" % "hbase" % "0.90.4"

The Code

Please look at the Java API. Adaptation to scala should be relatively easy. The example below shows part of the sample Java code adapted to scala:

import org.apache.hadoop.hbase.HBaseConfiguration
import org.apache.hadoop.hbase.client.{HBaseAdmin,HTable,Put,Get}
import org.apache.hadoop.hbase.util.Bytes

val conf = new HBaseConfiguration()
val admin = new HBaseAdmin(conf)

// list the tables
val listtables=admin.listTables() 

// let's insert some data in 'mytable' and get the row

val table = new HTable(conf, "mytable")

val theput= new Put(Bytes.toBytes("rowkey1"))


val theget= new Get(Bytes.toBytes("rowkey1"))
val result=table.get(theget)
val value=result.value()

Hbase/Scala (last edited 2012-01-04 21:02:56 by Joe Stein)