Distribute Cloud Environment on Ubuntu 14.04 with Docker

Operate HBase 1.0.0 via Local Scala App

I've created a simple sbt project to link HBase via client API.

Dependencies

build.sbt:
// All dependencies needed
libraryDependencies ++= Seq(
  "junit" % "junit" % "4.11",
  "org.apache.commons" % "commons-lang3" % "3.4",
  "commons-logging" % "commons-logging" % "1.2",
  // The hadoop client version is needed to be equal to server.
  "org.apache.hadoop" % "hadoop-client" % "2.5.1", 
  "org.apache.hbase" % "hbase" % "1.0.0",
  "org.apache.hbase" % "hbase-common" % "1.0.0",
  "org.apache.hbase" % "hbase-client" "1.0.0"
)

The following example use native Java API to connect to HBase, but there are still some zookeeper issues made me not manipulate HBase CRUD.

This is a simple example using native Java API to put data into HBase

class HBaseUtils {
  // The IP is set as "hbase.zookeeper.quorum" property in your hbase-site.xml
  private val zookeeperQuorumIP = "<ZOOKEEPER_QUORUM_IP>"

  def getConnection = ConnectionFactory.createConnection(setConf)

  def setConf = {
    val config = HBaseConfiguration.create()
    config.set("hbase.zookeeper.quorum",zookeeperQuorumIP)
    config
  }
}
object Main {
  val con = new HBaseUtils().getConnection
  val tableName = TableName.valueOf("table1")
  try {
    val t = con.getTable(tableName)
    Try(t.put(new Put("row-key").add("cf", "qual", "v1")))
      .getOrElse(t.close())
    t.close()  
    println("Link to HBase successfully.")
  }
  catch {
    case e: Exception => e.printStackTrace()
  }
  finally {
    con.close()
  }
}

There are many discussions on stackoverflow about this issue:

  1. http://stackoverflow.com/questions/6614413/how-to-connect-to-remote-hbase-in-java
  2. http://stackoverflow.com/questions/29534390/hbase-client-1-0-get-data-failed
  3. http://stackoverflow.com/questions/24219258/connecting-to-a-remote-hbase-instance
  4. http://stackoverflow.com/questions/18541301/cannot-connect-to-hbase-server-from-java-code

Now I still don't solve this problem, so I change to use thrift2 to manipulate HBase

Via thrift2 daemon to manipulate HBase

If you prefer other programming language despite Java, you can manipulate HBase via thrift2 daemon.

It's very easy to start thrift2 daemon on HBase with the following command:

hbase-daemon.sh start thrift2
build.sbt
// Append this statement in libraryDependencies sequencies
"org.apache.hbase" % "hbase-thrift" % "1.0.0"

object Main {
  private val hbaseURL = "192.168.1.31"
  private val thrift2Port = 9090

  def main(args: Array[String]) {
    val socket = new TSocket(hbaseURL, thrift2Port)
    try {
      val client = new Client(new TBinaryProtocol(socket, true, true))
      socket.open()
      val raw = client.get("t1", new TGet("r1"))
      println("Get raw: " + raw)
    }
    catch {
      case e: Exception => e.printStackTrace()
    }
    finally {
      socket.close()
    }
  }

  implicit private def str2ByteBuffer(str: String):ByteBuffer = 
    ByteBuffer.wrap(Bytes.toBytes(str))
}