在hbase中查找具有空值的行数

ht4b089n  于 2021-05-29  发布在  Hadoop
关注(0)|答案(1)|浏览(451)

我已经用rowid和与tweet相关的vrious信息填充了一个hbase表,如干净的文本、url、hashtag等,如下所示

902221655086211073    column=clean-tweet:clean-text-cta, timestamp=1514793745304, value=democrat mayor order hurricane harvey stand houston

然而,在填充时,我注意到有些行是空的,比如

902487280543305728    column=clean-tweet:clean-text-cta, timestamp=1514622371008, value=

现在我如何找到有数据的行数?
请帮帮我

hts6caw3

hts6caw31#

到目前为止,hbase shell中还没有这样做的规定。可能您可以使用这样一个简单的代码来获取一个没有值的记录数作为所提供的列限定符。
countandfilter[表名][列族][列限定符]

import java.io.IOException;

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.Result;
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 CountAndFilter {

private static Connection conn;
private static int recordsWithoutValue = 0;
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], args[2]);
    System.out.println("Records with empty value : " + recordsWithoutValue);
}

public static void scan(String tableName, String columnFamily, String columnQualifier) throws IOException {
    Table table = conn.getTable(TableName.valueOf(tableName));
    ResultScanner rs = table.getScanner(new Scan().addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(columnQualifier)));

    Result res = null;
    try {
        while ((res = rs.next()) != null) {
            if (res.containsEmptyColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(columnQualifier))){
                recordsWithoutValue++;
            }
        }
    } finally {
        rs.close();
    }
}
}

相关问题