2015-09-18 71 views
1

我正在寫基於Java的Hbase客戶端的一些非常基本的東西,用於在啓用的現有表上執行掃描操作。該方案是基於: https://hbase.apache.org/apidocs/org/apache/hadoop/hbase/client/package-summary.html來自Java的HBase掃描API

import java.io.IOException; 
import org.apache.hadoop.conf.Configuration; 
import org.apache.hadoop.hbase.HBaseConfiguration; 
import org.apache.hadoop.hbase.TableName; 
import org.apache.hadoop.hbase.client.Connection; 
import org.apache.hadoop.hbase.client.ConnectionFactory; 
import org.apache.hadoop.hbase.client.Get; 
import org.apache.hadoop.hbase.client.Table; 
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; 
import org.apache.hadoop.hbase.protobuf.generated.*; 


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

    Configuration config = HBaseConfiguration.create(); 

    Connection connection = ConnectionFactory.createConnection(config); 
    try { 


     Table table = connection.getTable(TableName.valueOf("test")); 
     try { 

     Scan s = new Scan(); 
     ResultScanner scanner = table.getScanner(s); 
     try { 

      for (Result rr = scanner.next(); rr != null; rr = scanner.next()) { 
      // print out the row we found and the columns we were looking for 
      System.out.println("Found row: " + rr); 
      } 

     } finally { 

      scanner.close(); 
     } 

     } finally { 
     if (table != null) table.close(); 
     } 
    } finally { 
     connection.close(); 
    } 
    } 
} 

編譯和執行被罰款......會話建立得到。 但我沒有從掃描操作得到任何結果,爲什麼? Eclipse控制檯輸出:

15/09/17 19:37:18 INFO zookeeper.ZooKeeper: Client environment:user.dir=/root/workspace_hbase/HBaseIntro 
15/09/17 19:37:18 INFO zookeeper.ZooKeeper: Initiating client connection, connectString=localhost:2181 sessionTimeout=90000 watcher=hconnection-0xea4a92b0x0, quorum=localhost:2181, baseZNode=/hbase 
15/09/17 19:37:18 INFO zookeeper.ClientCnxn: Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error) 
15/09/17 19:37:18 INFO zookeeper.ClientCnxn: Socket connection established to localhost/127.0.0.1:2181, initiating session 
15/09/17 19:37:18 INFO zookeeper.ClientCnxn: Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x14fde0f7576000e, negotiated timeout = 40000 

我在做什麼錯了? 我在Ubuntu Linux上使用Hbase-1.1.2版本並運行JDK1.8.x.

+0

我嘗試過使用Apache Phoenix來做這件事......從松鼠而不是從我的Java代碼中工作得很好。 – Ashu

+0

尚未在Hadoop中支持/認證JDK1.8。建議使用JDK7。 –

回答

2

Pheonix是完全不同的數據檢索方法... 我希望在那個時候測試,測試數據是可用的! 下面的代碼應該可以工作。

for (Result result = scanner.next(); (result != null); result = scanner.next()) { 
    for(KeyValue keyValue : result.list()) { 
     System.out.println("Qualifier : " + keyValue.getKeyString() + " : Value : " + Bytes.toString(keyValue.getValue())); 
    } 
} 
0

我使用了Apache Phoenix API,最終能夠超越連接到HBase並從Java Client執行HBase的所有CRUD操作。

import java.sql.*; 
import java.util.*; 

public class phoenixTest 
{ 
    public static void main(String args[]) throws Exception 
    { 
     Connection conn; 
     Properties prop = new Properties(); 
     Class.forName("org.apache.phoenix.jdbc.PhoenixDriver"); 
     System.out.println("Driver class loaded successfully"); 

     conn = DriverManager.getConnection("jdbc:phoenix:localhost"); 
     System.out.println("got connection"); 

     //WEB_STAT 
     ResultSet rst = conn.createStatement().executeQuery("select * from WEB_STAT"); 
     while (rst.next()) 
     { 
      System.out.println(rst.getString(1) + " " + rst.getString(2)); 
     } 

    } 
} 

並遵循下列步驟操作: 複製所有服務器JARS到HBase的LIB

然後從: 尼克斯-4.5.2-HBase的-0.98斌/ bin中 執行

./ psql.py localhost /home/cloudera/phoenix-4.5.2-HBase-0.98-src/examples/WEB_STAT.sql /home/cloudera/phoenix-4.5.2-HBase-0.98-src/examples/WEB_STAT.csv/home /cloudera/phoenix-4.5.2-HBase-0.98-src/examples/WEB_STAT_QUERIES.sql

然後在Hbase shell上... scan'WEB_STAT'

驗證表是否被創建?

也做Apache的鳳凰殼檢查

則只是把/home/cloudera/phoenix-4.5.2-HBase-0.98-bin/phoenix-4.5.2-HBase-0.98-client.jar

到Eclipse項目,並利用這個源代碼: 和它的工作都很好阿帕奇鳳凰非常容易上手並獲得與Apahce的HBase的工作。