Spark2.x读Hbase1-2.x

Posted zxbdboke

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spark2.x读Hbase1-2.x相关的知识,希望对你有一定的参考价值。

import org.apache.hadoop.hbase.HBaseConfiguration
import org.apache.hadoop.hbase.mapreduce.TableInputFormat
import org.apache.hadoop.hbase.util.Bytes
import org.apache.spark.{SparkConf, SparkContext}

/**
  * 读取HBase表数据
  */
object SparkOperateHBase {

  def main(args: Array[String]): Unit = {

    val conf = HBaseConfiguration.create()
    val sc = new SparkContext(new SparkConf())

    conf.set(TableInputFormat.INPUT_TABLE,"student")

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

    stuRDD.cache()

    val count = stuRDD.count()
    println("Students RDDCount: " + count)

    //读取HBase表数据并打印出来
    stuRDD.foreach({case (_,result) =>
      val key = Bytes.toString(result.getRow)
      val name = Bytes.toString(result.getValue("info".getBytes,"name".getBytes()))
      val gender = Bytes.toString(result.getValue("info".getBytes,"gender".getBytes()))
      val age = Bytes.toString(result.getValue("info".getBytes,"age".getBytes()))
      println("Row key:" + key + " Name: " + name + " Gender: " + gender + " Age: " + age)
    })

    //读取HBase表数据并转为RDD
    val resRDD = stuRDD.map(res => {
      val key = Bytes.toString(res._2.getRow)
      val name = Bytes.toString(res._2.getValue("info".getBytes,"name".getBytes()))
      val gender = Bytes.toString(res._2.getValue("info".getBytes,"gender".getBytes()))
      val age = Bytes.toString(res._2.getValue("info".getBytes,"age".getBytes()))
      (key, name, gender, age)
    })

  }

}

 

以上是关于Spark2.x读Hbase1-2.x的主要内容,如果未能解决你的问题,请参考以下文章

Spark2.x(六十):在Structured Streaming流处理中是如何查找kafka的DataSourceProvider?

Spark2.x 新特性

Spark2.x 与 Spark1.x 关系

Spark2.x(六十二):(Spark2.4)Broadcast原理分析

Spark2.X 傻瓜教程

Spark2.x+Python大数据机器学习视频课程