Search code examples
scalahadoopsbthbase

HBase with Scala class Connection* cannot be imported


I'm trying to compile and run scala example from Hbase documentation http://hbase.apache.org/1.2/book.html#scala but I'm getting compilation errors that Connection and ConnectionFactory classes cannot be imported.

I've tried two scala versions 2.10.6 and 2.11.11 but it fails in both cases.

Simple Scala code:

import org.apache.hadoop.hbase.HBaseConfiguration
import org.apache.hadoop.hbase.client.Connection
import org.apache.hadoop.hbase.client.ConnectionFactory

object Client {

        def main(args: Array[String]): Unit = {
                val conf = new HBaseConfiguration()
                val connection = ConnectionFactory.createConnection(conf)
                val admin = connection.getAdmin()

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

Sbt file (sbt version 0.13.15):

name := "HBaseScalaClient"

scalaVersion := "2.10.6"

resolvers += "Apache HBase" at "https://repository.apache.org/content/repositories/releases"

resolvers += "Thrift" at "http://people.apache.org/~rawson/repo/"

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

Compilation error:

[error] /home/user/HBaseScala/Client.scala:2: object Connection is not a member of package org.apache.hadoop.hbase.client
[error] import org.apache.hadoop.hbase.client.Connection
[error]        ^
[error] /home/user/HBaseScala/Client.scala:3: object ConnectionFactory is not a member of package org.apache.hadoop.hbase.client
[error] import org.apache.hadoop.hbase.client.ConnectionFactory
[error]        ^
[error] /home/user/HBaseScala/Client.scala:9: not found: value ConnectionFactory
[error]                 val connection = ConnectionFactory.createConnection(conf)
[error]                                  ^
[error] three errors found
[error] (compile:compileIncremental) Compilation failed
[error] Total time: 1 s, completed May 12, 2017 3:30:48 PM

Any ideas what I'm missing or what's wrong with this code?


Solution

  • Try with this library dependencies:

    libraryDependencies ++= Seq(
    "org.apache.hadoop" % "hadoop-core" % "1.2.1",
    "org.apache.hbase" % "hbase" % "1.2.0",
    "org.apache.hbase" % "hbase-client" % "1.2.0",
    "org.apache.hbase" % "hbase-common" % "1.2.0",
    "org.apache.hbase" % "hbase-server" % "1.2.0"
    )
    

    And add this imports:

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