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
This commit is contained in:
Andrew Kyle Purtell 2012-12-14 23:23:05 +00:00
parent 778ab055a0
commit c9a360e360
2 changed files with 116 additions and 24 deletions

View File

@ -148,6 +148,29 @@ public class RemoteHTable implements HTableInterface {
return sb.toString(); 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) { protected Result[] buildResultFromModel(final CellSetModel model) {
List<Result> results = new ArrayList<Result>(); List<Result> results = new ArrayList<Result>();
for (RowModel row: model.getRows()) { for (RowModel row: model.getRows()) {
@ -273,31 +296,66 @@ public class RemoteHTable implements HTableInterface {
if (get.getFilter() != null) { if (get.getFilter() != null) {
LOG.warn("filters not supported on gets"); 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++) { for (int i = 0; i < maxRetries; i++) {
Response response = client.get(spec, Constants.MIMETYPE_PROTOBUF); Response response = client.get(spec, Constants.MIMETYPE_PROTOBUF);
int code = response.getCode(); int code = response.getCode();
switch (code) { switch (code) {
case 200: case 200:
CellSetModel model = new CellSetModel(); CellSetModel model = new CellSetModel();
model.getObjectFromMessage(response.getBody()); model.getObjectFromMessage(response.getBody());
Result[] results = buildResultFromModel(model); Result[] results = buildResultFromModel(model);
if (results.length > 0) { if ( results.length > 0) {
if (results.length > 1) { return results;
LOG.warn("too many results for get (" + results.length + ")");
} }
return results[0]; // fall through
} case 404:
// fall through return new Result[0];
case 404:
return new Result();
case 509: case 509:
try { try {
Thread.sleep(sleepTime); Thread.sleep(sleepTime);
} catch (InterruptedException e) { } } catch (InterruptedException e) { }
break; break;
default: default:
throw new IOException("get request returned " + code); throw new IOException("get request returned " + code);
} }
} }
throw new IOException("get request timed out"); throw new IOException("get request timed out");
@ -708,11 +766,6 @@ public class RemoteHTable implements HTableInterface {
throw new IOException("batchCallback not supported"); throw new IOException("batchCallback not supported");
} }
@Override
public Result[] get(List<Get> gets) throws IOException {
throw new IOException("get(List<Get>) not supported");
}
@Override @Override
public <T extends CoprocessorProtocol> T coprocessorProxy(Class<T> protocol, public <T extends CoprocessorProtocol> T coprocessorProxy(Class<T> protocol,
byte[] row) { byte[] row) {

View File

@ -216,6 +216,45 @@ public class TestRemoteTable {
assertEquals(2, count); assertEquals(2, count);
} }
@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 @Test
public void testPut() throws IOException { public void testPut() throws IOException {
Put put = new Put(ROW_3); Put put = new Put(ROW_3);