Yonex
Yonex

Reputation: 87

Formatting output in HBase

I want to get the rows of selected column family from HBase after applying Single Column value filter.

In my table 'Projectdata' I have column family like

    name|story|type|keyword

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

. I want to get list of 'name' when their 'keyword' is happy. Here is my code.

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();
        }

    }

I am geting output like this

Key: 102happybob
Key: 109happyaritra

But I want to get only name. I am trying to get

 Key: 102bob
 Key: 109aritra

Is it possible in hbase? Where is my fault actually?

Upvotes: 1

Views: 443

Answers (1)

Abhishek Kumar
Abhishek Kumar

Reputation: 292

Use

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

You will get the desired output.Here resulth.getRow() gives you rowkey while .getValue(columnfamily,column) gives you value of a specific column which is "" in your case.

Upvotes: 1

Related Questions