2013-07-03 14 views
0

我目前正在創建一個可以從Hadoop HBase後端讀取Titan頂點的Java代碼。我知道blueprint api在每個TransactionalGraph上都提供了getVertices()方法,但我仍然試圖實現我自己的方法。現在,對於通常的頂點讀取,我已經有了一個工作代碼,可以讀取整個HBase後端並從Titan Graph中獲取所有頂點,但是我在實現分頁時遇到問題。從HBase中讀取Titan頂點的分頁

到目前爲止我的代碼:

Scan scan = new Scan(); 
    Filter pageFilter = new ColumnPaginationFilter(DEFAULT_PAGE_SIZE, currentOffSet); 
    scan.setFilter(pageFilter); 
    scan.addFamily(Backend.EDGESTORE_NAME.getBytes()); 
    scan.setMaxVersions(10); 
    List<Vertex> vertexList = new ArrayList<>(DEFAULT_PAGE_SIZE); 
    HTablePool pool = new HTablePool(config, DEFAULT_PAGE_SIZE); 
    ResultScanner scanner = pool.getTable(attributeMap.get("storage.tablename")).getScanner(scan); 

但ResultScanner返回整個圖形。

currentOffSet是一個確定當前頁碼的int變量。

我也試過ResultScanner#next(int rowCount)。它工作正常。但在這個過程中,我沒有選擇返回到上一頁。

任何人都可以幫助我嗎?

預先感謝您。

回答

0

我已經解決了。邏輯非常簡單。您必須在掃描程序實例上使用setStartRow方法。這是第一次沒有必要,因爲掃描應該從第一行開始。然後我們需要獲取*(PAGE_SIZE + 1)*行數。來自ResultScanner的最後一行將用作下一頁的起始行。

要返回到上一頁,我們需要使用一個緩衝區或堆棧,將存儲起始行的所有先前訪問過的頁面。

這是我的代碼片段:

Scan scan = (new Scan()).addFamily(Backend.EDGESTORE_NAME.getBytes()); 
    Filter filter = new PageFilter(DEFAULT_PAGE_SIZE + 1); 
    scan.setFilter(filter); 
    if (currentPageStartRowForHBase != null) { 
     scan.setStartRow(currentPageStartRowForHBase); 
    } 
    List<Vertex> vertexList = new ArrayList<>(DEFAULT_PAGE_SIZE + 1); 
    HTablePool pool = null; 
    ResultScanner scanner = null; 
    try { 
     if (pool == null) { 
      pool = new HTablePool(config, DEFAULT_PAGE_SIZE + 1); 

     } 
     scanner = pool.getTable(attributeMap.get("storage.tablename")).getScanner(scan); 
     for (Result result : scanner) { 
      ByteBuffer byteBuffer = ByteBuffer.wrap(result.getRow()); 
      Vertex vertex = this.getVertex(IDHandler.getKeyID(byteBuffer)); 
      if (vertexList.size() < DEFAULT_PAGE_SIZE) 
       vertexList.add(vertex); 
      else { 
       nextPageStartRowForHBase = byteBuffer.array(); 
      } 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

nextPageStartRowForHBase & currentPageStartRowForHBase的byte []

這滿足了我的要求。但如果有人有更好的解決方案,請與我們分享。

+0

我剛開始評估泰坦,我的理解是曾經無法直接從Hbase讀取/寫入泰坦圖形數據。看起來你說這是可能的。你能指點我的例子,可以告訴我如何做到這一點? – chapstick

+0

上面的代碼是你問的最簡單的例子。列表 vertexList是我用來保存頂點的集合。 'IDHandler.getKeyID(byteBuffer)' 這行實際上是從HBase後端返回頂點ID。現在,一旦我得到頂點ID,獲取頂點實例並不複雜。 我需要的是從HBase Backend和Cassandra讀取數據的簡單方法,因爲Titan不支持全局查詢。如果您想要更好的方式從HBase或Cassandra讀取數據,您可以通過探索後端功能來完成。 – Pradatta