java - 在 HBase 中格式化输出

标签 java hbase

我想在应用单列值过滤器后从 HBase 获取所选列族的行。

在我的表“Projectdata”中,我有像这样的列族

    name|story|type|keyword

  aritra| kl  |ac  |happy
   nill |jk   |bc  |sad
   bob  |pk   |dd  |happy

。当他们的“关键字”快乐时,我想获取“名称”列表。 这是我的代码。

public class ByCategory {

    public static void main(String [] args) throws Exception{

        Configuration conf = HBaseConfiguration.create();
        HTable table = new HTable(conf, "Projectdata");

        SingleColumnValueFilter filter_by_happycategory = new SingleColumnValueFilter(
                Bytes.toBytes("keyword" ),
                Bytes.toBytes(""),
                CompareOp.EQUAL,
                Bytes.toBytes("happy")
                );
        FilterList filterListk =new FilterList();
        filterListk.addFilter(filter_by_happycategory);

        Scan scanh = new Scan();
        scanh.addFamily(Bytes.toBytes("name"));
        scanh.addFamily(Bytes.toBytes("keyword"));
        scanh.setFilter(filterListk);


        ResultScanner scannerh = table.getScanner(scanh);
        String key = new String("~");
        String keyFlag = new String("~");
        System.out.println("Scanning table... ");

            for(Result resulth: scannerh){
                //System.out.println("getRow:"+Bytes.toString(resulth.getRow()));
                 key = "~";
                for(KeyValue kv:resulth.raw())
                {
                    if(key.compareTo(keyFlag)==0){
                        key=Bytes.toString(kv.getRow());
                        System.out.print("Key: "+key);
                    }
                    System.out.print(Bytes.toString(kv.getValue()));

                }
                System.out.println("");

            }
            scannerh.close();
            System.out.println("complete");  
            table.close();
        }

    }

我得到这样的输出

Key: 102happybob
Key: 109happyaritra

但我只想得到名字。我正在努力获得

 Key: 102bob
 Key: 109aritra

在hbase中可以吗?我的错究竟在哪里?

最佳答案

使用

for(Result resulth: scannerh){
        System.out.println("Key: "+Bytes.toString(resulth.getRow())+Bytes.toString(resulth.getValue(Bytes.toBytes("name"),Bytes.toBytes(""))));
    }

您将获得所需的输出。这里 resulth.getRow() 为您提供 rowkey,而 .getValue(columnfamily,column) 为您提供特定列的值,在您的情况下为 ""

关于java - 在 HBase 中格式化输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46484385/

相关文章:

hadoop - 使用 Ooize 时为 TableMapReduceUtil 重置 Kerberos 登录上下文

python - AWS Hbase : Invalid method name: 'getTableNames'

hadoop - 尝试创建表时出现 Phoenix 错误

java - Bash:迭代更改文件中的一行并将 file.txt 读取到程序

java - 寻找好的Java音频压缩库

java - 从 OpenGL ES 1.0 切换到 2.0

apache - 扩展自定义 Apache Knox 服务以查询多个 HBase 表

hadoop - Hadoop是否在映射和reduce步骤之间使用HBase作为 “auxiliar”?

java - 在Java中选择随机数时如何停止重复

java - NetBeans 不会从远程位置下载,但测试 FTP 使我成功