use of org.apache.hadoop.hbase.rest.model.CellSetModel in project hbase by apache.
the class TestTableScan method readProtobufStream.
/**
* Read protobuf stream.
* @param inputStream the input stream
* @return The number of rows in the cell set model.
* @throws IOException Signals that an I/O exception has occurred.
*/
public int readProtobufStream(InputStream inputStream) throws IOException {
DataInputStream stream = new DataInputStream(inputStream);
CellSetModel model = null;
int rowCount = 0;
try {
while (true) {
byte[] lengthBytes = new byte[2];
int readBytes = stream.read(lengthBytes);
if (readBytes == -1) {
break;
}
assertEquals(2, readBytes);
int length = Bytes.toShort(lengthBytes);
byte[] cellset = new byte[length];
stream.read(cellset);
model = new CellSetModel();
model.getObjectFromMessage(cellset);
checkRowsNotNull(model);
rowCount = rowCount + TestScannerResource.countCellSet(model);
}
} catch (EOFException exp) {
exp.printStackTrace();
} finally {
stream.close();
}
return rowCount;
}
use of org.apache.hadoop.hbase.rest.model.CellSetModel in project hbase by apache.
the class TestTableScan method testQualifierAndPrefixFilters.
@Test
public void testQualifierAndPrefixFilters() throws IOException, JAXBException {
StringBuilder builder = new StringBuilder();
builder.append("/abc*");
builder.append("?");
builder.append(Constants.SCAN_FILTER + "=" + URLEncoder.encode("QualifierFilter(=,'binary:1')", "UTF-8"));
Response response = client.get("/" + TABLE + builder.toString(), Constants.MIMETYPE_XML);
assertEquals(200, response.getCode());
JAXBContext ctx = JAXBContext.newInstance(CellSetModel.class);
Unmarshaller ush = ctx.createUnmarshaller();
CellSetModel model = (CellSetModel) ush.unmarshal(response.getStream());
int count = TestScannerResource.countCellSet(model);
assertEquals(1, count);
assertEquals("abc", new String(model.getRows().get(0).getCells().get(0).getValue(), StandardCharsets.UTF_8));
}
use of org.apache.hadoop.hbase.rest.model.CellSetModel in project hbase by apache.
the class TestTableScan method testSimpleScannerJson.
@Test
public void testSimpleScannerJson() throws IOException {
// Test scanning particular columns with limit.
StringBuilder builder = new StringBuilder();
builder.append("/*");
builder.append("?");
builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_1);
builder.append("&");
builder.append(Constants.SCAN_LIMIT + "=2");
Response response = client.get("/" + TABLE + builder.toString(), Constants.MIMETYPE_JSON);
assertEquals(200, response.getCode());
assertEquals(Constants.MIMETYPE_JSON, response.getHeader("content-type"));
ObjectMapper mapper = new JacksonJaxbJsonProvider().locateMapper(CellSetModel.class, MediaType.APPLICATION_JSON_TYPE);
CellSetModel model = mapper.readValue(response.getStream(), CellSetModel.class);
int count = TestScannerResource.countCellSet(model);
assertEquals(2, count);
checkRowsNotNull(model);
// Test scanning with no limit.
builder = new StringBuilder();
builder.append("/*");
builder.append("?");
builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_2);
response = client.get("/" + TABLE + builder.toString(), Constants.MIMETYPE_JSON);
assertEquals(200, response.getCode());
assertEquals(Constants.MIMETYPE_JSON, response.getHeader("content-type"));
model = mapper.readValue(response.getStream(), CellSetModel.class);
count = TestScannerResource.countCellSet(model);
assertEquals(expectedRows2, count);
checkRowsNotNull(model);
// Test with start row and end row.
builder = new StringBuilder();
builder.append("/*");
builder.append("?");
builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_1);
builder.append("&");
builder.append(Constants.SCAN_START_ROW + "=aaa");
builder.append("&");
builder.append(Constants.SCAN_END_ROW + "=aay");
response = client.get("/" + TABLE + builder.toString(), Constants.MIMETYPE_JSON);
assertEquals(200, response.getCode());
model = mapper.readValue(response.getStream(), CellSetModel.class);
RowModel startRow = model.getRows().get(0);
assertEquals("aaa", Bytes.toString(startRow.getKey()));
RowModel endRow = model.getRows().get(model.getRows().size() - 1);
assertEquals("aax", Bytes.toString(endRow.getKey()));
count = TestScannerResource.countCellSet(model);
assertEquals(24, count);
checkRowsNotNull(model);
}
use of org.apache.hadoop.hbase.rest.model.CellSetModel 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.CellSetModel 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");
}
Aggregations