hbase-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From apurt...@apache.org
Subject svn commit: r1422297 - in /hbase/branches/0.94/src: main/java/org/apache/hadoop/hbase/rest/client/RemoteHTable.java test/java/org/apache/hadoop/hbase/rest/client/TestRemoteTable.java
Date Sat, 15 Dec 2012 17:34:36 GMT
Author: apurtell
Date: Sat Dec 15 17:34:32 2012
New Revision: 1422297

URL: http://svn.apache.org/viewvc?rev=1422297&view=rev
Log:
HBASE-7197. Add multi get to RemoteHTable (Elliott Clark)

Modified:
    hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/rest/client/RemoteHTable.java
    hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/rest/client/TestRemoteTable.java

Modified: hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/rest/client/RemoteHTable.java
URL: http://svn.apache.org/viewvc/hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/rest/client/RemoteHTable.java?rev=1422297&r1=1422296&r2=1422297&view=diff
==============================================================================
--- hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/rest/client/RemoteHTable.java
(original)
+++ hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/rest/client/RemoteHTable.java
Sat Dec 15 17:34:32 2012
@@ -142,6 +142,29 @@ public class RemoteHTable implements HTa
     return sb.toString();
   }
 
+  protected String buildMultiRowSpec(final byte[][] rows, int maxVersions) {
+    StringBuilder sb = new StringBuilder();
+    sb.append('/');
+    sb.append(Bytes.toStringBinary(name));
+    sb.append("/multiget/");
+    if (rows == null || rows.length == 0) {
+      return sb.toString();
+    }
+    sb.append("?");
+    for(int i=0; i<rows.length; i++) {
+      byte[] rk = rows[i];
+      if (i != 0) {
+        sb.append('&');
+      }
+      sb.append("row=");
+      sb.append(Bytes.toStringBinary(rk));
+    }
+    sb.append("&v=");
+    sb.append(maxVersions);
+
+    return sb.toString();
+  }
+
   protected Result[] buildResultFromModel(final CellSetModel model) {
     List<Result> results = new ArrayList<Result>();
     for (RowModel row: model.getRows()) {
@@ -267,30 +290,68 @@ public class RemoteHTable implements HTa
     if (get.getFilter() != null) {
       LOG.warn("filters not supported on gets");
     }
+    Result[] results = getResults(spec);
+    if (results.length > 0) {
+      if (results.length > 1) {
+        LOG.warn("too many results for get (" + results.length + ")");
+      }
+      return results[0];
+    } else {
+      return new Result();
+    }
+  }
+
+  public Result[] get(List<Get> gets) throws IOException {
+    byte[][] rows = new byte[gets.size()][];
+    int maxVersions = 1;
+    int count = 0;
+
+    for (Get g : gets) {
+
+      if (count == 0) {
+        maxVersions = g.getMaxVersions();
+      } else if (g.getMaxVersions() != maxVersions) {
+        LOG.warn("MaxVersions on Gets do not match, using the first in the list ("
+            + maxVersions +")");
+      }
+
+      if (g.getFilter() != null) {
+        LOG.warn("filters not supported on gets");
+      }
+
+      rows[count] = g.getRow();
+      count++;
+    }
+
+    String spec = buildMultiRowSpec(rows, maxVersions);
+
+    return getResults(spec);
+  }
+
+  private Result[] getResults(String spec) throws IOException {
     for (int i = 0; i < maxRetries; i++) {
       Response response = client.get(spec, Constants.MIMETYPE_PROTOBUF);
       int code = response.getCode();
       switch (code) {
-      case 200:
-        CellSetModel model = new CellSetModel();
-        model.getObjectFromMessage(response.getBody());
-        Result[] results = buildResultFromModel(model);
-        if (results.length > 0) {
-          if (results.length > 1) {
-            LOG.warn("too many results for get (" + results.length + ")");
+        case 200:
+          CellSetModel model = new CellSetModel();
+          model.getObjectFromMessage(response.getBody());
+          Result[] results = buildResultFromModel(model);
+          if (results.length > 0) {
+            return results;
           }
-          return results[0];
-        }
-        // fall through
-      case 404:
-        return new Result();
-      case 509:
-        try {
-          Thread.sleep(sleepTime);
-        } catch (InterruptedException e) { }
-        break;
-      default:
-        throw new IOException("get request returned " + code);
+          // fall through
+        case 404:
+          return new Result[0];
+
+        case 509:
+          try {
+            Thread.sleep(sleepTime);
+          } catch (InterruptedException e) {
+          }
+          break;
+        default:
+          throw new IOException("get request returned " + code);
       }
     }
     throw new IOException("get request timed out");
@@ -696,11 +757,6 @@ public class RemoteHTable implements HTa
   }
 
   @Override
-  public Result[] get(List<Get> gets) throws IOException {
-    throw new IOException("get(List<Get>) not supported");
-  }
-
-  @Override
   public <T extends CoprocessorProtocol> T coprocessorProxy(Class<T> protocol,
       byte[] row) {
     throw new

Modified: hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/rest/client/TestRemoteTable.java
URL: http://svn.apache.org/viewvc/hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/rest/client/TestRemoteTable.java?rev=1422297&r1=1422296&r2=1422297&view=diff
==============================================================================
--- hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/rest/client/TestRemoteTable.java
(original)
+++ hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/rest/client/TestRemoteTable.java
Sat Dec 15 17:34:32 2012
@@ -222,6 +222,45 @@ public class TestRemoteTable {
   }
 
   @Test
+  public void testMultiGet() throws Exception {
+    ArrayList<Get> gets = new ArrayList<Get>();
+    gets.add(new Get(ROW_1));
+    gets.add(new Get(ROW_2));
+    Result[] results = remoteTable.get(gets);
+    assertNotNull(results);
+    assertEquals(2, results.length);
+    assertEquals(1, results[0].size());
+    assertEquals(2, results[1].size());
+
+    //Test Versions
+    gets = new ArrayList<Get>();
+    Get g = new Get(ROW_1);
+    g.setMaxVersions(3);
+    gets.add(g);
+    gets.add(new Get(ROW_2));
+    results = remoteTable.get(gets);
+    assertNotNull(results);
+    assertEquals(2, results.length);
+    assertEquals(1, results[0].size());
+    assertEquals(3, results[1].size());
+
+    //404
+    gets = new ArrayList<Get>();
+    gets.add(new Get(Bytes.toBytes("RESALLYREALLYNOTTHERE")));
+    results = remoteTable.get(gets);
+    assertNotNull(results);
+    assertEquals(0, results.length);
+
+    gets = new ArrayList<Get>();
+    gets.add(new Get(Bytes.toBytes("RESALLYREALLYNOTTHERE")));
+    gets.add(new Get(ROW_1));
+    gets.add(new Get(ROW_2));
+    results = remoteTable.get(gets);
+    assertNotNull(results);
+    assertEquals(0, results.length);
+  }
+
+  @Test
   public void testPut() throws IOException {
     Put put = new Put(ROW_3);
     put.add(COLUMN_1, QUALIFIER_1, VALUE_1);



Mime
View raw message