Search in sources :

Example 1 with CellSetModel

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

the class ProtobufStreamingUtil method createModelFromResults.

private CellSetModel createModelFromResults(Result[] results) {
    CellSetModel cellSetModel = new CellSetModel();
    for (Result rs : results) {
        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)));
        }
        cellSetModel.addRow(rModel);
    }
    return cellSetModel;
}
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) Result(org.apache.hadoop.hbase.client.Result)

Example 2 with CellSetModel

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

the class RowResource method increment.

/**
 * Validates the input request parameters, parses columns from CellSetModel,
 * and invokes Increment on HTable.
 *
 * @param model instance of CellSetModel
 * @return Response 200 OK, 304 Not modified, 400 Bad request
 */
Response increment(final CellSetModel model) {
    Table table = null;
    Increment increment = null;
    try {
        table = servlet.getTable(tableResource.getName());
        if (model.getRows().size() != 1) {
            servlet.getMetrics().incrementFailedIncrementRequests(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();
        }
        if (key == null) {
            servlet.getMetrics().incrementFailedIncrementRequests(1);
            return Response.status(Response.Status.BAD_REQUEST).type(MIMETYPE_TEXT).entity("Bad request: Row key found to be null." + CRLF).build();
        }
        increment = new Increment(key);
        increment.setReturnResults(returnResult);
        int i = 0;
        for (CellModel cell : rowModel.getCells()) {
            byte[] col = cell.getColumn();
            if (col == null) {
                try {
                    col = rowspec.getColumns()[i++];
                } catch (ArrayIndexOutOfBoundsException e) {
                    col = null;
                }
            }
            if (col == null) {
                servlet.getMetrics().incrementFailedIncrementRequests(1);
                return Response.status(Response.Status.BAD_REQUEST).type(MIMETYPE_TEXT).entity("Bad request: Column found to be null." + CRLF).build();
            }
            byte[][] parts = CellUtil.parseColumn(col);
            if (parts.length != 2) {
                servlet.getMetrics().incrementFailedIncrementRequests(1);
                return Response.status(Response.Status.BAD_REQUEST).type(MIMETYPE_TEXT).entity("Bad request: Column incorrectly specified." + CRLF).build();
            }
            increment.addColumn(parts[0], parts[1], Long.parseLong(Bytes.toStringBinary(cell.getValue())));
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("INCREMENT " + increment.toString());
        }
        Result result = table.increment(increment);
        if (returnResult) {
            if (result.isEmpty()) {
                servlet.getMetrics().incrementFailedIncrementRequests(1);
                return Response.status(Response.Status.NOT_MODIFIED).type(MIMETYPE_TEXT).entity("Increment return empty." + CRLF).build();
            }
            CellSetModel rModel = new CellSetModel();
            RowModel rRowModel = new RowModel(result.getRow());
            for (Cell cell : result.listCells()) {
                rRowModel.addCell(new CellModel(CellUtil.cloneFamily(cell), CellUtil.cloneQualifier(cell), cell.getTimestamp(), CellUtil.cloneValue(cell)));
            }
            rModel.addRow(rowModel);
            servlet.getMetrics().incrementSucessfulIncrementRequests(1);
            return Response.ok(rModel).build();
        }
        ResponseBuilder response = Response.ok();
        servlet.getMetrics().incrementSucessfulIncrementRequests(1);
        return response.build();
    } catch (Exception e) {
        servlet.getMetrics().incrementFailedIncrementRequests(1);
        return processException(e);
    } finally {
        if (table != null)
            try {
                table.close();
            } catch (IOException ioe) {
                LOG.debug("Exception received while closing the table " + table.getName(), ioe);
            }
    }
}
Also used : Table(org.apache.hadoop.hbase.client.Table) CellSetModel(org.apache.hadoop.hbase.rest.model.CellSetModel) IOException(java.io.IOException) IOException(java.io.IOException) Result(org.apache.hadoop.hbase.client.Result) Increment(org.apache.hadoop.hbase.client.Increment) RowModel(org.apache.hadoop.hbase.rest.model.RowModel) CellModel(org.apache.hadoop.hbase.rest.model.CellModel) ResponseBuilder(org.apache.hbase.thirdparty.javax.ws.rs.core.Response.ResponseBuilder) Cell(org.apache.hadoop.hbase.Cell)

Example 3 with CellSetModel

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

the class RowResource method append.

/**
 * Validates the input request parameters, parses columns from CellSetModel,
 * and invokes Append on HTable.
 *
 * @param model instance of CellSetModel
 * @return Response 200 OK, 304 Not modified, 400 Bad request
 */
Response append(final CellSetModel model) {
    Table table = null;
    Append append = null;
    try {
        table = servlet.getTable(tableResource.getName());
        if (model.getRows().size() != 1) {
            servlet.getMetrics().incrementFailedAppendRequests(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();
        }
        if (key == null) {
            servlet.getMetrics().incrementFailedAppendRequests(1);
            return Response.status(Response.Status.BAD_REQUEST).type(MIMETYPE_TEXT).entity("Bad request: Row key found to be null." + CRLF).build();
        }
        append = new Append(key);
        append.setReturnResults(returnResult);
        int i = 0;
        for (CellModel cell : rowModel.getCells()) {
            byte[] col = cell.getColumn();
            if (col == null) {
                try {
                    col = rowspec.getColumns()[i++];
                } catch (ArrayIndexOutOfBoundsException e) {
                    col = null;
                }
            }
            if (col == null) {
                servlet.getMetrics().incrementFailedAppendRequests(1);
                return Response.status(Response.Status.BAD_REQUEST).type(MIMETYPE_TEXT).entity("Bad request: Column found to be null." + CRLF).build();
            }
            byte[][] parts = CellUtil.parseColumn(col);
            if (parts.length != 2) {
                servlet.getMetrics().incrementFailedAppendRequests(1);
                return Response.status(Response.Status.BAD_REQUEST).type(MIMETYPE_TEXT).entity("Bad request: Column incorrectly specified." + CRLF).build();
            }
            append.addColumn(parts[0], parts[1], cell.getValue());
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("APPEND " + append.toString());
        }
        Result result = table.append(append);
        if (returnResult) {
            if (result.isEmpty()) {
                servlet.getMetrics().incrementFailedAppendRequests(1);
                return Response.status(Response.Status.NOT_MODIFIED).type(MIMETYPE_TEXT).entity("Append return empty." + CRLF).build();
            }
            CellSetModel rModel = new CellSetModel();
            RowModel rRowModel = new RowModel(result.getRow());
            for (Cell cell : result.listCells()) {
                rRowModel.addCell(new CellModel(CellUtil.cloneFamily(cell), CellUtil.cloneQualifier(cell), cell.getTimestamp(), CellUtil.cloneValue(cell)));
            }
            rModel.addRow(rRowModel);
            servlet.getMetrics().incrementSucessfulAppendRequests(1);
            return Response.ok(rModel).build();
        }
        servlet.getMetrics().incrementSucessfulAppendRequests(1);
        return Response.ok().build();
    } catch (Exception e) {
        servlet.getMetrics().incrementFailedAppendRequests(1);
        return processException(e);
    } finally {
        if (table != null)
            try {
                table.close();
            } catch (IOException ioe) {
                LOG.debug("Exception received while closing the table" + table.getName(), ioe);
            }
    }
}
Also used : Table(org.apache.hadoop.hbase.client.Table) CellSetModel(org.apache.hadoop.hbase.rest.model.CellSetModel) IOException(java.io.IOException) IOException(java.io.IOException) Result(org.apache.hadoop.hbase.client.Result) Append(org.apache.hadoop.hbase.client.Append) RowModel(org.apache.hadoop.hbase.rest.model.RowModel) CellModel(org.apache.hadoop.hbase.rest.model.CellModel) Cell(org.apache.hadoop.hbase.Cell)

Example 4 with CellSetModel

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

the class RowResource method get.

@GET
@Produces({ MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF, MIMETYPE_PROTOBUF_IETF })
public Response get(@Context final UriInfo uriInfo) {
    if (LOG.isTraceEnabled()) {
        LOG.trace("GET " + uriInfo.getAbsolutePath());
    }
    servlet.getMetrics().incrementRequests(1);
    MultivaluedMap<String, String> params = uriInfo.getQueryParameters();
    try {
        ResultGenerator generator = ResultGenerator.fromRowSpec(tableResource.getName(), rowspec, null, !params.containsKey(NOCACHE_PARAM_NAME));
        if (!generator.hasNext()) {
            servlet.getMetrics().incrementFailedGetRequests(1);
            return Response.status(Response.Status.NOT_FOUND).type(MIMETYPE_TEXT).entity("Not found" + CRLF).build();
        }
        int count = 0;
        CellSetModel model = new CellSetModel();
        Cell value = generator.next();
        byte[] rowKey = CellUtil.cloneRow(value);
        RowModel rowModel = new RowModel(rowKey);
        do {
            if (!Bytes.equals(CellUtil.cloneRow(value), rowKey)) {
                model.addRow(rowModel);
                rowKey = CellUtil.cloneRow(value);
                rowModel = new RowModel(rowKey);
            }
            rowModel.addCell(new CellModel(CellUtil.cloneFamily(value), CellUtil.cloneQualifier(value), value.getTimestamp(), CellUtil.cloneValue(value)));
            if (++count > rowspec.getMaxValues()) {
                break;
            }
            value = generator.next();
        } while (value != null);
        model.addRow(rowModel);
        servlet.getMetrics().incrementSucessfulGetRequests(1);
        return Response.ok(model).build();
    } catch (Exception e) {
        servlet.getMetrics().incrementFailedPutRequests(1);
        return processException(e);
    }
}
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) IOException(java.io.IOException) Produces(org.apache.hbase.thirdparty.javax.ws.rs.Produces) GET(org.apache.hbase.thirdparty.javax.ws.rs.GET)

Example 5 with CellSetModel

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

the class ScannerInstanceResource method get.

@GET
@Produces({ MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF, MIMETYPE_PROTOBUF_IETF })
public Response get(@Context final UriInfo uriInfo, @QueryParam("n") int maxRows, @QueryParam("c") final int maxValues) {
    if (LOG.isTraceEnabled()) {
        LOG.trace("GET " + uriInfo.getAbsolutePath());
    }
    servlet.getMetrics().incrementRequests(1);
    if (generator == null) {
        servlet.getMetrics().incrementFailedGetRequests(1);
        return Response.status(Response.Status.NOT_FOUND).type(MIMETYPE_TEXT).entity("Not found" + CRLF).build();
    } else {
        // Updated the connection access time for each client next() call
        RESTServlet.getInstance().getConnectionCache().updateConnectionAccessTime();
    }
    CellSetModel model = new CellSetModel();
    RowModel rowModel = null;
    byte[] rowKey = null;
    int limit = batch;
    if (maxValues > 0) {
        limit = maxValues;
    }
    int count = limit;
    do {
        Cell value = null;
        try {
            value = generator.next();
        } catch (IllegalStateException e) {
            if (ScannerResource.delete(id)) {
                servlet.getMetrics().incrementSucessfulDeleteRequests(1);
            } else {
                servlet.getMetrics().incrementFailedDeleteRequests(1);
            }
            servlet.getMetrics().incrementFailedGetRequests(1);
            return Response.status(Response.Status.GONE).type(MIMETYPE_TEXT).entity("Gone" + CRLF).build();
        } catch (IllegalArgumentException e) {
            Throwable t = e.getCause();
            if (t instanceof TableNotFoundException) {
                return Response.status(Response.Status.NOT_FOUND).type(MIMETYPE_TEXT).entity("Not found" + CRLF).build();
            }
            throw e;
        }
        if (value == null) {
            if (LOG.isTraceEnabled()) {
                LOG.trace("generator exhausted");
            }
            // returned
            if (count == limit) {
                return Response.noContent().build();
            }
            break;
        }
        if (rowKey == null) {
            rowKey = CellUtil.cloneRow(value);
            rowModel = new RowModel(rowKey);
        }
        if (!Bytes.equals(CellUtil.cloneRow(value), rowKey)) {
            // specified number of rows
            if (maxRows > 0) {
                if (--maxRows == 0) {
                    generator.putBack(value);
                    break;
                }
            }
            model.addRow(rowModel);
            rowKey = CellUtil.cloneRow(value);
            rowModel = new RowModel(rowKey);
        }
        rowModel.addCell(new CellModel(CellUtil.cloneFamily(value), CellUtil.cloneQualifier(value), value.getTimestamp(), CellUtil.cloneValue(value)));
    } while (--count > 0);
    model.addRow(rowModel);
    ResponseBuilder response = Response.ok(model);
    response.cacheControl(cacheControl);
    servlet.getMetrics().incrementSucessfulGetRequests(1);
    return response.build();
}
Also used : TableNotFoundException(org.apache.hadoop.hbase.TableNotFoundException) CellSetModel(org.apache.hadoop.hbase.rest.model.CellSetModel) RowModel(org.apache.hadoop.hbase.rest.model.RowModel) CellModel(org.apache.hadoop.hbase.rest.model.CellModel) ResponseBuilder(org.apache.hbase.thirdparty.javax.ws.rs.core.Response.ResponseBuilder) Cell(org.apache.hadoop.hbase.Cell) Produces(org.apache.hbase.thirdparty.javax.ws.rs.Produces) GET(org.apache.hbase.thirdparty.javax.ws.rs.GET)

Aggregations

CellSetModel (org.apache.hadoop.hbase.rest.model.CellSetModel)61 Response (org.apache.hadoop.hbase.rest.client.Response)44 RowModel (org.apache.hadoop.hbase.rest.model.RowModel)42 CellModel (org.apache.hadoop.hbase.rest.model.CellModel)37 Test (org.junit.Test)26 StringWriter (java.io.StringWriter)14 IOException (java.io.IOException)12 ByteArrayInputStream (java.io.ByteArrayInputStream)11 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)9 Cell (org.apache.hadoop.hbase.Cell)9 JacksonJaxbJsonProvider (org.apache.hbase.thirdparty.com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider)9 InterruptedIOException (java.io.InterruptedIOException)7 JAXBContext (javax.xml.bind.JAXBContext)7 Unmarshaller (javax.xml.bind.Unmarshaller)7 Map (java.util.Map)6 ScannerModel (org.apache.hadoop.hbase.rest.model.ScannerModel)6 HashMap (java.util.HashMap)5 Result (org.apache.hadoop.hbase.client.Result)5 KeyValue (org.apache.hadoop.hbase.KeyValue)4 Put (org.apache.hadoop.hbase.client.Put)3