2

I have created a java application to read data from HBase. I checked link1 link2 link3 and link4. Program returns null even there is data in my table.

hbase shell:

 hbase(main):009:0> get 'login','1'
 COLUMN                CELL                                                      
 password: password   timestamp=1456588594424, value=hpassword                     
 username: username   timestamp=1456588582413, value=husername                     
 2 row(s) in 0.0120 seconds

Code:

Configuration config = HBaseConfiguration.create();
HTable table = new HTable(config, "login");
Get g = new Get(Bytes.toBytes("row1"));
Result r = table.get(g);

byte [] value = r.getValue(Bytes.toBytes("username"),Bytes.toBytes("username"));
byte [] value1 = r.getValue(Bytes.toBytes("password"),Bytes.toBytes("password"));

String valueStr = Bytes.toString(value);
String valueStr1 = Bytes.toString(value1);
System.out.println("username: "+ valueStr+"\npassword: "+valueStr1);
Scan s = new Scan();
s.addColumn(Bytes.toBytes("username"), Bytes.toBytes("username"));
s.addColumn(Bytes.toBytes("password"), Bytes.toBytes("password"));
ResultScanner scanner = table.getScanner(s);

try
{
 for (Result rnext = scanner.next(); rnext != null; rnext = scanner.next())
  {
    System.out.println("Found row : " + rnext);
   }
 }finally{
  scanner.close();
 }

Output:

username: null
password: null

Am I missing something, or do I need to edit somewhere in the code?

Also, I need to implement this into Java Play Framework. Do you have any idea about it?

Thanks in advance.

Alican Balik
  • 1,284
  • 1
  • 8
  • 22

2 Answers2

2

Try this:

 // Instantiating Configuration class
  Configuration config = HBaseConfiguration.create();

  // Instantiating HTable class
  HTable table = new HTable(config, "tablename");

  // Instantiating the Scan class
  Scan scan = new Scan();

  // Scanning the required columns
  scan.addColumn(Bytes.toBytes("columnfamily"), Bytes.toBytes("column1"));
  scan.addColumn(Bytes.toBytes("columnfamily"), Bytes.toBytes("column2"));

  // Getting the scan result
  ResultScanner scanner = table.getScanner(scan);

  // Reading values from scan result
  for (Result result = scanner.next(); result != null; result = scanner.next())

  System.out.println("Found row : " + result);
  //closing the scanner
  scanner.close();
  • Column name and column family had the same name and I changed their names. It is working perfectly now. Thank you for cleaner code. – Alican Balik Mar 01 '16 at 13:59
2

as @AdamSkywalker said that I needed to change names of column name and column family. I was getting null return when their names were same. I renamed them and code works perfectly.

Alican Balik
  • 1,284
  • 1
  • 8
  • 22