From c9a360e36059aa46d7090e879762d44d54fa2782 Mon Sep 17 00:00:00 2001 From: Andrew Kyle Purtell Date: Fri, 14 Dec 2012 23:23:05 +0000 Subject: [PATCH] HBASE-7197. Add multi get to RemoteHTable (Elliott Clark) git-svn-id: https://svn.apache.org/repos/asf/hbase/trunk@1422143 13f79535-47bb-0310-9956-ffa450edef68 --- .../hbase/rest/client/RemoteHTable.java | 101 +++++++++++++----- .../hbase/rest/client/TestRemoteTable.java | 39 +++++++ 2 files changed, 116 insertions(+), 24 deletions(-) diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/client/RemoteHTable.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/client/RemoteHTable.java index beebe960b05..92fe09202f6 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/client/RemoteHTable.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/client/RemoteHTable.java @@ -148,6 +148,29 @@ public class RemoteHTable implements HTableInterface { 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 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 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(); + // 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); + 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"); @@ -708,11 +766,6 @@ public class RemoteHTable implements HTableInterface { throw new IOException("batchCallback not supported"); } - @Override - public Result[] get(List gets) throws IOException { - throw new IOException("get(List) not supported"); - } - @Override public T coprocessorProxy(Class protocol, byte[] row) { diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/client/TestRemoteTable.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/client/TestRemoteTable.java index 01e23d99a0e..b52a167fbbc 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/client/TestRemoteTable.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/client/TestRemoteTable.java @@ -216,6 +216,45 @@ public class TestRemoteTable { assertEquals(2, count); } + @Test + public void testMultiGet() throws Exception { + ArrayList gets = new ArrayList(); + 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 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(); + gets.add(new Get(Bytes.toBytes("RESALLYREALLYNOTTHERE"))); + results = remoteTable.get(gets); + assertNotNull(results); + assertEquals(0, results.length); + + gets = new ArrayList(); + 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);