hbase中的java格式输出

but5z9lq  于 2021-06-08  发布在  Hbase
关注(0)|答案(1)|浏览(336)

我想在应用单列值筛选器后从hbase获取所选列族的行。
在我的“projectdata”表中,我有一个类似

name|story|type|keyword

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

. 当他们的“关键字”满意时,我想得到“name”的列表。这是我的密码。

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中是否可能?我的错到底在哪里?

dohp0rv5

dohp0rv51#

使用

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) 提供特定列的值 "" 对你来说。

相关问题