码迷,mamicode.com
首页 > 其他好文 > 详细

Spark2.x读Hbase1-2.x

时间:2020-04-22 09:16:07      阅读:55      评论:0      收藏:0      [点我收藏+]

标签:col   main   表数据   表数   mapr   park   spark   key   mapreduce   

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

标签:col   main   表数据   表数   mapr   park   spark   key   mapreduce   

原文地址:https://www.cnblogs.com/zxbdboke/p/12749533.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!