2016-02-28 215 views
1

我創建了一個java應用程序來讀取HBase中的數據。我檢查了link1link2link3link4。即使我的表中有數據,程序也會返回null。HBase讀取數據返回null

HBase的外殼:

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 

代碼:

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

輸出:

username: null 
password: null 

我失去了一些東西,或者我需要在代碼編輯的地方?

此外,我需要將其實現到Java Play Framework中。你有什麼想法嗎?

在此先感謝。

+0

目前還不清楚哪些列族用於存儲數據。你使用「用戶名」列和列家庭,它似乎是錯誤的 – AdamSkywalker

+0

謝謝你的幫助。我改變了它們,並將Bytes.toBytes(「row1」))「row1」更改爲「1」。 –

+0

爲什麼你使用掃描並在同一時間? –

回答

2

爲@AdamSkywalker說,我需要改變的列名和列族的名字。當他們的名字相同時,我得到空回報。 我將它們重命名並且代碼完美無缺。

2

試試這個:

// 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(); 
+0

列名和列族具有相同的名稱,我更改了它們的名稱。它現在正在完美工作。 感謝您提供更乾淨的代碼。 –