通过hbase shell命令显示列族和列限定符

时间:2017-12-11 10:53:21

标签: java hbase mapr

我在HBASE中有数百万行,其中包含可计数的运行时列限定符(即小于10)。很难扫描HBASE表中的整个行。有没有办法通过hbase shell或任何可用于打印的实用程序打印列族中的列限定符?

1 个答案:

答案 0 :(得分:0)

到目前为止,我还没有意识到预定义的实用程序。可能是您可以使用以下代码获取表中列族的列限定符。

用法:HbaseCqIdentifier [tableName] [columnFamily]

import java.io.IOException;
import java.util.Set;

import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

public class HbaseCqIdentifier {
    private static Connection conn;
    private static Set<byte[]> columnQualifiers;

    public static Admin getConnection() throws IOException {
        if (conn == null) {
            conn = ConnectionFactory.createConnection(HBaseConfiguration.create());
        }
        return conn.getAdmin();
    }

    public static void main(String args[]) throws IOException {
        getConnection();
        scan(args[0], args[1]);
        columnQualifiers.forEach((k) -> System.out.println("Column Qualifier : " + Bytes.toString(k)));
    }

    public static void scan(String tableName, String columnFamily) throws IOException {
        System.out.println("Table Name : " + tableName);
        Table table = conn.getTable(TableName.valueOf(tableName));
        Scan scan = new Scan();
        scan.addFamily(Bytes.toBytes(columnFamily));
        ResultScanner rs = table.getScanner(scan);

        while(rs.iterator().hasNext()){
            rs.forEach((k)-> columnQualifiers = k.getFamilyMap(Bytes.toBytes(columnFamily)).keySet());
        }
    }
}

注意:在类路径中添加hadoop和hbase jar以及hbase-site.xml