package HBase
import org.apache.hadoop.hbase.HBaseConfiguration
import org.apache.hadoop.hbase.client.Result
import org.apache.hadoop.hbase.io.ImmutableBytesWritable
import org.apache.hadoop.hbase.mapreduce.TableInputFormat
import org.apache.spark.{SparkConf, SparkContext}
import org.apache.hadoop.hbase.util.Bytes
object SparkReadHBase {
/**
* 此demo可以获取HBase中所有数据并转化成RDD
* @param args
*/
def main(args: Array[String]): Unit = {
val conf: SparkConf = new SparkConf()
.setAppName("HBaseTest")
.setMaster("local[*]")
val sc = new SparkContext(conf)
val tablename = "myspace:test"
val hbaseconf = HBaseConfiguration.create()
//设置zookeeper集群地址,
hbaseconf.set("hbase.zookeeper.quorum","tianxiang01,tianxiang03,tianxiang04,tianxiang05")
//设置zookeeper连接端口,默认是2181
hbaseconf.set("hbase.zookeeper.property.clientPort","2181")
hbaseconf.set(TableInputFormat.INPUT_TABLE,tablename)
//读取数据并转化成RDD
val HBaseRDD = sc.newAPIHadoopRDD(
hbaseconf,
classOf[TableInputFormat],
classOf[ImmutableBytesWritable],
classOf[Result]
)
val count = HBaseRDD.count()
println(count) // 1
HBaseRDD.foreach{
case (_,result) => {
val key = Bytes.toString(result.getRow)
val ziduan = Bytes.toString(result.getValue("ziduan".getBytes(),"ziduan".getBytes))
println(key+" "+ziduan) // 1 12.19
}
}
}
}