zoukankan      html  css  js  c++  java
  • 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)
        })
    
      }
    
    }
  • 相关阅读:
    实验七---类的多态
    实验六
    实验五---排序、质数
    实验四---杨辉三角
    node中间件KOA函数
    java文件名判断练习
    npm install 安装报错错误问题
    bundle is not defined 手动搭建项目架构(一)
    ztree实现拖拽功能
    js单线程 详解 来自知乎
  • 原文地址:https://www.cnblogs.com/zxbdboke/p/12749533.html
Copyright © 2011-2022 走看看