spark从hbase读数据到存入hbase数据两种版本写法

spark2版本:

object SparkCoreTest {
  def main(args: Array[String]): Unit = {
    // 使用sparksession来创建对象
    val sparkConf: SparkConf = new SparkConf().setMaster("local[*]").setAppName("SparkCoreTest")
    val spark: SparkSession = SparkSession.builder().config(sparkConf).getOrCreate()
    // 设置读表和写表
    val readTable: String = "hydrogenation_flow_record"
    val writeTable: String = "test200"

    // 创建hbase输入的配置文件,并且把服务器上的hbase-site放进resources目录下
    val hBaseConfRead: Configuration = HBaseConfiguration.create()
    // inputtable代表是读数据的配置
    hBaseConfRead.set(TableInputFormat.INPUT_TABLE, readTable)


    //配置写入表,要定义一个Jobconf,与读表不同
    val hBaseConfWrite: Configuration = HBaseConfiguration.create()
    val jobConf = new JobConf(hBaseConfWrite)
    jobConf.setOutputFormat(classOf[TableOutputFormat])
    jobConf.set(TableOutputFormat.OUTPUT_TABLE, writeTable);

    // 创建hadooprdd算子,出来的rdd为一个元组对象,第一个元素类型为ImmutableBytesWritable,所以写入时也要转成同样的转子
    val hbaseRDD: RDD[(ImmutableBytesWritable, Result)] = spark.sparkContext.newAPIHadoopRDD(hBaseConfRead, classOf[TableInputFormat],
      classOf[ImmutableBytesWritable],
      classOf[Result])
    // 导入toDF变成dataframe的隐式依赖,让下面可以用toDF方法
    import spark.implicits._

    val sps: DataFrame = hbaseRDD.map(r => (
      Bytes.toString(r._2.getValue(Bytes.toBytes("SPSFlowInfo"), Bytes.toBytes("SPSFlowTotal"))),
      Bytes.toString(r._2.getRow)
    )).toDF("SPSFlowTotal", "row")
    // 创建出来的dataframe进行命名
    sps.createOrReplaceTempView("sps")
    // 执行sql语句
    val frame: DataFrame = spark.sql("SELECT sum(SPSFlowTotal) as A FROM sps WHERE row BETWEEN '4000069:1618539744390' and '4000069:1618539744426'")
    // 将查到的数据组装成元组类型,元组的第一个为qualifier,元组的第二个是从dataframe里读到的数据
    val tupleDS: Dataset[(String, String)] = frame.map(t => ("SPSFlowTotal", t(0).toString))
    // 配置输出到hbase的rdd,新建一个put,第一个为row,第二个为具体列,具体列可以填写列族列,值,可以同时加多个列
    val rdd: RDD[(ImmutableBytesWritable, Put)] = tupleDS.rdd.map { a => {
      val put: Put = new Put(Bytes.toBytes("34353454353"))
      put.addColumn(Bytes.toBytes("info"), Bytes.toBytes(a._1.toString), Bytes.toBytes(a._2))
      // 封装成元组时第一个必须为ImmutableBytesWritable,符合spark和hadoop的规范
      (new ImmutableBytesWritable, put)
    }
    }
    // 执行保存操作
    rdd.saveAsHadoopDataset(jobConf)
    // 关闭session
    spark.stop()
  }

}

spark老版本:

object SparkCoreTest {
  def main(args: Array[String]): Unit = {
    val sparkConf: SparkConf = new SparkConf().setMaster("local[*]").setAppName("SparkCoreTest")
//    val spark: SparkSession = SparkSession.builder().config(sparkConf).getOrCreate()
    val sc: SparkContext = new SparkContext(sparkConf)

    val hBaseConf = HBaseConfiguration.create()
    hBaseConf.set(TableInputFormat.INPUT_TABLE, "hydrogenation_flow_record")
        val sqlContext = new SQLContext(sc)
        import sqlContext.implicits._
//    import spark.implicits._
    val hBaseConf1 = HBaseConfiguration.create()
    val jobConf = new JobConf(hBaseConf1)
    jobConf.setOutputFormat(classOf[TableOutputFormat])
    jobConf.set(TableOutputFormat.OUTPUT_TABLE, "test200");


    val hbaseRDD: RDD[(ImmutableBytesWritable, Result)] = sc.newAPIHadoopRDD(hBaseConf, classOf[TableInputFormat],
      classOf[ImmutableBytesWritable],
      classOf[Result])

    val sps: DataFrame = hbaseRDD.map(r => (
      Bytes.toString(r._2.getValue(Bytes.toBytes("SPSFlowInfo"), Bytes.toBytes("SPSFlowTotal"))),
      Bytes.toString(r._2.getRow)
    )).toDF("SPSFlowTotal", "row")
    //    sps.registerTempTable("sps")
    sps.createOrReplaceTempView("sps")

    val frame: DataFrame = sqlContext.sql("SELECT sum(SPSFlowTotal) as A FROM sps WHERE row BETWEEN '4000069:1618539744390' and '4000069:1618539744426'")
    val tupleDS: Dataset[(String, String)] = frame.map(t => ("SPSFlowTotal", t(0).toString))

    val rdd: RDD[(ImmutableBytesWritable, Put)] = tupleDS.rdd.map { a => {
      val put: Put = new Put(Bytes.toBytes("343534543533".toString))
      put.addColumn(Bytes.toBytes("info"), Bytes.toBytes(a._1.toString), Bytes.toBytes(a._2))
      (new ImmutableBytesWritable, put)
    }
    }
    rdd.saveAsHadoopDataset(jobConf)
    spark.stop()
  }

}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

graceful coding

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值