使用TableSnapshotInputFormat读取Hbase快照数据
根据快照名称读取hbase快照中的数据,在网上查了好多资料,很少有资料能够给出清晰的方案,根据自己的摸索终于实现,现将代码贴出,希望能给大家有所帮助:
public void read(org.apache.hadoop.conf.Configuration hadoopConf, Pipeline pipeline, ReaderParam readerParam, int batchSize) {
limiter = RateLimiter.create(readerParam.getFetchSize() * M_BYTE_SIZE); //用于记录读取行数
AtomicInteger totalCount = new AtomicInteger(); JobConf conf = new JobConf(hadoopConf);
String sourceRcFilePath = readerParam.getFilePath();
logger.info(String.format("Start Read Rcfile [%s].", sourceRcFilePath));
String defaultFS=String.format("hdfs://%s", readerParam.getFsdefaultname()); try {
int size = 1;
BatchData batchData;
List<Record> recordList = new ArrayList<>(batchSize); Scan scan = new Scan();
scan.setCaching(500);
scan.setCacheBlocks(false); //离线任务必须设置
conf.set(TableInputFormat.SCAN, Base64.encodeBytes(ProtobufUtil.toScan(scan).toByteArray())); //序列化
InputFormat<ImmutableBytesWritable, Result> in = new TableSnapshotInputFormat();
Path rootDir = FSUtils.getRootDir(conf);
String[] tableNameSplit = readerParam.getFileName().split(":");
String namespace_table = tableNameSplit[0]+"_"+tableNameSplit[1]; Connection conn = ConnectionFactory.createConnection(conf);
Admin admin = conn.getAdmin();
boolean tableExist = admin.tableExists(TableName.valueOf(readerParam.getFileName()));
// List<HBaseProtos.SnapshotDescription> list = admin.listSnapshots("^"+namespace_table); // TableName[] tables = admin.listTableNames();
// List<HBaseProtos.SnapshotDescription> list = admin.listSnapshots();
// for(HBaseProtos.SnapshotDescription snapshotDescription : list){
// String snapshotName = snapshotDescription.getMsg();
// String table = snapshotDescription.getTable();
// } FileSystem fs = FileSystem.get(conf);
Path rootPath = new Path(conf.get("hbase.rootdir"));
Path snapshotDir = new Path(conf.get("hbase.rootdir")+HBASE_SNAPSHOT_BASE_PATH);
snapshotDir = SnapshotDescriptionUtils.getSnapshotRootDir(new Path(conf.get("hbase.rootdir")));
FileStatus[] listStatus = fs.listStatus(snapshotDir); // HBaseProtos.SnapshotDescription snapshotDescription = SnapshotDescriptionUtils.readSnapshotInfo(fs, new Path(conf.get("hbase.rootdir")+"/.snapshots/completed"));
// Arrays.stream(listStatus).forEach(x-> System.out.println(x.getPath().toString()));
// System.out.println("-----------------------------------------"); List<String> snapshotList = new ArrayList<String>();
Arrays.stream(listStatus).filter(x-> !x.getPath().getName().startsWith(".")).forEach(x->{
String snapshotName = x.getPath().getName();
Path snapshotPath = SnapshotDescriptionUtils.getCompletedSnapshotDir(snapshotName, rootPath);
try {
HBaseProtos.SnapshotDescription s = SnapshotDescriptionUtils.readSnapshotInfo(fs, snapshotPath);
System.out.println("tableName:"+ s.getTable()+"\t snapshot:"+s.getName());
if (s.getTable().equalsIgnoreCase(readerParam.getFileName())){
snapshotList.add(s.getName());
}
} catch (CorruptedSnapshotException e) {
e.printStackTrace();
}
});
// List<String> snapshotList = Arrays.stream(listStatus).filter(x-> !x.getPath().getMsg().startsWith(".")).map(x -> String.valueOf(x.getPath())).filter(x -> x.contains(namespace_table)).sorted(Comparator.reverseOrder()).collect(Collectors.toList());
snapshotList.stream().forEach(x -> System.out.println(x));
if (snapshotList.isEmpty()){
String message = String.format("读取Hbase快照信息发生异常,没有找到对应表快照,请联系系统管理员。", readerParam.getFilePath());
logger.error(message);
throw DiException.asDiException(CommonErrorCode.CONFIG_ERROR, message);
}
String snapshotName = snapshotList.stream().sorted(Comparator.reverseOrder()).findFirst().get();
String restorTmp = String.format("%s/user/%s/restoretmp/%s", conf.get("fs.defaultFS"), "di", namespace_table);
Path restorPath = new Path(restorTmp);
// Path restorPath = new Path("hdfs://RouterSit/user/di/restoretmp/ns_di_snapshot_test2");
TableSnapshotInputFormatImpl.setInput(conf, snapshotName, restorPath); List<String> columns = Arrays.asList(readerParam.getReadColumns().split(",")); //Each file as a split
InputSplit[] splits = in.getSplits(conf, 1);
for (InputSplit split : splits){ recordReader = in.getRecordReader(split, conf, Reporter.NULL);
ImmutableBytesWritable key = recordReader.createKey();
Result value = recordReader.createValue(); List<Object> recordFields;
while (start && recordReader.next(key, value)) { Record record = result2Record(value, columns);
limiter.acquire(record.getMemorySize());
recordList.add(record);
size++;
}
} } catch (Exception e) {
String message = String.format("读取Hbase快照数据发生异常,请联系系统管理员。", readerParam.getFilePath());
logger.error(message);
throw DiException.asDiException(CommonErrorCode.CONFIG_ERROR, message, e);
} finally {
stop();
} }
如果读取快照数据时,数据列簇使用的是lzo压缩的话,可能会遇到lzo解压缩问题,可以参照:hbase读取快照数据-lzo压缩遇到的问题
使用TableSnapshotInputFormat读取Hbase快照数据的更多相关文章
- Spark读取Hbase的数据
val conf = HBaseConfiguration.create() conf.addResource(new Path("/opt/cloudera/parcels/CDH-5.4 ...
- hbase与hive集成:hive读取hbase中数据
1.创建hbase jar包到hive lib目录软连接 hive需要jar包: hive-hbase-handler-0.13.1-cdh5.3.6.jar zookeeper-3.4.5-cdh5 ...
- hbase读取快照数据-lzo压缩遇到的问题
1.读取hbase快照数据时报UnsatisfiedLinkError: no gplcompression in java.library.path错: 2019-09-04 17:36:07,44 ...
- Spark 读取HBase和SolrCloud数据
Spark1.6.2读取SolrCloud 5.5.1 //httpmime-4.4.1.jar // solr-solrj-5.5.1.jar //spark-solr-2.2.2-20161007 ...
- 关于mapreducer 读取hbase数据 存入mysql的实现过程
mapreducer编程模型是一种八股文的代码逻辑,就以用户行为分析求流存率的作为例子 1.map端来说:必须继承hadoop规定好的mapper类:在读取hbase数据时,已经有现成的接口 Tabl ...
- 使用MapReduce读取HBase数据存储到MySQL
Mapper读取HBase数据 package MapReduce; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hba ...
- SparkSQL读取HBase数据
这里的SparkSQL是指整合了Hive的spark-sql cli(关于SparkSQL和Hive的整合,见文章后面的参考阅读). 本质上就是通过Hive访问HBase表,具体就是通过hive-hb ...
- Spark 读取HBase数据
Spark1.6.2 读取 HBase 1.2.3 //hbase-common-1.2.3.jar //hbase-protocol-1.2.3.jar //hbase-server-1.2.3.j ...
- Spark读取Hbase中的数据
大家可能都知道很熟悉Spark的两种常见的数据读取方式(存放到RDD中):(1).调用parallelize函数直接从集合中获取数据,并存入RDD中:Java版本如下: JavaRDD<Inte ...
随机推荐
- Java多线程核心知识(跳槽面试必备)
多线程相对于其他 Java 知识点来讲,有一定的学习门槛,并且了解起来比较费劲.在平时工作中如若使用不当会出现数据错乱.执行效率低(还不如单线程去运行)或者死锁程序挂掉等等问题,所以掌握了解多线程至关 ...
- JVM 监控工具——jstack
[参考文章]:jstack 命令使用经验总结 1. 简介 jstack主要用于生成java虚拟机当前时刻的线程快照. 线程快照是当前java虚拟机内每一条线程正在执行的方法堆栈的集合, 主要目的是定位 ...
- LeetCode 23. 合并K个排序链表(Merge k Sorted Lists)
题目描述 合并 k 个排序链表,返回合并后的排序链表.请分析和描述算法的复杂度. 示例: 输入: [ 1->4->5, 1->3->4, 2->6 ] 输出: ...
- apt 软件安装问题
1.sudo apt-get update由于没有公钥,无法验证下列签名: NO_PUBKEY F42ED6FBAB17C654 解决方法:安装公钥 sudo apt-key adv --keyser ...
- [常用的Cmd运行命令]
打开命令提示符,有很多与系统有关的命令都可以在命令提示符中完成,比如输入ipconfig查看电脑的IP osk 打开屏幕键盘 calc 打开计算器的功能 notepad 打开记事本 mspaint ...
- EXCEL中自定义格式输入的数据怎么完整复制
在用设置单元格式里 自定义 输入数值 如图,B列的数据,我复制后,用选择性粘贴到别的地方,还是无法将75FG4Y2一起复制过去,只能复制过去FG 怎么办? ===>先把这些复制到一个记事本里,再 ...
- [ubuntu]android SDK 与Gradle环境的安装与配置|搭建android基础开发/构建环境
系统环境: linux:ubuntu18 已配置jdk 环境变量 切换到root账户 sudo su 安装Android-sdk (0)准备工作 切换到/usr/local目录: /usr/local ...
- flutter textfield设置高度后内容区无法居中?
textfiled 设置高度后,内容永远无法居中,最后找到原因 decoration: 中有一个 contentPadding属性,设置这个属性对应的Padding值即可
- redis(3)主从与集群
一.主从架构 yum安装的redis cd /etc/ cp redis.conf /etc/slave.conf vim slave.conf bind 192.168.42.7 slaveof p ...
- ubuntu源与常用python配置pip源(win)、pip常用命令
pip常用命令 ubuntu更新系统源 首先备份/etc/apt/sources.list mv /etc/apt/sources.list /etc/apt/sources.list.bak 然后下 ...