简体   繁体   中英

HBase scan values without their id

I am trying to create a list with HBase on Java. I can get values all together, but I am confused how to assign them to variables.( String bookName = ... , String bookAuthor = ... )

I need to get all values which are in contribute table, and assign them to variable.

In contribute table, there are id , author , name .

HTable hTable = new HTable(hConn.config, "contribute");
            Scan scan = new Scan();
            scan.addColumn(Bytes.toBytes("book"), Bytes.toBytes("author"));
            scan.addColumn(Bytes.toBytes("book"), Bytes.toBytes("name"));
            ResultScanner scanner = hTable.getScanner(scan);
            for (Result result = scanner.next(); result != null; result = scanner.next())
            {
                for(KeyValue keyValue : result.list()) {
                    System.out.println("Qualifier : " + Bytes.toString(keyValue.getKey()) + " : Value : " + Bytes.toString(keyValue.getValue()));
                }
            }

Qualifier : $dba190f6-ff45-4d5b-bf2f-d2ea75bb528fbookauthorT : Value : Frans Hoffman

Qualifier : $dba190f6-ff45-4d5b-bf2f-d2ea75bb528fbooknameT : Value : h32

When I check them, keyValue.getKey() and keyValue.getValue() show every value in table. Is it possible to get specific value/qualify? For example, I just need to get values of name .

My understanding : You want to get only one column name

        HTable hTable = new HTable(hConn.config, "contribute");
        Scan scan = new Scan();
        scan.addColumn(Bytes.toBytes("book"), Bytes.toBytes("author"));
        scan.addColumn(Bytes.toBytes("book"), Bytes.toBytes("name"));
        ResultScanner scanner = hTable.getScanner(scan);

for (Result rr = scan.next() ; rr != null; rr = scan.next())  { 
NavigableMap familyMap = rr.getFamilyMap(Bytes.toBytes("book")); 
byte[] name = (byte[]) familyMap.get(Bytes.toBytes("name")); 
System.out.println(Bytes.toString(name)); // This you can assign it to variable
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM