spark将数据加载到hbase--bulkload方式

本文详细介绍了如何使用HBase的bulkload方式高效导入大量数据,包括读取数据生成RDD、预分区行键、配置HFileOutputFormat2、保存为HFile以及将HFile加载到HBase表中。在操作过程中需要注意行键的排序和唯一性,以及处理写入权限问题。这种方法不占用Region资源,适合大数据量导入。
摘要由CSDN通过智能技术生成

通过bulkload方式加载数据优点:与put方式相比
1.导入过程不占用Region资源
2.能快速导入海量的数据
3.节省内存
应该是业界将数据载入hbase常用方式之一,因此有必要学习掌握

实现步骤

步骤一 读取数据生成rdd

读入数据是面向行的表,一行有多个字段,需要转换成面向列的数据,构造keyValue对象,一定要注意key们要排序,比如user:age列要在user:gender列之前
需要设计行键保证行键唯一和避免数据都涌入一个region,如我的是按时间设计的,好几个月的数据,因此将数据按月预分区。

    val rdd = sc.textFile("file:///"+filePath)
      .flatMap(x=>getLineData(x,rowKeyBase,HBaseUtils.LOG_FIELD_NAMES))
      .sortByKey()
  //处理每一条记录生成keyvalue对象
  def getLineData(line:String,rowkey:String,fieldNames: TreeMap[String, Int]): List[(ImmutableBytesWritable, KeyValue)] ={
    val length = fieldNames.size
    val values:Array[String] = line.split("\\\t")
    if (null == values || values.length!=length) return Nil
    //println(rowkey+values(1)+Random.nextInt(100000).toString)
    val rowKey = Bytes.toBytes(rowkey+values(1)+Random.nextInt(1000).toString)
    val writable = new ImmutableBytesWritable(rowKey)
    val columnFamily = Bytes.toBytes("detail")
    fieldNames.toList.map{
      case (fieldName, fieldIndex) =>
        // KeyValue实例对象
        val keyValue = new KeyValue(
          rowKey, //
          columnFamily, //
          Bytes.toBytes(fieldName), //
          Bytes.toBytes(values(fieldIndex)) //
        )
        // 返回
        (writable, keyValue)
    }
  }

步骤二 配置输出HFile文件

输出前检查

检查HFile输出目录是否存在
    // TODO:构建Job,设置相关配置信息,主要为输出格式
    // a. 读取配置信息
    val hbaseConfig: Configuration = HBaseUtils.getHBaseConfiguration("hbase","2181")
    //  Configuration parameter hbase.mapreduce.hfileoutputformat.table.name cannot be empty
    hbaseConfig.set("hbase.mapreduce.hfileoutputformat.table.name", "log")
    // b. 如果输出目录存在,删除
    val dfs = FileSystem.get(hbaseConfig)
    val outputPath: Path = new Path("hdfs://hbase:9000/hbase/log/"+rowKeyBase)
    if (dfs.exists(outputPath)) {
      dfs.delete(outputPath, true)
    }
    dfs.close()
配置HFileOutputFormat2
    // TODO: 配置HFileOutputFormat2输出
    val conn = ConnectionFactory.createConnection(hbaseConfig)
    val htableName = TableName.valueOf("log")
    val table: Table = conn.getTable(htableName)
    HFileOutputFormat2.configureIncrementalLoad(
      Job.getInstance(hbaseConfig), //
      table, //
      conn.getRegionLocator(htableName) //
    )

输出HFile文件

    // TODO: 3. 保存数据为HFile文件//先排序
    rdd.sortBy(x=>(x._1, x._2.getKeyString), ascending = true)
      .saveAsNewAPIHadoopFile(
        "hdfs://hbase:9000/hbase/log/"+rowKeyBase,
        classOf[ImmutableBytesWritable], //
        classOf[KeyValue], //
        classOf[HFileOutputFormat2], //
        hbaseConfig)

将HFile文件bulkload到hbase表分区当中

    // TODO:4. 将输出HFile加载到HBase表中
    val load = new LoadIncrementalHFiles(hbaseConfig)
    load.doBulkLoad(outputPath, conn.getAdmin, table,
      conn.getRegionLocator(htableName))

出现的问题

写入权限
可以将HFile要输出的文件位置chmod 777 /outputDir

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

一加六

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

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

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

打赏作者

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

抵扣说明:

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

余额充值