Search in sources :

Example 26 with CellModel

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

the class RowResourceBase method checkAndPutValuePB.

protected static Response checkAndPutValuePB(String url, String table, String row, String column, String valueToCheck, String valueToPut, HashMap<String, String> otherCells) throws IOException {
    RowModel rowModel = new RowModel(row);
    rowModel.addCell(new CellModel(Bytes.toBytes(column), Bytes.toBytes(valueToPut)));
    if (otherCells != null) {
        for (Map.Entry<String, String> entry : otherCells.entrySet()) {
            rowModel.addCell(new CellModel(Bytes.toBytes(entry.getKey()), Bytes.toBytes(entry.getValue())));
        }
    }
    // This Cell need to be added as last cell.
    rowModel.addCell(new CellModel(Bytes.toBytes(column), Bytes.toBytes(valueToCheck)));
    CellSetModel cellSetModel = new CellSetModel();
    cellSetModel.addRow(rowModel);
    Response response = client.put(url, Constants.MIMETYPE_PROTOBUF, cellSetModel.createProtobufOutput());
    Thread.yield();
    return response;
}
Also used : Response(org.apache.hadoop.hbase.rest.client.Response) CellSetModel(org.apache.hadoop.hbase.rest.model.CellSetModel) RowModel(org.apache.hadoop.hbase.rest.model.RowModel) CellModel(org.apache.hadoop.hbase.rest.model.CellModel)

Example 27 with CellModel

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

the class RowResourceBase method checkAndPutValueXML.

protected static Response checkAndPutValueXML(String url, String table, String row, String column, String valueToCheck, String valueToPut, HashMap<String, String> otherCells) throws IOException, JAXBException {
    RowModel rowModel = new RowModel(row);
    rowModel.addCell(new CellModel(Bytes.toBytes(column), Bytes.toBytes(valueToPut)));
    if (otherCells != null) {
        for (Map.Entry<String, String> entry : otherCells.entrySet()) {
            rowModel.addCell(new CellModel(Bytes.toBytes(entry.getKey()), Bytes.toBytes(entry.getValue())));
        }
    }
    // This Cell need to be added as last cell.
    rowModel.addCell(new CellModel(Bytes.toBytes(column), Bytes.toBytes(valueToCheck)));
    CellSetModel cellSetModel = new CellSetModel();
    cellSetModel.addRow(rowModel);
    StringWriter writer = new StringWriter();
    xmlMarshaller.marshal(cellSetModel, writer);
    Response response = client.put(url, Constants.MIMETYPE_XML, Bytes.toBytes(writer.toString()));
    Thread.yield();
    return response;
}
Also used : Response(org.apache.hadoop.hbase.rest.client.Response) CellSetModel(org.apache.hadoop.hbase.rest.model.CellSetModel) StringWriter(java.io.StringWriter) RowModel(org.apache.hadoop.hbase.rest.model.RowModel) CellModel(org.apache.hadoop.hbase.rest.model.CellModel)

Example 28 with CellModel

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

the class RowResourceBase method checkAndDeleteValuePB.

protected static Response checkAndDeleteValuePB(String url, String table, String row, String column, String valueToCheck, HashMap<String, String> cellsToDelete) throws IOException {
    RowModel rowModel = new RowModel(row);
    if (cellsToDelete != null) {
        for (Map.Entry<String, String> entry : cellsToDelete.entrySet()) {
            rowModel.addCell(new CellModel(Bytes.toBytes(entry.getKey()), Bytes.toBytes(entry.getValue())));
        }
    }
    // Add this at the end
    rowModel.addCell(new CellModel(Bytes.toBytes(column), Bytes.toBytes(valueToCheck)));
    CellSetModel cellSetModel = new CellSetModel();
    cellSetModel.addRow(rowModel);
    Response response = client.put(url, Constants.MIMETYPE_PROTOBUF, cellSetModel.createProtobufOutput());
    Thread.yield();
    return response;
}
Also used : Response(org.apache.hadoop.hbase.rest.client.Response) CellSetModel(org.apache.hadoop.hbase.rest.model.CellSetModel) RowModel(org.apache.hadoop.hbase.rest.model.RowModel) CellModel(org.apache.hadoop.hbase.rest.model.CellModel)

Example 29 with CellModel

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

the class RemoteHTable method buildResultFromModel.

protected Result[] buildResultFromModel(final CellSetModel model) {
    List<Result> results = new ArrayList<>();
    for (RowModel row : model.getRows()) {
        List<Cell> kvs = new ArrayList<>(row.getCells().size());
        for (CellModel cell : row.getCells()) {
            byte[][] split = KeyValue.parseColumn(cell.getColumn());
            byte[] column = split[0];
            byte[] qualifier = null;
            if (split.length == 1) {
                qualifier = HConstants.EMPTY_BYTE_ARRAY;
            } else if (split.length == 2) {
                qualifier = split[1];
            } else {
                throw new IllegalArgumentException("Invalid familyAndQualifier provided.");
            }
            kvs.add(new KeyValue(row.getKey(), column, qualifier, cell.getTimestamp(), cell.getValue()));
        }
        results.add(Result.create(kvs));
    }
    return results.toArray(new Result[results.size()]);
}
Also used : KeyValue(org.apache.hadoop.hbase.KeyValue) ArrayList(java.util.ArrayList) 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 30 with CellModel

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

the class RemoteHTable method put.

@Override
public void put(List<Put> puts) throws IOException {
    // this is a trick: The gateway accepts multiple rows in a cell set and
    // ignores the row specification in the URI
    // separate puts by row
    TreeMap<byte[], List<Cell>> map = new TreeMap<>(Bytes.BYTES_COMPARATOR);
    for (Put put : puts) {
        byte[] row = put.getRow();
        List<Cell> cells = map.get(row);
        if (cells == null) {
            cells = new ArrayList<>();
            map.put(row, cells);
        }
        for (List<Cell> l : put.getFamilyCellMap().values()) {
            cells.addAll(l);
        }
    }
    // build the cell set
    CellSetModel model = new CellSetModel();
    for (Map.Entry<byte[], List<Cell>> e : map.entrySet()) {
        RowModel row = new RowModel(e.getKey());
        for (Cell cell : e.getValue()) {
            row.addCell(new CellModel(cell));
        }
        model.addRow(row);
    }
    // build path for multiput
    StringBuilder sb = new StringBuilder();
    sb.append('/');
    sb.append(Bytes.toString(name));
    // can be any nonexistent row
    sb.append("/$multiput");
    for (int i = 0; i < maxRetries; i++) {
        Response response = client.put(sb.toString(), Constants.MIMETYPE_PROTOBUF, model.createProtobufOutput());
        int code = response.getCode();
        switch(code) {
            case 200:
                return;
            case 509:
                try {
                    Thread.sleep(sleepTime);
                } catch (InterruptedException e) {
                    throw (InterruptedIOException) new InterruptedIOException().initCause(e);
                }
                break;
            default:
                throw new IOException("multiput request failed with " + code);
        }
    }
    throw new IOException("multiput request timed out");
}
Also used : InterruptedIOException(java.io.InterruptedIOException) CellSetModel(org.apache.hadoop.hbase.rest.model.CellSetModel) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) TreeMap(java.util.TreeMap) Put(org.apache.hadoop.hbase.client.Put) List(java.util.List) ArrayList(java.util.ArrayList) RowModel(org.apache.hadoop.hbase.rest.model.RowModel) CellModel(org.apache.hadoop.hbase.rest.model.CellModel) Cell(org.apache.hadoop.hbase.Cell) Map(java.util.Map) TreeMap(java.util.TreeMap)

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