spark bulkload hbase笔记
1. 现有的三方包不能完全支持
- 官方:hbase-spark,不能设置 timestamp
- unicredit/hbase-rdd:接口太复杂,不能同时支持多个 family
2. HFile 得是有序的,排序依据 KeyValue.KVComparator,于是我们自定义一个 Comparator,内部调用 KeyValue.KVComparator
3. 如果没有自定义 partitioner,极有可能出现以下异常
ERROR: "java.io.IOException: Retry attempted 10 times without completing, bailing out"
https://community.hortonworks.com/content/supportkb/150138/error-javaioioexception-retry-attempted-10-times-w.html
自定义的方法,参考了:https://github.com/unicredit/hbase-rdd/blob/master/src/main/scala/unicredit/spark/hbase/HFileSupport.scala
4. 很多博客中有以下代码,一开始理解为可以用来对 rdd 分区,实际没有用。这是 mapreduce 的 job 参数,spark中不生效
val job = Job.getInstance(hbaseConfig)
HFileOutputFormat2.configureIncrementalLoad(job, table.getTableDescriptor, regionLocator)
job.getConfiguration
其他知识点:
1. scala 中实现 serializable 接口
2. HFilePartitioner,使用 hbase 的 regionLocator.getStartKeys,将 rdd 中的 put,按 rowkey 分割成不同的 partition,每个 partition 会产生一个 hfile,对应于 hbase region 的分区
代码,以后整理:
object BulkloadHelper {
private val logger = Logger.getLogger(this.getClass)
def bulkloadWrite(rdd: RDD[Put], hbaseConfig: Configuration, thisTableName: TableName): Unit = {
val hbaseConnection = ConnectionFactory.createConnection(hbaseConfig)
val regionLocator = hbaseConnection.getRegionLocator(thisTableName)
val myPartitioner = HFilePartitioner.apply(hbaseConfig, regionLocator.getStartKeys, 1)
logger.info(s"regionLocator.getStartKeys.length = ${regionLocator.getStartKeys.length}")
regionLocator.getStartKeys.foreach(keys => logger.info("regionLocator.getStartKeys: " + new String(keys)))
val hFilePath = getHFilePath()
logger.info(s"bulkload, begin to write to hdfs path: $hFilePath")
/**
* HFile sort function -> KeyValue.KVComparator
* CellComparator
*/
rdd.flatMap(put => putToKeyValueList(put))
.map(c => (c, 1))
.repartitionAndSortWithinPartitions(myPartitioner) // repartition so each hfile can match the hbase region
.map(tuple => (new ImmutableBytesWritable(tuple._1.row), tuple._1.getKeyValue()))
.saveAsNewAPIHadoopFile(
hFilePath,
classOf[ImmutableBytesWritable],
classOf[KeyValue],
classOf[HFileOutputFormat2],
hbaseConfig)
// Bulk load Hfiles to Hbase
logger.info("bulkload, begin to load to hbase")
val bulkLoader = new LoadIncrementalHFiles(hbaseConfig)
bulkLoader.doBulkLoad(new Path(hFilePath), new HTable(hbaseConfig, thisTableName))
logger.info("bulkload, delete hdfs path")
val hadoopConf = new Configuration()
val fileSystem = FileSystem.get(hadoopConf)
fileSystem.delete(new Path(hFilePath), true)
hbaseConnection.close()
fileSystem.close()
logger.info("bulkload, done")
}
def getHFilePath():String = "hdfs:///user/hadoop/hbase/bulkload/hfile/" + LocalDate.now().toString + "-" + UUID.randomUUID().toString
/**
* select one keyvalue from put
* @param put
*/
def putToKeyValueList(put: Put): Seq[MyKeyValue] = {
put.getFamilyCellMap.asScala
.flatMap(_._2.asScala) // list cells
.map(cell => new MyKeyValue(put.getRow, cell.getFamily, cell.getQualifier, cell.getTimestamp, cell.getValue))
.toSeq
}
}
class MyKeyValue(var row: Array[Byte], var family: Array[Byte], var qualifier: Array[Byte], var timestamp: Long, var value: Array[Byte])
extends Serializable with Ordered[MyKeyValue] { import java.io.IOException
import java.io.ObjectInputStream
import java.io.ObjectOutputStream var keyValue: KeyValue = _ def getKeyValue(): KeyValue = {
if (keyValue == null) {
keyValue = new KeyValue(row, family, qualifier, timestamp, value)
}
keyValue
} @throws[IOException]
private def writeObject(out: ObjectOutputStream) {
keyValue = null
out.defaultWriteObject()
out.writeObject(this)
} @throws[IOException]
@throws[ClassNotFoundException]
private def readObject(in: ObjectInputStream) {
in.defaultReadObject()
val newKeyValue = in.readObject().asInstanceOf[MyKeyValue]
this.row = newKeyValue.row
this.family = newKeyValue.family
this.qualifier = newKeyValue.qualifier
this.timestamp = newKeyValue.timestamp
this.value = newKeyValue.value
getKeyValue()
} class MyComparator extends KeyValue.KVComparator with Serializable {}
val comparator = new MyComparator() override def compare(that: MyKeyValue): Int = {
comparator.compare(this.getKeyValue(), that.getKeyValue())
} override def toString: String = {
getKeyValue().toString
}
}
object HFilePartitionerHelper {
object HFilePartitioner {
def apply(conf: Configuration, splits: Array[Array[Byte]], numFilesPerRegionPerFamily: Int): HFilePartitioner = {
if (numFilesPerRegionPerFamily == 1)
new SingleHFilePartitioner(splits)
else {
val fraction = 1 max numFilesPerRegionPerFamily min conf.getInt(LoadIncrementalHFiles.MAX_FILES_PER_REGION_PER_FAMILY, 32)
new MultiHFilePartitioner(splits, fraction)
}
}
}
protected abstract class HFilePartitioner extends Partitioner {
def extractKey(n: Any): Array[Byte] = {
// println(s"n = $n")
n match {
case kv: MyKeyValue => kv.row
}
}
}
private class MultiHFilePartitioner(splits: Array[Array[Byte]], fraction: Int) extends HFilePartitioner {
override def getPartition(key: Any): Int = {
val k = extractKey(key)
val h = (k.hashCode() & Int.MaxValue) % fraction
for (i <- 1 until splits.length)
if (Bytes.compareTo(k, splits(i)) < 0) return (i - 1) * fraction + h
(splits.length - 1) * fraction + h
}
override def numPartitions: Int = splits.length * fraction
}
private class SingleHFilePartitioner(splits: Array[Array[Byte]]) extends HFilePartitioner {
override def getPartition(key: Any): Int = {
val p = selfGetPartition(key)
// println(s"p = $p")
p
}
def selfGetPartition(key: Any): Int = {
val k = extractKey(key)
for (i <- 1 until splits.length)
if (Bytes.compareTo(k, splits(i)) < 0) return i - 1
splits.length - 1
}
override def numPartitions: Int = splits.length
}
}
spark bulkload hbase笔记的更多相关文章
- Spark、BulkLoad Hbase、单列、多列
背景 之前的博客:Spark:DataFrame写HFile (Hbase)一个列族.一个列扩展一个列族.多个列 用spark 1.6.0 和 hbase 1.2.0 版本实现过spark BulkL ...
- Spark操作HBase问题:java.io.IOException: Non-increasing Bloom keys
1 问题描述 在使用Spark BulkLoad数据到HBase时遇到以下问题: 17/05/19 14:47:26 WARN scheduler.TaskSetManager: Lost task ...
- MapReduce和Spark写入Hbase多表总结
作者:Syn良子 出处:http://www.cnblogs.com/cssdongl 转载请注明出处 大家都知道用mapreduce或者spark写入已知的hbase中的表时,直接在mapreduc ...
- spark 操作hbase
HBase经过七年发展,终于在今年2月底,发布了 1.0.0 版本.这个版本提供了一些让人激动的功能,并且,在不牺牲稳定性的前提下,引入了新的API.虽然 1.0.0 兼容旧版本的 API,不过还是应 ...
- Spark操作hbase
于Spark它是一个计算框架,于Spark环境,不仅支持单个文件操作,HDFS档,同时也可以使用Spark对Hbase操作. 从企业的数据源HBase取出.这涉及阅读hbase数据,在本文中尽快为了尽 ...
- 大数据学习系列之九---- Hive整合Spark和HBase以及相关测试
前言 在之前的大数据学习系列之七 ----- Hadoop+Spark+Zookeeper+HBase+Hive集群搭建 中介绍了集群的环境搭建,但是在使用hive进行数据查询的时候会非常的慢,因为h ...
- Spark 基本函数学习笔记一
Spark 基本函数学习笔记一¶ spark的函数主要分两类,Transformations和Actions. Transformations为一些数据转换类函数,actions为一些行动类函数: ...
- Spark读Hbase优化 --手动划分region提高并行数
一. Hbase的region 我们先简单介绍下Hbase的架构和Hbase的region: 从物理集群的角度看,Hbase集群中,由一个Hmaster管理多个HRegionServer,其中每个HR ...
- spark读写hbase性能对比
一.spark写入hbase hbase client以put方式封装数据,并支持逐条或批量插入.spark中内置saveAsHadoopDataset和saveAsNewAPIHadoopDatas ...
随机推荐
- fastjson数据返回配置
阿里的fastjson 包升级后,可能导致返回的json 数据,字段为null时不显示等问题 <dependency> <groupId>com.alibaba</gro ...
- 【PAT甲级】1101 Quick Sort (25 分)
题意: 输入一个正整数N(<=1e5),接着输入一行N个各不相同的正整数.输出可以作为快速排序枢纽点的个数并升序输出这些点的值. trick: 测试点2格式错误原因:当答案为0时,需要换行两次
- 【PAT甲级】1091 Acute Stroke (30 分)(BFS)
题意: 输入四个正整数M,N,K,T(K<=60,M<=1286,N<=128),代表每片的高度和宽度,片数和最小联通块大小.输出一共有多少个单元满足所在联通块大小大于等于T. tr ...
- 【C#】图解如何添加引用using MySql.Data.MySqlClient;
使用C#连接MySQL时,经常会用到命名空间using MySql.Data.MySqlClient; 这说明VS中没有添加引用,解决方法如下: 1,可点击以下两个链接的其中任何一个,下载MySQL. ...
- Nexus 3048的NX-OS升级方法
1.System Software和Kick Start 与普通IOS设备不同,NX OS升级时,共有2个文件需要升级.安装,如果只安装其中一个,可能会导致设备重启后无法进入系统.这2个文件包括: N ...
- 【layui】提交表单
1 <script type="text/javascript"> layui.use(['form', 'layer', 'jquery'], function () ...
- iOS 根据域名查询 IP 地址
在 iOS 开发中,如果需要知道网站的 IP 地址: #include <netdb.h> #include <arpa/inet.h> NSString *webSiteSt ...
- JavaScript中关于隐式转换的一些总结
JavaScript运算符中的隐式转换规律:一.递增递减运算符(前置.后置)1.如果包含的是有效数字字符串或者是有效浮点数字符串,则会将字符串转换(Number())为数值,再进行加减操作,返回值的类 ...
- 安装mysql常见错误解决方法
Mysql ERROR 1698 (28000) 解决 解决mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exi ...
- Django 学习之Rest Framework 视图集与Routers与扩展功能
一.视图集使用 使用视图集ViewSet,可以将一系列逻辑相关的动作放到一个类中: list() 提供一组数据 retrieve() 提供单个数据 create() 创建数据 update() 保存数 ...