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?
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!
總結
以上是生活随笔為你收集整理的Scala Hbase 问题汇总的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spark从hbase读取写入数据
- 下一篇: Spark 下操作 HBase