Search in sources :

Example 31 with CellModel

use of org.apache.hadoop.hbase.rest.model.CellModel 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;
}
Also used : CellSetModel(org.apache.hadoop.hbase.rest.model.CellSetModel) RowModel(org.apache.hadoop.hbase.rest.model.RowModel) CellModel(org.apache.hadoop.hbase.rest.model.CellModel) Cell(org.apache.hadoop.hbase.Cell)

Example 32 with CellModel

use of org.apache.hadoop.hbase.rest.model.CellModel 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);
}
Also used : CellSetModel(org.apache.hadoop.hbase.rest.model.CellSetModel) Response(org.apache.hadoop.hbase.rest.client.Response) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) RowModel(org.apache.hadoop.hbase.rest.model.RowModel) CellModel(org.apache.hadoop.hbase.rest.model.CellModel) ScannerModel(org.apache.hadoop.hbase.rest.model.ScannerModel)

Example 33 with CellModel

use of org.apache.hadoop.hbase.rest.model.CellModel in project hbase by apache.

the class TestScannersWithFilters method verifyScanFull.

private static void verifyScanFull(Scan s, KeyValue[] kvs) 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);
    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(), CellUtil.cloneRow(kvs[idx])));
            byte[][] split = KeyValue.parseColumn(cell.getColumn());
            assertTrue("Family mismatch", Bytes.equals(split[0], CellUtil.cloneFamily(kvs[idx])));
            assertTrue("Qualifier mismatch", Bytes.equals(split[1], CellUtil.cloneQualifier(kvs[idx])));
            assertTrue("Value mismatch", Bytes.equals(cell.getValue(), CellUtil.cloneValue(kvs[idx])));
            idx++;
        }
    }
    assertEquals("Expected " + kvs.length + " total keys but scanned " + idx, kvs.length, idx);
}
Also used : CellSetModel(org.apache.hadoop.hbase.rest.model.CellSetModel) Response(org.apache.hadoop.hbase.rest.client.Response) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) RowModel(org.apache.hadoop.hbase.rest.model.RowModel) CellModel(org.apache.hadoop.hbase.rest.model.CellModel) ScannerModel(org.apache.hadoop.hbase.rest.model.ScannerModel)

Example 34 with CellModel

use of org.apache.hadoop.hbase.rest.model.CellModel in project hbase by apache.

the class TableScanResource method get.

@GET
@Produces({ Constants.MIMETYPE_XML, Constants.MIMETYPE_JSON })
public CellSetModelStream get(@Context final UriInfo uriInfo) {
    servlet.getMetrics().incrementRequests(1);
    final int rowsToSend = userRequestedLimit;
    servlet.getMetrics().incrementSucessfulScanRequests(1);
    final Iterator<Result> itr = results.iterator();
    return new CellSetModelStream(new ArrayList<RowModel>() {

        public Iterator<RowModel> iterator() {
            return new Iterator<RowModel>() {

                int count = rowsToSend;

                @Override
                public boolean hasNext() {
                    if (count > 0) {
                        return itr.hasNext();
                    } else {
                        return false;
                    }
                }

                @Override
                public void remove() {
                    throw new UnsupportedOperationException("Remove method cannot be used in CellSetModelStream");
                }

                @Override
                public RowModel next() {
                    Result rs = itr.next();
                    if ((rs == null) || (count <= 0)) {
                        return null;
                    }
                    byte[] rowKey = rs.getRow();
                    RowModel rModel = new RowModel(rowKey);
                    List<Cell> kvs = rs.listCells();
                    for (Cell kv : kvs) {
                        rModel.addCell(new CellModel(CellUtil.cloneFamily(kv), CellUtil.cloneQualifier(kv), kv.getTimestamp(), CellUtil.cloneValue(kv)));
                    }
                    count--;
                    return rModel;
                }
            };
        }
    });
}
Also used : Result(org.apache.hadoop.hbase.client.Result) Iterator(java.util.Iterator) RowModel(org.apache.hadoop.hbase.rest.model.RowModel) ArrayList(java.util.ArrayList) List(java.util.List) CellModel(org.apache.hadoop.hbase.rest.model.CellModel) Cell(org.apache.hadoop.hbase.Cell) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 35 with CellModel

use of org.apache.hadoop.hbase.rest.model.CellModel in project hbase by apache.

the class RowResource method checkAndPut.

/**
   * Validates the input request parameters, parses columns from CellSetModel,
   * and invokes checkAndPut on HTable.
   *
   * @param model instance of CellSetModel
   * @return Response 200 OK, 304 Not modified, 400 Bad request
   */
