Search code examples
apache-sparkhbase

Connection to remote hbase through scala spark


I am trying to connect to remote Hbase through scala and spark. Unable to succeeded. Can anyone suggest any methods related to this.

Thanks in advance.


Solution

  • we have two methods to connect to HBASE from spark/scala

    1. HBASE Rest Api
    2. phoenix.apache -- https://phoenix.apache.org/

    Hbase Rest API code

            val hbaseCluster  = new org.apache.hadoop.hbase.rest.client.Cluster()
                        hbaseCluster.add("localhost or UP", <port>)
                        val restClient = new Client(hbaseCluster)
                         val table =  new RemoteHTable(restClient, "STUDENT")
                         println("connected...")
                        var p = new Put(Bytes.toBytes("row1"))
    
                  p.add(Bytes.toBytes("0"), Bytes.toBytes("NAME"),Bytes.toBytes("raju"))
                  p.add(Bytes.toBytes("0"), Bytes.toBytes("COURSE"),Bytes.toBytes("SCALA"))
                  p.add(Bytes.toBytes("0"), Bytes.toBytes("YEAR"),Bytes.toBytes("2017"))
                  table.put(p)
    
                  val scan = new Scan()
                  val scanner : ResultScanner = table.getScanner(scan)
                  println("got scanner...")
                  val g = new Get(Bytes.toBytes("row1"))
                  val result = table.get(g)
    
                  val name = Bytes.toString(result.getValue(Bytes.toBytes("0"),Bytes.toBytes("NAME")))
                  val course = Bytes.toString(result.getValue(Bytes.toBytes("0"),Bytes.toBytes("COURSE")))
                  val year = Bytes.toString(result.getValue(Bytes.toBytes("0"),Bytes.toBytes("YEAR")))
    
                  println("row1 " + "name: " + name + " course: " + course + " year:" + year);
    
    
                  for (result  <- scanner) {
                          var userId = Bytes.toString(result.getValue("NAME".getBytes(), "ID".getBytes()))
                           println("userId " + userId)
                   }
                }
                }
    

    Apache Phoenix

    Phoenix provides spark plugin and JDBC connection as well.

    spark plugin - https://phoenix.apache.org/phoenix_spark.html

    JDBC Connection (query server)- https://phoenix.apache.org/server.html