use of org.apache.hadoop.hbase.rest.model.RowModel in project hbase by apache.
the class RemoteHTable method buildResultFromModel.
protected Result[] buildResultFromModel(final CellSetModel model) {
List<Result> results = new ArrayList<>();
for (RowModel row : model.getRows()) {
List<Cell> kvs = new ArrayList<>(row.getCells().size());
for (CellModel cell : row.getCells()) {
byte[][] split = KeyValue.parseColumn(cell.getColumn());
byte[] column = split[0];
byte[] qualifier = null;
if (split.length == 1) {
qualifier = HConstants.EMPTY_BYTE_ARRAY;
} else if (split.length == 2) {
qualifier = split[1];
} else {
throw new IllegalArgumentException("Invalid familyAndQualifier provided.");
}
kvs.add(new KeyValue(row.getKey(), column, qualifier, cell.getTimestamp(), cell.getValue()));
}
results.add(Result.create(kvs));
}
return results.toArray(new Result[results.size()]);
}
use of org.apache.hadoop.hbase.rest.model.RowModel in project hbase by apache.
the class RemoteHTable method put.
@Override
public void put(List<Put> puts) throws IOException {
// this is a trick: The gateway accepts multiple rows in a cell set and
// ignores the row specification in the URI
// separate puts by row
TreeMap<byte[], List<Cell>> map = new TreeMap<>(Bytes.BYTES_COMPARATOR);
for (Put put : puts) {
byte[] row = put.getRow();
List<Cell> cells = map.get(row);
if (cells == null) {
cells = new ArrayList<>();
map.put(row, cells);
}
for (List<Cell> l : put.getFamilyCellMap().values()) {
cells.addAll(l);
}
}
// build the cell set
CellSetModel model = new CellSetModel();
for (Map.Entry<byte[], List<Cell>> e : map.entrySet()) {
RowModel row = new RowModel(e.getKey());
for (Cell cell : e.getValue()) {
row.addCell(new CellModel(cell));
}
model.addRow(row);
}
// build path for multiput
StringBuilder sb = new StringBuilder();
sb.append('/');
sb.append(Bytes.toString(name));
// can be any nonexistent row
sb.append("/$multiput");
for (int i = 0; i < maxRetries; i++) {
Response response = client.put(sb.toString(), Constants.MIMETYPE_PROTOBUF, model.createProtobufOutput());
int code = response.getCode();
switch(code) {
case 200:
return;
case 509:
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
throw (InterruptedIOException) new InterruptedIOException().initCause(e);
}
break;
default:
throw new IOException("multiput request failed with " + code);
}
}
throw new IOException("multiput request timed out");
}
use of org.apache.hadoop.hbase.rest.model.RowModel in project hbase by apache.
the class RemoteHTable method buildModelFromPut.
protected CellSetModel buildModelFromPut(Put put) {
RowModel row = new RowModel(put.getRow());
long ts = put.getTimeStamp();
for (List<Cell> cells : put.getFamilyCellMap().values()) {
for (Cell cell : cells) {
row.addCell(new CellModel(CellUtil.cloneFamily(cell), CellUtil.cloneQualifier(cell), ts != HConstants.LATEST_TIMESTAMP ? ts : cell.getTimestamp(), CellUtil.cloneValue(cell)));
}
}
CellSetModel model = new CellSetModel();
model.addRow(row);
return model;
}
use of org.apache.hadoop.hbase.rest.model.RowModel in project hbase by apache.
the class TestScannersWithFilters method verifyScanNoEarlyOut.
private static void verifyScanNoEarlyOut(Scan s, long expectedRows, long expectedKeys) throws Exception {
ScannerModel model = ScannerModel.fromScan(s);
// fetch it all at once
model.setBatch(Integer.MAX_VALUE);
StringWriter writer = new StringWriter();
marshaller.marshal(model, writer);
LOG.debug(writer.toString());
byte[] body = Bytes.toBytes(writer.toString());
Response response = client.put("/" + TABLE + "/scanner", Constants.MIMETYPE_XML, body);
assertEquals(response.getCode(), 201);
String scannerURI = response.getLocation();
assertNotNull(scannerURI);
// get a cell set
response = client.get(scannerURI, Constants.MIMETYPE_XML);
assertEquals(response.getCode(), 200);
assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type"));
CellSetModel cellSet = (CellSetModel) unmarshaller.unmarshal(new ByteArrayInputStream(response.getBody()));
// delete the scanner
response = client.delete(scannerURI);
assertEquals(response.getCode(), 200);
Iterator<RowModel> i = cellSet.getRows().iterator();
int j = 0;
for (boolean done = true; done; j++) {
done = i.hasNext();
if (!done)
break;
RowModel rowModel = i.next();
List<CellModel> cells = rowModel.getCells();
if (cells.isEmpty())
break;
assertTrue("Scanned too many rows! Only expected " + expectedRows + " total but already scanned " + (j + 1), expectedRows > j);
assertEquals("Expected " + expectedKeys + " keys per row but " + "returned " + cells.size(), expectedKeys, cells.size());
}
assertEquals("Expected " + expectedRows + " rows but scanned " + j + " rows", expectedRows, j);
}
use of org.apache.hadoop.hbase.rest.model.RowModel in project hbase by apache.
the class TestScannersWithFilters method verifyScan.
private static void verifyScan(Scan s, long expectedRows, long expectedKeys) throws Exception {
ScannerModel model = ScannerModel.fromScan(s);
// fetch it all at once
model.setBatch(Integer.MAX_VALUE);
StringWriter writer = new StringWriter();
marshaller.marshal(model, writer);
LOG.debug(writer.toString());
byte[] body = Bytes.toBytes(writer.toString());
Response response = client.put("/" + TABLE + "/scanner", Constants.MIMETYPE_XML, body);
assertEquals(response.getCode(), 201);
String scannerURI = response.getLocation();
assertNotNull(scannerURI);
// get a cell set
response = client.get(scannerURI, Constants.MIMETYPE_XML);
assertEquals(response.getCode(), 200);
assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type"));
CellSetModel cells = (CellSetModel) unmarshaller.unmarshal(new ByteArrayInputStream(response.getBody()));
int rows = cells.getRows().size();
assertTrue("Scanned too many rows! Only expected " + expectedRows + " total but scanned " + rows, expectedRows == rows);
for (RowModel row : cells.getRows()) {
int count = row.getCells().size();
assertEquals("Expected " + expectedKeys + " keys per row but " + "returned " + count, expectedKeys, count);
}
// delete the scanner
response = client.delete(scannerURI);
assertEquals(response.getCode(), 200);
}
Aggregations