Response checkAndPut(final CellSetModel model) {
    Table table = null;
    try {
        table = servlet.getTable(tableResource.getName());
        if (model.getRows().size() != 1) {
            servlet.getMetrics().incrementFailedPutRequests(1);
            return Response.status(Response.Status.BAD_REQUEST).type(MIMETYPE_TEXT).entity("Bad request: Number of rows specified is not 1." + CRLF).build();
        }
        RowModel rowModel = model.getRows().get(0);
        byte[] key = rowModel.getKey();
        if (key == null) {
            key = rowspec.getRow();
        }
        List<CellModel> cellModels = rowModel.getCells();
        int cellModelCount = cellModels.size();
        if (key == null || cellModelCount <= 1) {
            servlet.getMetrics().incrementFailedPutRequests(1);
            return Response.status(Response.Status.BAD_REQUEST).type(MIMETYPE_TEXT).entity("Bad request: Either row key is null or no data found for columns specified." + CRLF).build();
        }
        Put put = new Put(key);
        boolean retValue;
        CellModel valueToCheckCell = cellModels.get(cellModelCount - 1);
        byte[] valueToCheckColumn = valueToCheckCell.getColumn();
        byte[][] valueToPutParts = KeyValue.parseColumn(valueToCheckColumn);
        if (valueToPutParts.length == 2 && valueToPutParts[1].length > 0) {
            CellModel valueToPutCell = null;
            // and track if the check cell's latest value is also sent
            for (int i = 0, n = cellModelCount - 1; i < n; i++) {
                CellModel cell = cellModels.get(i);
                byte[] col = cell.getColumn();
                if (col == null) {
                    servlet.getMetrics().incrementFailedPutRequests(1);
                    return Response.status(Response.Status.BAD_REQUEST).type(MIMETYPE_TEXT).entity("Bad request: Column found to be null." + CRLF).build();
                }
                byte[][] parts = KeyValue.parseColumn(col);
                if (parts.length != 2) {
                    return Response.status(Response.Status.BAD_REQUEST).type(MIMETYPE_TEXT).entity("Bad request" + CRLF).build();
                }
                put.addImmutable(parts[0], parts[1], cell.getTimestamp(), cell.getValue());
                if (Bytes.equals(col, valueToCheckCell.getColumn())) {
                    valueToPutCell = cell;
                }
            }
            if (valueToPutCell == null) {
                servlet.getMetrics().incrementFailedPutRequests(1);
                return Response.status(Response.Status.BAD_REQUEST).type(MIMETYPE_TEXT).entity("Bad request: The column to put and check do not match." + CRLF).build();
            } else {
                retValue = table.checkAndPut(key, valueToPutParts[0], valueToPutParts[1], valueToCheckCell.getValue(), put);
            }
        } else {
            servlet.getMetrics().incrementFailedPutRequests(1);
            return Response.status(Response.Status.BAD_REQUEST).type(MIMETYPE_TEXT).entity("Bad request: Column incorrectly specified." + CRLF).build();
        }
        if (LOG.isTraceEnabled()) {
            LOG.trace("CHECK-AND-PUT " + put.toString() + ", returns " + retValue);
        }
        if (!retValue) {
            servlet.getMetrics().incrementFailedPutRequests(1);
            return Response.status(Response.Status.NOT_MODIFIED).type(MIMETYPE_TEXT).entity("Value not Modified" + CRLF).build();
        }
        ResponseBuilder response = Response.ok();
        servlet.getMetrics().incrementSucessfulPutRequests(1);
        return response.build();
    } catch (Exception e) {
        servlet.getMetrics().incrementFailedPutRequests(1);
        return processException(e);
    } finally {
        if (table != null)
            try {
                table.close();
            } catch (IOException ioe) {
                LOG.debug("Exception received while closing the table", ioe);
            }
    }
}
Also used : Table(org.apache.hadoop.hbase.client.Table) RowModel(org.apache.hadoop.hbase.rest.model.RowModel) CellModel(org.apache.hadoop.hbase.rest.model.CellModel) IOException(java.io.IOException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) Put(org.apache.hadoop.hbase.client.Put) IOException(java.io.IOException)

Aggregations

CellModel (org.apache.hadoop.hbase.rest.model.CellModel)37 RowModel (org.apache.hadoop.hbase.rest.model.RowModel)37 CellSetModel (org.apache.hadoop.hbase.rest.model.CellSetModel)30 Response (org.apache.hadoop.hbase.rest.client.Response)24 StringWriter (java.io.StringWriter)11 Test (org.junit.Test)10 Cell (org.apache.hadoop.hbase.Cell)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)4 GET (javax.ws.rs.GET)4 Produces (javax.ws.rs.Produces)4 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)4 Put (org.apache.hadoop.hbase.client.Put)3 Result (org.apache.hadoop.hbase.client.Result)3 Table (org.apache.hadoop.hbase.client.Table)3 List (java.util.List)2 ScannerModel (org.apache.hadoop.hbase.rest.model.ScannerModel)2 InterruptedIOException (java.io.InterruptedIOException)1 Iterator (java.util.Iterator)1