统计HBase表中列族中的记录数

统计HBase表中列族中的记录数,hbase,jruby,bigdata,database,nosql,Hbase,Jruby,Bigdata,Database,Nosql,我正在寻找一个HBase shell命令,该命令将统计指定列族中的记录数。 我知道我能跑: echo "scan 'table_name'" | hbase shell | grep column_family_name | wc -l 但是,这将比标准计数命令运行得慢得多: count 'table_name' , CACHE => 50000 (because the use of the CACHE=>50000) 更糟糕的是,它不会返回实际记录数,而是返回指定列族

我正在寻找一个HBase shell命令,该命令将统计指定列族中的记录数。 我知道我能跑:

echo "scan 'table_name'" | hbase shell | grep column_family_name | wc -l  
但是,这将比标准计数命令运行得慢得多:

count 'table_name' , CACHE => 50000 (because the use of the CACHE=>50000)  
更糟糕的是,它不会返回实际记录数,而是返回指定列族中的单元格总数(如果我没弄错的话)。 我需要这样的东西:

count 'table_name' , CACHE => 50000 , {COLUMNS => 'column_family_name'}
提前感谢,

Michael

这是我在需要时编写的Ruby代码,就像您需要的一样。提供了适当的意见。它为您提供了
HBase
shell
count\u table
命令。第一个参数是表名,第二个参数是属性数组,与
scan
shell命令相同

对你的问题的直接回答是

count_table 'your.table', { COLUMNS => 'your.family' }
我还建议添加缓存,如用于扫描:

count_table 'your.table', { COLUMNS => 'your.family', CACHE => 10000 }
以下是来源:

# Argiments are the same as for scan command.
# Examples:
#
# count_table 'test.table', { COLUMNS => 'f:c1' }
# --- Counts f:c1 columsn in 'test_table'.
#
# count_table 'other.table', { COLUMNS => 'f' }
# --- Counts 'f' family rows in 'other.table'.
#
# count_table 'test.table', { CACHE => 1000 }
# --- Count rows with caching.
#
def count_table(tablename, args = {})

    table = @shell.hbase_table(tablename)

    # Run the scanner
    scanner = table._get_scanner(args)

    count = 0
    iter = scanner.iterator

    # Iterate results
    while iter.hasNext
        row = iter.next
        count += 1
    end

    # Return the counter
    return count
end

您知道如何在HBase中添加此功能吗?它只是客户端脚本,而不是您在HBase服务器中需要的东西。您不应该添加这些内容,因为它的所有组件都已在HBase中。可以将上面的
count\u表
之类的实用程序功能添加到
~/.irbrc
文件中,以防用户需要跨多个HBase外壳会话。
.irbrc
文件是IRB使用的同一个rc文件,即Intercive Ruby Shell,有一些细微的区别-hbase Shell实际上是IRB的hbase风格,它在JRuby上下文中运行,允许使用Java库。