网上有很多spark生成HFIle文件导入到HBase的例子,但是参考网上例子再结合自己的程序依然有很多bug,之前采用如下这种方式:
val tableName= PRE_WTB + dataDate //表名
createTable(conf, wzktb, Array(CF), 2) //创建hbase表
conf.set(TableOutputFormat.OUTPUT_TABLE, tableName)
val wzktbRDD = rdd
.flatMap(s => {
val cl = s.split("\t",-1)
val hm = “”
val kh = “”
val rowKey = timeSection
val put: Put = new Put(Bytes.toBytes(rowKey))
val famliy = Bytes.toBytes("cf") // HBase 列族是固定的
if(hm != "" && kh != ""){
put.addColumn(famliy, Bytes.toBytes("1_" + hm), Bytes.toBytes(""))
put.addColumn(famliy, Bytes.toBytes("2_" + kh), Bytes.toBytes(""))
}else if(hm == ""){
put.addColumn(famliy, Bytes.toBytes("2_" + kh), Bytes.toBytes(""))
}else {
put.addColumn(famliy, Bytes.toBytes("1_" + hm), Bytes.toBytes(""))
}
Array(Tuple2(String.valueOf(System.currentTimeMillis()), put))
})
wzktbRDD.saveAsNewAPIHadoopFile(wzktb, classOf[String], classOf[Put], classOf[TableOutputFormat[String]], conf)
在数据量不大的情况下这种入库方式完全可以满足业务需求,但当数据量达到几亿十几亿数量级别后入库时间就到了小时级别,严重影响性能。
在网上参考其大神的博客尝试采用doBulkLoad方式,参考网上的例子写了如下代码:
val conf = HBaseConfiguration.create()
val conn = ConnectionFactory.createConnection(conf)
val tableName= PRE_WTB + dataDate;//表名
val table = conn.getTable(TableName.valueOf(tableName))
val regionLocator = conn.getRegionLocator(TableName.