Scala Hbase 问题汇总

1.
object hbase is not a member of package org.apache.hadoop when compiling scala 
在Scala工程中使用HBase API,import hbase时,

import org.apache.hadoop.hbase

出现编译错误

解决(大概,没有尝试): 
使用sbt构建工程时,添加依赖: 
hbase-client,hbase-common,hbase-server然后sbt assembly

libraryDependencies ++=Seq(
    "org.apache.hbase" % "hbase-server" % "0.99.2",
    "org.apache.hbase" % "hbase-common" % "0.99.2"
    "org.apache.hbase" % "hbase-client" % "0.99.2"

2.
Spark读取Hbase中的数据 
加入如下依赖:

libraryDependencies ++= Seq(
        "org.apache.spark" % "spark-core_2.10" % "0.9.1",
        "org.apache.hbase" % "hbase" % "0.98.2-hadoop2",
        "org.apache.hbase" % "hbase-client" % "0.98.2-hadoop2",
        "org.apache.hbase" % "hbase-common" % "0.98.2-hadoop2",
        "org.apache.hbase" % "hbase-server" % "0.98.2-hadoop2"
)

在测试的时候,需要配置好Hbase、Hadoop环境,否则程序会出现问题,特别是让程序找到Hbase-site.xml配置文件。

import org.apache.spark._
import org.apache.spark.rdd.NewHadoopRDD
import org.apache.hadoop.hbase.{HBaseConfiguration, HTableDescriptor}
import org.apache.hadoop.hbase.client.HBaseAdmin
import org.apache.hadoop.hbase.mapreduce.TableInputFormat

 

object HBaseTest {
  def main(args: Array[String]) {
    val sc = new SparkContext(args(0), "HBaseTest",
      System.getenv("SPARK_HOME"), SparkContext.jarOfClass(this.getClass))

    val conf = HBaseConfiguration.create()
    conf.set(TableInputFormat.INPUT_TABLE, args(1))

    val hBaseRDD = sc.newAPIHadoopRDD(conf, classOf[TableInputFormat], 
      classOf[org.apache.hadoop.hbase.io.ImmutableBytesWritable],
      classOf[org.apache.hadoop.hbase.client.Result])

    hBaseRDD.count()

    System.exit(0)
  }
}

3.
How to read from hbase using spark 
使用Spark(Scala)读取HBase的基本范例:

mport org.apache.hadoop.hbase.client.{HBaseAdmin, Result}
import org.apache.hadoop.hbase.{ HBaseConfiguration, HTableDescriptor }
import org.apache.hadoop.hbase.mapreduce.TableInputFormat
import org.apache.hadoop.hbase.io.ImmutableBytesWritable

import org.apache.spark._

object HBaseRead {
  def main(args: Array[String]) {
    val sparkConf = new SparkConf().setAppName("HBaseRead").setMaster("local[2]")
    val sc = new SparkContext(sparkConf)
    val conf = HBaseConfiguration.create()
    val tableName = "table1"

    System.setProperty("user.name", "hdfs")
    System.setProperty("HADOOP_USER_NAME", "hdfs")
    conf.set("hbase.master", "localhost:60000")
    conf.setInt("timeout", 120000)
    conf.set("hbase.zookeeper.quorum", "localhost")
    conf.set("zookeeper.znode.parent", "/hbase-unsecure")
    conf.set(TableInputFormat.INPUT_TABLE, tableName)

    val admin = new HBaseAdmin(conf)
    if (!admin.isTableAvailable(tableName)) {
      val tableDesc = new HTableDescriptor(tableName)
      admin.createTable(tableDesc)
    }

    val hBaseRDD = sc.newAPIHadoopRDD(conf, classOf[TableInputFormat], classOf[ImmutableBytesWritable], classOf[Result])
    println("Number of Records found : " + hBaseRDD.count())
    sc.stop()
  }
}

4.
Spark 下操作 HBase(1.0.0 新 API) 
这个个人博客外观挺好的

5.
How to configure hbase in spark? 
Q:Spark连接HBase的步骤是什么?是不是只要把HBase的地址添加到spark的classpath? 
A:No,事实上,你需要把HBase的配置文件加到Spark的classpath(put the HBase configuration files in the Spark classpath),如果不这样,你应该在代码中设置,如:

Configuraiton hConf = HBaseConfiguration.create(conf);
hConf.set("hbase.zookeeper.quorum","PDHadoop1.corp.CompanyName.com,PDHadoop2.corp.CompanyName.com");
hConf.setInt("hbase.zookeeper.property.clientPort",10000);

Q:如何将hbase configuration加入spark classpath,是不是像这样:export SPARK_CLASSPATH=/path/to/hbaes/conf? 
A:对,但这只能在driver里用。
--------------------- 
作者:power0405hf 
来源:CSDN 
原文:https://blog.csdn.net/power0405hf/article/details/49906773 
版权声明:本文为博主原创文章,转载请附上博文链接!

你可能感兴趣的:(Hbase,spark)