美文网首页
HBase Export命令 Binary String格式的r

HBase Export命令 Binary String格式的r

作者: Tankilo | 来源:发表于2020-03-03 21:30 被阅读0次

    今天遇到一个问题,需要把现场HBase的数据导回本地,用本地环境的界面去解析数据。也为了保存好故障数据,现场HBase的数据过段时间会清理。但是这个表里有些行的数据列非常非常多,所以需要精确控制只导出某一行数据。

    先在HBase文档里搜索到了官方自带的命令 Export

    Export is a utility that will dump the contents of table to HDFS in a sequence file. The Export can be run via a Coprocessor Endpoint or MapReduce. Invoke via:

    mapreduce-based Export

    $ bin/hbase org.apache.hadoop.hbase.mapreduce.Export <tablename> <outputdir> [<versions> [<starttime> [<endtime>]]]

    在HBase命令行里提示的帮助信息如下:

    Usage: Export [-D <property=value>]* <tablename> <outputdir> [<versions> [<starttime> [<endtime>]] [^[regex pattern] or [Prefix] to filter]]

    Note: -D properties will be applied to the conf used.
    For example:
    -D mapreduce.output.fileoutputformat.compress=true
    -D mapreduce.output.fileoutputformat.compress.codec=org.apache.hadoop.io.compress.GzipCodec
    -D mapreduce.output.fileoutputformat.compress.type=BLOCK
    Additionally, the following SCAN properties can be specified to control/limit what is exported..
    -D hbase.mapreduce.scan.column.family=<familyName>
    -D hbase.mapreduce.include.deleted.rows=true
    -D hbase.mapreduce.scan.row.start=<ROWSTART>
    -D hbase.mapreduce.scan.row.stop=<ROWSTOP>
    For performance consider the following properties:
    -Dhbase.client.scanner.caching=100
    -Dmapreduce.map.speculative=false
    -Dmapreduce.reduce.speculative=false
    For tables with very wide rows consider setting the batch size as below:
    -Dhbase.export.scanner.batch=10

    这个里面可以用hbase.mapreduce.scan.row.start和hbase.mapreduce.scan.row.stop来指定rowkey的起点和终点,如果设置成一样,就是查询一条了。

    首先我先确定rowkey,从其他的异常信息里获取到了生成这条rowkey的各个部分,然后在代码里转换成bytes数组,并且通过Bytes#toStringBinary(byte[], int, int)生成 Binary String。这里的rowkey不是简单类型的,是多个部分拼接起来的。HBase在shell里显示成其自定义的Binary String格式,

    确认一下STARTROW和ENDROW可以查询到数据。PS:参数我做了一些简化替换,不包含公司敏感信息。

    scan 'TraceV2', {STARTROW => "\x00\x02\xC1", ENDROW => "\x00\x02\xC1"}
    

    然后开始导出了

    ./hbase org.apache.hadoop.hbase.mapreduce.Export -D hbase.mapreduce.scan.column.family='T' -D  hbase.mapreduce.scan.row.start="\x00\x02\xC1" -D hbase.mapreduce.scan.row.stop="\x00\x02\xC1" TargetTable ./export_data
    

    这里发现问题命令输出的MapReduce Job日志里,Map input recordsMap output records均为0。这说明没有查询到数据,但是scan明明就可以查到那条数据。

    经过一系列的搜索后,我发现这个世界就我不行,或者网上成功的案例都没有Binary String格式的String。

    HBase是Java写的,直接去看代码,通过分析,发现了​原因。

    HBase github 主分支 ExportUtils.java

    主分支

    HBase github 1.2.6 Export.java

    1.2.6 tag

    很明显发现问题所在,1.2.6版本没用Bytes#toBytesBinary去解析字符串,而是用的Bytes#toBytes(java.lang.String),这个会用UTF-8编码去解析字符串,所以导致无法查询到数据。

    本地修改完后打包hbase-server-1.2.6.jar到本地环境替换后,同样的命令导出成功。

    但是新的问题来了,现场是不允许直接修改容器的。

    我之前一般执行hbase shell都是直接到HBase所在主机里的安装目录执行的,我突然记起来HBase Shell可以远程连接到别的HBase主机。但是我并不清楚,这样导出时候用的jar包是本地的,还是远程HBase主机的。我可能不了解里面的通信环节。所以我自己手工测试了下。

    我在一台主机放一个HBase 1.2.6安装目录,里面没有修改过代码,修改hbase-site.xml后,在hbase shell执行导出命令。导出失败,没有查询到数据。我更新修改后的hbase-server-1.2.6.jar到lib目录后,数据导出成功。

    这说明HBase Shell远程连接的时候使用本地jar包。

    于是我让现场在和HBase集群网络相通的主机放置HBase安装目录,正确设置hbase-site.xml,执行上面的导出命令,数据导出成功。

    相关文章

      网友评论

          本文标题:HBase Export命令 Binary String格式的r

          本文链接:https://www.haomeiwen.com/subject/ngpllhtx.html