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:
Now I still don't solve this problem, so I change to use thrift2 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))
}