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

Spark2.x写Hbase1-2.x

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

标签:spl   make   code   ado   job   reduce   out   Spark2   col   

import org.apache.hadoop.hbase.io.ImmutableBytesWritable
import org.apache.hadoop.hbase.mapreduce.TableOutputFormat
import org.apache.hadoop.hbase.client.Result
import org.apache.hadoop.hbase.client.Put
import org.apache.hadoop.mapreduce.Job
import org.apache.hadoop.hbase.util.Bytes
import org.apache.spark.{SparkConf, SparkContext}


/**
  * Spark写HBase
  */
object SparkWriteHbase {

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

    val conf = new SparkConf().setAppName("SparkWriteHBase").setMaster("local")
    val sc = new SparkContext(conf)
    val tableName = "student"


    sc.hadoopConfiguration.set(TableOutputFormat.OUTPUT_TABLE, tableName)

    val job = new Job(sc.hadoopConfiguration)

    job.setOutputKeyClass(classOf[ImmutableBytesWritable])
    job.setOutputValueClass(classOf[Result])
    job.setOutputFormatClass(classOf[TableOutputFormat[ImmutableBytesWritable]])



    val inDataRDD = sc.makeRDD(Array("3,Rongcheng,M,26","4,Guanhua,M,27"))

    val rdd = inDataRDD.map(_.split(",")).map(arr=>{
      val put = new Put(Bytes.toBytes(arr(0)))
      put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("name"),Bytes.toBytes(arr(1)))
      put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("gender"),Bytes.toBytes(arr(2)))
      put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("age"),Bytes.toBytes(arr(3)))
      (new ImmutableBytesWritable(),put)
    })

    rdd.saveAsNewAPIHadoopDataset(job.getConfiguration)


  }

}

 

Spark2.x写Hbase1-2.x

标签:spl   make   code   ado   job   reduce   out   Spark2   col   

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

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