Spark写入HBase(BulkLoad方式)

4 篇文章 0 订阅

在使用Spark时经常需要把数据落入HBase中,如果使用普通的Java API,写入会速度很慢。Spark提供了Bulk写入方式的接口。那么Bulk写入与普通写入相比有什么优势呢?

  • BulkLoad不会写WAL,也不会产生flush以及split。
  • 如果我们大量调用PUT接口插入数据,可能会导致大量的GC操作。除了影响性能之外,严重时甚至可能会对HBase节点的稳定性造成影响。但是采用Bulk就不会有这个顾虑。
  • 过程中没有大量的接口调用消耗性能
/**
  * GDPR数据脱敏
  * 1、将铭文的的设备ID进行MD5加密
  * 2、增量更新到HBase
  * 3、同步一份数据到HIVE
  */
object DimDevInfoPro extends AnalysysLogger{

  def main(args: Array[String]): Unit = {
    Logger.getLogger("org.apache.spark").setLevel(Level.OFF)

    if (args.length < 2) {
      throw new IllegalArgumentException("Need to 2 args!!!")
    }
    val Array(proDate, proTableName) = args
    val spark = SparkSession.builder().enableHiveSupport.getOrCreate()
    val sc = spark.sparkContext

    val tablename = "dim_device_id_info"
    sc.hadoopConfiguration.set(TableOutputFormat.OUTPUT_TABLE, tablename)

    val job = Job.getInstance(sc.hadoopConfiguration)
    job.setOutputKeyClass(classOf[ImmutableBytesWritable])
    job.setOutputValueClass(classOf[Result])
    job.setOutputFormatClass(classOf[TableOutputFormat[ImmutableBytesWritable]])

    // 获取待更新的设备
    var dim_device_id_info_sql = s"select md5(a.device_id) di_md5 ,a.device_id di from(select device_id from ${proTableName} where day=${proDate}  and device_id is not null and length(device_id)>15 and length(device_id)<100 group by 1) a left join dim.dim_device_id_info b on b.di=a.device_id where b.di is null"
    if(proTableName.equals("source.source_dev_filter")){
       dim_device_id_info_sql = s"select md5(a.device_id) di_md5 ,a.device_id di from(select device_id from source.source_dev_filter where day=${proDate} and status=0 and device_id is not null and length(device_id)>15 and length(device_id)<100 group by 1) a left join dim.dim_device_id_info b on b.di=a.device_id where b.di is null"
    }
    logger.info("dim_device_id_info_sql: " + dim_device_id_info_sql)
    val dim_device_id_info = spark.sql(dim_device_id_info_sql)
    // 更新hbase
    val rdd = dim_device_id_info.rdd.map { x => {
      val di_md5 = x.getAs("di_md5").toString
      val di = x.getAs("di").toString

      val put = new Put(di_md5.getBytes()) //行健的值
      put.addColumn(Bytes.toBytes("dev"), Bytes.toBytes("di"), di.getBytes()) //dev:di列的值
      (new ImmutableBytesWritable, put)
    }
    }
    rdd.saveAsNewAPIHadoopDataset(job.getConfiguration())

    // 同步到hive表中方便查询
    spark.sql("insert overwrite table dim.dim_device_id_info select * from dim.dim_device_id_info_hbase")
    spark.stop()
  }

}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值