2014-03-27 179 views
1

我可以使用java api以獨立模式(不使用Hadoop)連接到Hbase嗎?無法使用java api連接到Hbase

這是我的代碼,我想知道如何使它工作。我應該將某些屬性設置爲變量'config'嗎?

我有這些本地安裝:HBase的-0.98.0的Hadoop 2.2.0

import java.io.IOException; 

import org.apache.hadoop.conf.Configuration; 
import org.apache.hadoop.fs.Path; 
import org.apache.hadoop.hbase.HBaseConfiguration; 
import org.apache.hadoop.hbase.client.Get; 
import org.apache.hadoop.hbase.client.HTable; 
import org.apache.hadoop.hbase.client.Put; 
import org.apache.hadoop.hbase.client.Result; 
import org.apache.hadoop.hbase.client.ResultScanner; 
import org.apache.hadoop.hbase.client.Scan; 
import org.apache.hadoop.hbase.util.Bytes; 


public class MyLittleHBaseClient { 
    public static void main(String[] args) throws IOException { 

    // maybe I should do some configuration here, but I don't know how 
    Configuration config = HBaseConfiguration.create(); 

    HTable table = new HTable(config, "myLittleHBaseTable"); 

    Put p = new Put(Bytes.toBytes("myLittleRow")); 

    p.add(Bytes.toBytes("myLittleFamily"), Bytes.toBytes("someQualifier"), 
     Bytes.toBytes("Some Value")); 

    table.put(p); 

    Get g = new Get(Bytes.toBytes("myLittleRow")); 
    Result r = table.get(g); 
    byte [] value = r.getValue(Bytes.toBytes("myLittleFamily"), 
     Bytes.toBytes("someQualifier")); 

    String valueStr = Bytes.toString(value); 
    System.out.println("GET: " + valueStr); 

    Scan s = new Scan(); 
    s.addColumn(Bytes.toBytes("myLittleFamily"), Bytes.toBytes("someQualifier")); 
    ResultScanner scanner = table.getScanner(s); 
    try { 

     for (Result rr = scanner.next(); rr != null; rr = scanner.next()) { 

     System.out.println("Found row: " + rr); 
     } 

    } finally { 

     scanner.close(); 
    } 
    } 
} 
+0

你可以試試這個屬性設置爲HbaseConfiguration對象'hbase.zookeeper.quorum' – donut

+0

你試過運行呢?該程序對我來說是正確的。 –

回答

0

如果您的HBase-site.xml中以獨立模式是空的(),你沒有設置任何東西。如果已經覆蓋了hbase-site.xml中的任何內容,最好添加hbase-site.xml而不是單獨設置參數。

Configuration config = HBaseConfiguration.create(); 
config.addResource("<HBASE_CONF_DIR_PATH>/hbase-site.xml"); 
+0

感謝您的回答,這對我來說真的很有幫助,而我又遇到了另一個問題,請您看看它嗎? http://stackoverflow.com/questions/22710112/get-error-when-i-use-java-api-to-connect-hbasestandalone-mode-without-hadoop –