Search in sources :

Example 36 with CellModel

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

the class RowResource method update.

Response update(final CellSetModel model, final boolean replace) {
    servlet.getMetrics().incrementRequests(1);
    if (servlet.isReadOnly()) {
        servlet.getMetrics().incrementFailedPutRequests(1);
        return Response.status(Response.Status.FORBIDDEN).type(MIMETYPE_TEXT).entity("Forbidden" + CRLF).build();
    }
    if (CHECK_PUT.equalsIgnoreCase(check)) {
        return checkAndPut(model);
    } else if (CHECK_DELETE.equalsIgnoreCase(check)) {
        return checkAndDelete(model);
    } else if (check != null && check.length() > 0) {
        return Response.status(Response.Status.BAD_REQUEST).type(MIMETYPE_TEXT).entity("Invalid check value '" + check + "'" + CRLF).build();
    }
    Table table = null;
    try {
        List<RowModel> rows = model.getRows();
        List<Put> puts = new ArrayList<>();
        for (RowModel row : rows) {
            byte[] key = row.getKey();
            if (key == null) {
                key = rowspec.getRow();
            }
            if (key == null) {
                servlet.getMetrics().incrementFailedPutRequests(1);
                return Response.status(Response.Status.BAD_REQUEST).type(MIMETYPE_TEXT).entity("Bad request: Row key not specified." + CRLF).build();
            }
            Put put = new Put(key);
            int i = 0;
            for (CellModel cell : row.getCells()) {
                byte[] col = cell.getColumn();
                if (col == null)
                    try {
                        col = rowspec.getColumns()[i++];
                    } catch (ArrayIndexOutOfBoundsException e) {
                        col = null;
                    }
                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());
            }
            puts.add(put);
            if (LOG.isTraceEnabled()) {
                LOG.trace("PUT " + put.toString());
            }
        }
        table = servlet.getTable(tableResource.getName());
        table.put(puts);
        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) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Put(org.apache.hadoop.hbase.client.Put) IOException(java.io.IOException) RowModel(org.apache.hadoop.hbase.rest.model.RowModel) CellModel(org.apache.hadoop.hbase.rest.model.CellModel) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder)

Example 37 with CellModel

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

the class MultiRowResource method get.

@GET
@Produces({ MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF, MIMETYPE_PROTOBUF_IETF })
public Response get(@Context final 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);
            }
            if (this.columns != null) {
                for (int i = 0; i < this.columns.length; i++) {
                    rowSpec.addColumn(this.columns[i].getBytes());
                }
            }
            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 {
                if (LOG.isTraceEnabled()) {
                    LOG.trace("The row : " + rk + " not found in the table.");
                }
            }
        }
        if (model.getRows().isEmpty()) {
            //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 (IOException e) {
        servlet.getMetrics().incrementFailedGetRequests(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) IOException(java.io.IOException) Cell(org.apache.hadoop.hbase.Cell) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

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