Search in sources :

Example 31 with Cell

use of org.apache.hadoop.hbase.Cell in project hbase by apache.

the class ProtobufUtil method toGet.

/**
   * Convert a protocol buffer Mutate to a Get.
   * @param proto the protocol buffer Mutate to convert.
   * @param cellScanner
   * @return the converted client get.
   * @throws IOException
   */
public static Get toGet(final MutationProto proto, final CellScanner cellScanner) throws IOException {
    MutationType type = proto.getMutateType();
    assert type == MutationType.INCREMENT || type == MutationType.APPEND : type.name();
    byte[] row = proto.hasRow() ? proto.getRow().toByteArray() : null;
    Get get = null;
    int cellCount = proto.hasAssociatedCellCount() ? proto.getAssociatedCellCount() : 0;
    if (cellCount > 0) {
        // The proto has metadata only and the data is separate to be found in the cellScanner.
        if (cellScanner == null) {
            throw new DoNotRetryIOException("Cell count of " + cellCount + " but no cellScanner: " + TextFormat.shortDebugString(proto));
        }
        for (int i = 0; i < cellCount; i++) {
            if (!cellScanner.advance()) {
                throw new DoNotRetryIOException("Cell count of " + cellCount + " but at index " + i + " no cell returned: " + TextFormat.shortDebugString(proto));
            }
            Cell cell = cellScanner.current();
            if (get == null) {
                get = new Get(CellUtil.cloneRow(cell));
            }
            get.addColumn(CellUtil.cloneFamily(cell), CellUtil.cloneQualifier(cell));
        }
    } else {
        get = new Get(row);
        for (ColumnValue column : proto.getColumnValueList()) {
            byte[] family = column.getFamily().toByteArray();
            for (QualifierValue qv : column.getQualifierValueList()) {
                byte[] qualifier = qv.getQualifier().toByteArray();
                if (!qv.hasValue()) {
                    throw new DoNotRetryIOException("Missing required field: qualifier value");
                }
                get.addColumn(family, qualifier);
            }
        }
    }
    if (proto.hasTimeRange()) {
        TimeRange timeRange = protoToTimeRange(proto.getTimeRange());
        get.setTimeRange(timeRange);
    }
    for (NameBytesPair attribute : proto.getAttributeList()) {
        get.setAttribute(attribute.getName(), attribute.getValue().toByteArray());
    }
    return get;
}
Also used : TimeRange(org.apache.hadoop.hbase.io.TimeRange) MutationType(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.MutationProto.MutationType) NameBytesPair(org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos.NameBytesPair) DoNotRetryIOException(org.apache.hadoop.hbase.DoNotRetryIOException) Get(org.apache.hadoop.hbase.client.Get) QualifierValue(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.MutationProto.ColumnValue.QualifierValue) ColumnValue(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.MutationProto.ColumnValue) Cell(org.apache.hadoop.hbase.Cell) ByteBufferCell(org.apache.hadoop.hbase.ByteBufferCell)

Example 32 with Cell

use of org.apache.hadoop.hbase.Cell 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(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 33 with Cell

use of org.apache.hadoop.hbase.Cell in project hbase by apache.

the class RowResultGenerator method next.

public Cell next() {
    if (cache != null) {
        Cell kv = cache;
        cache = null;
        return kv;
    }
    if (valuesI == null) {
        return null;
    }
    try {
        return valuesI.next();
    } catch (NoSuchElementException e) {
        return null;
    }
}
Also used : Cell(org.apache.hadoop.hbase.Cell) NoSuchElementException(java.util.NoSuchElementException)

Example 34 with Cell

use of org.apache.hadoop.hbase.Cell 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();
    }
    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(javax.ws.rs.core.Response.ResponseBuilder) Cell(org.apache.hadoop.hbase.Cell) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 35 with Cell

use of org.apache.hadoop.hbase.Cell in project hbase by apache.

the class ScannerResultGenerator method next.

public Cell next() {
    if (cache != null) {
        Cell kv = cache;
        cache = null;
        return kv;
    }
    boolean loop;
    do {
        loop = false;
        if (rowI != null) {
            if (rowI.hasNext()) {
                return rowI.next();
            } else {
                rowI = null;
            }
        }
        if (cached != null) {
            rowI = cached.listCells().iterator();
            loop = true;
            cached = null;
        } else {
            Result result = null;
            try {
                result = scanner.next();
            } catch (UnknownScannerException e) {
                throw new IllegalArgumentException(e);
            } catch (TableNotEnabledException tnee) {
                throw new IllegalStateException(tnee);
            } catch (TableNotFoundException tnfe) {
                throw new IllegalArgumentException(tnfe);
            } catch (IOException e) {
                LOG.error(StringUtils.stringifyException(e));
            }
            if (result != null && !result.isEmpty()) {
                rowI = result.listCells().iterator();
                loop = true;
            }
        }
    } while (loop);
    return null;
}
Also used : TableNotFoundException(org.apache.hadoop.hbase.TableNotFoundException) IOException(java.io.IOException) Cell(org.apache.hadoop.hbase.Cell) UnknownScannerException(org.apache.hadoop.hbase.UnknownScannerException) Result(org.apache.hadoop.hbase.client.Result) TableNotEnabledException(org.apache.hadoop.hbase.TableNotEnabledException)

Aggregations

Cell (org.apache.hadoop.hbase.Cell)862 Test (org.junit.Test)326 ArrayList (java.util.ArrayList)323 Scan (org.apache.hadoop.hbase.client.Scan)258 KeyValue (org.apache.hadoop.hbase.KeyValue)220 Result (org.apache.hadoop.hbase.client.Result)203 Put (org.apache.hadoop.hbase.client.Put)159 IOException (java.io.IOException)123 ResultScanner (org.apache.hadoop.hbase.client.ResultScanner)106 Get (org.apache.hadoop.hbase.client.Get)85 Table (org.apache.hadoop.hbase.client.Table)85 List (java.util.List)80 TableName (org.apache.hadoop.hbase.TableName)77 Delete (org.apache.hadoop.hbase.client.Delete)75 CellScanner (org.apache.hadoop.hbase.CellScanner)69 Configuration (org.apache.hadoop.conf.Configuration)62 InterruptedIOException (java.io.InterruptedIOException)48 Map (java.util.Map)45 Path (org.apache.hadoop.fs.Path)45 RegionScanner (org.apache.hadoop.hbase.regionserver.RegionScanner)45