通过 scala spark 连接到远程 hbase

Connection to remote hbase through scala spark

我正在尝试通过 scala 和 spark 连接到远程 Hbase。无法成功。 谁能提出与此相关的任何方法。

提前致谢。

我们有两种方法可以从 spark/scala

连接到 HBASE
  1. HBASE 休息 Api
  2. phoenix.apache -- https://phoenix.apache.org/

Hbase RestAPI代码

        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)
               }
            }
            }

阿帕奇凤凰

Phoenix 也提供 spark 插件和 JDBC 连接。

spark 插件 - https://phoenix.apache.org/phoenix_spark.html

JDBC 连接(查询服务器)- https://phoenix.apache.org/server.html

我上周遇到了类似的问题。最终我使用 HBase Spark 连接器实现了它。确实有点setup/configuration。我在下面 link 中记录了我的步骤

Setup Apache Zeppelin with Spark and HBase