JAVA hbase groupby_hbase手动compact与split

本文介绍了一个使用Shell脚本和Python脚本结合的方法来实现HBase表的大区域(split)操作,并通过扫描验证分区效果。该脚本可以根据指定大小进行分区,并检查分区后的文件大小是否符合预期。

#!/bin/bash

die () {

echo >&2 "$@"

echo "usage:"

echo "       $0 check|split table_name [split_size]"

exit 1

}

[[ "$#" -lt 2 ]] && die "at least 2 arguments required, $# provided"

COMMAND=$1

TABLE=$2

SIZE="${3:-1073741824}"

split() {

region_key=`python /home/hduser/hbase/hbase-scan.py -t hbase:meta -f "RowFilter (=, 'substring:$1')"`

echo "split '$region_key'" | hbase shell

}

if [ "$COMMAND" != "check" ] ; then

for region in `hadoop fs -ls /hbase/data/default/$TABLE | awk {'print $8'}`

do

[[ ${region##*/} =~ ^\. ]] && continue

[[ `hadoop fs -du -s $region | awk {'print $1'}` -gt $SIZE ]] && split ${region##*/}

done

# check after split

sleep 60

fi

for region in `hadoop fs -ls /hbase/data/default/$TABLE | awk {'print $8'}`

do

[[ ${region##*/} =~ ^\. ]] && continue

[[ `hadoop fs -du -s $region | awk {'print $1'}` -gt $SIZE ]] && echo "${region##*/} (`hadoop fs -du -s -h $region | awk {'print $1 $2'}`) is a huge region" || echo "${region##*/} (`hadoop fs -du -s -h $region | awk {'print $1 $2'}`) is a small region"

done

hbase-scan.pyimport subprocess

import datetime

import argparse

import csv

import gzip

import happybase

import logging

def connect_to_hbase():

return happybase.Connection('itr-hbasetest01')

def main():

logging.basicConfig(format='%(asctime)s %(name)s %(levelname)s: %(message)s',level=logging.INFO)

argp = argparse.ArgumentParser(description='EventLog Reader')

argp.add_argument('-t','--table', dest='table', default='eventlog')

argp.add_argument('-p','--prefix', dest='prefix')

argp.add_argument('-f','--filter', dest='filter')

argp.add_argument('-l','--limit', dest='limit', default=10)

args = argp.parse_args()

hbase_conn = connect_to_hbase()

table = hbase_conn.table(args.table)

logging.info("scan start")

scanner = table.scan(row_prefix=args.prefix, batch_size=1000, limit=int(args.limit), filter=args.filter)

logging.info("scan done")

i = 0

for key, data in scanner:

logging.info(key)

print key

i+=1

logging.info('%s rows read in total', i)

if __name__ == '__main__':

main()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值