private static void verifyScan(Scan s, long expectedRows, long expectedKeys) throws Exception { ScannerModel model = ScannerModel.fromScan(s); model.setBatch(Integer.MAX_VALUE); // fetch it all at once 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); 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); }
private static int fullTableScan(ScannerModel model) throws IOException { model.setBatch(100); Response response = client.put( "/" + TABLE + "/scanner", Constants.MIMETYPE_PROTOBUF, model.createProtobufOutput()); assertEquals(response.getCode(), 201); String scannerURI = response.getLocation(); assertNotNull(scannerURI); int count = 0; while (true) { response = client.get(scannerURI, Constants.MIMETYPE_PROTOBUF); assertTrue(response.getCode() == 200 || response.getCode() == 204); if (response.getCode() == 200) { assertEquals(Constants.MIMETYPE_PROTOBUF, response.getHeader("content-type")); CellSetModel cellSet = new CellSetModel(); cellSet.getObjectFromMessage(response.getBody()); Iterator<RowModel> rows = cellSet.getRows().iterator(); while (rows.hasNext()) { RowModel row = rows.next(); Iterator<CellModel> cells = row.getCells().iterator(); while (cells.hasNext()) { cells.next(); count++; } } } else { break; } } // delete the scanner response = client.delete(scannerURI); assertEquals(response.getCode(), 200); return count; }
@GET @Produces({MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF, MIMETYPE_PROTOBUF_IETF}) public Response get(final @Context UriInfo uriInfo) { MultivaluedMap<String, String> params = uriInfo.getQueryParameters(); servlet.getMetrics().incrementRequests(1); try { CellSetModel model = new CellSetModel(); for (String rk : params.get(ROW_KEYS_PARAM_NAME)) { RowSpec rowSpec = new RowSpec(rk); if (this.versions != null) { rowSpec.setMaxVersions(this.versions); } ResultGenerator generator = ResultGenerator.fromRowSpec( this.tableResource.getName(), rowSpec, null, !params.containsKey(NOCACHE_PARAM_NAME)); Cell value = null; RowModel rowModel = new RowModel(rk); if (generator.hasNext()) { while ((value = generator.next()) != null) { rowModel.addCell( new CellModel( CellUtil.cloneFamily(value), CellUtil.cloneQualifier(value), value.getTimestamp(), CellUtil.cloneValue(value))); } model.addRow(rowModel); } else { LOG.trace("The row : " + rk + " not found in the table."); } } if (model.getRows().size() == 0) { // If no rows found. servlet.getMetrics().incrementFailedGetRequests(1); return Response.status(Response.Status.NOT_FOUND) .type(MIMETYPE_TEXT) .entity("No rows found." + CRLF) .build(); } else { servlet.getMetrics().incrementSucessfulGetRequests(1); return Response.ok(model).build(); } } catch (Exception e) { servlet.getMetrics().incrementFailedGetRequests(1); return processException(e); } }
private static void verifyScanFull(Scan s, KeyValue[] kvs) throws Exception { ScannerModel model = ScannerModel.fromScan(s); model.setBatch(Integer.MAX_VALUE); // fetch it all at once 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); CellSetModel cellSet = (CellSetModel) unmarshaller.unmarshal(new ByteArrayInputStream(response.getBody())); // delete the scanner response = client.delete(scannerURI); assertEquals(response.getCode(), 200); int row = 0; int idx = 0; Iterator<RowModel> i = cellSet.getRows().iterator(); for (boolean done = true; done; row++) { done = i.hasNext(); if (!done) break; RowModel rowModel = i.next(); List<CellModel> cells = rowModel.getCells(); if (cells.isEmpty()) break; assertTrue( "Scanned too many keys! Only expected " + kvs.length + " total but already scanned " + (cells.size() + idx), kvs.length >= idx + cells.size()); for (CellModel cell : cells) { assertTrue("Row mismatch", Bytes.equals(rowModel.getKey(), kvs[idx].getRow())); byte[][] split = KeyValue.parseColumn(cell.getColumn()); assertTrue("Family mismatch", Bytes.equals(split[0], kvs[idx].getFamily())); assertTrue("Qualifier mismatch", Bytes.equals(split[1], kvs[idx].getQualifier())); assertTrue("Value mismatch", Bytes.equals(cell.getValue(), kvs[idx].getValue())); idx++; } } assertEquals("Expected " + kvs.length + " total keys but scanned " + idx, kvs.length, idx); }
@Test public void testSimpleScannerPB() throws IOException { final int BATCH_SIZE = 10; // new scanner ScannerModel model = new ScannerModel(); model.setBatch(BATCH_SIZE); model.addColumn(Bytes.toBytes(COLUMN_1)); // test put operation is forbidden in read-only mode conf.set("hbase.rest.readonly", "true"); Response response = client.put( "/" + TABLE + "/scanner", Constants.MIMETYPE_PROTOBUF, model.createProtobufOutput()); assertEquals(response.getCode(), 403); String scannerURI = response.getLocation(); assertNull(scannerURI); // recall previous put operation with read-only off conf.set("hbase.rest.readonly", "false"); response = client.put( "/" + TABLE + "/scanner", Constants.MIMETYPE_PROTOBUF, model.createProtobufOutput()); assertEquals(response.getCode(), 201); scannerURI = response.getLocation(); assertNotNull(scannerURI); // get a cell set response = client.get(scannerURI, Constants.MIMETYPE_PROTOBUF); assertEquals(response.getCode(), 200); assertEquals(Constants.MIMETYPE_PROTOBUF, response.getHeader("content-type")); CellSetModel cellSet = new CellSetModel(); cellSet.getObjectFromMessage(response.getBody()); // confirm batch size conformance assertEquals(countCellSet(cellSet), BATCH_SIZE); // test delete scanner operation is forbidden in read-only mode conf.set("hbase.rest.readonly", "true"); response = client.delete(scannerURI); assertEquals(response.getCode(), 403); // recall previous delete scanner operation with read-only off conf.set("hbase.rest.readonly", "false"); response = client.delete(scannerURI); assertEquals(response.getCode(), 200); }
private static void verifyScanNoEarlyOut(Scan s, long expectedRows, long expectedKeys) throws Exception { ScannerModel model = ScannerModel.fromScan(s); model.setBatch(Integer.MAX_VALUE); // fetch it all at once 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); 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); }
static int countCellSet(CellSetModel model) { int count = 0; Iterator<RowModel> rows = model.getRows().iterator(); while (rows.hasNext()) { RowModel row = rows.next(); Iterator<CellModel> cells = row.getCells().iterator(); while (cells.hasNext()) { cells.next(); count++; } } return count; }
@GET @Produces({MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF}) public Response get( final @Context UriInfo uriInfo, @QueryParam("n") int maxRows, final @QueryParam("c") int maxValues) { if (LOG.isDebugEnabled()) { LOG.debug("GET " + uriInfo.getAbsolutePath()); } servlet.getMetrics().incrementRequests(1); CellSetModel model = new CellSetModel(); RowModel rowModel = null; byte[] rowKey = null; int limit = batch; if (maxValues > 0) { limit = maxValues; } int count = limit; do { KeyValue value = null; try { value = generator.next(); } catch (IllegalStateException e) { ScannerResource.delete(id); throw new WebApplicationException(Response.Status.GONE); } if (value == null) { LOG.info("generator exhausted"); // respond with 204 (No Content) if an empty cell set would be // returned if (count == limit) { return Response.noContent().build(); } break; } if (rowKey == null) { rowKey = value.getRow(); rowModel = new RowModel(rowKey); } if (!Bytes.equals(value.getRow(), rowKey)) { // if maxRows was given as a query param, stop if we would exceed the // specified number of rows if (maxRows > 0) { if (--maxRows == 0) { generator.putBack(value); break; } } model.addRow(rowModel); rowKey = value.getRow(); rowModel = new RowModel(rowKey); } rowModel.addCell( new CellModel( value.getFamily(), value.getQualifier(), value.getTimestamp(), value.getValue())); } while (--count > 0); model.addRow(rowModel); ResponseBuilder response = Response.ok(model); response.cacheControl(cacheControl); return response.build(); }