Search in sources :

Example 56 with CellSetModel

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

the class RemoteHTable method doCheckAndDelete.

private boolean doCheckAndDelete(byte[] row, byte[] family, byte[] qualifier, byte[] value, Delete delete) throws IOException {
    Put put = new Put(row, HConstants.LATEST_TIMESTAMP, delete.getFamilyCellMap());
    // column to check-the-value
    put.add(new KeyValue(row, family, qualifier, value));
    CellSetModel model = buildModelFromPut(put);
    StringBuilder sb = new StringBuilder();
    sb.append('/');
    sb.append(Bytes.toString(name));
    sb.append('/');
    sb.append(toURLEncodedBytes(row));
    sb.append("?check=delete");
    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 true;
            case // NOT-MODIFIED
            304:
                return false;
            case 509:
                try {
                    Thread.sleep(sleepTime);
                } catch (final InterruptedException e) {
                    throw (InterruptedIOException) new InterruptedIOException().initCause(e);
                }
                break;
            default:
                throw new IOException("checkAndDelete request failed with " + code);
        }
    }
    throw new IOException("checkAndDelete request timed out");
}
Also used : InterruptedIOException(java.io.InterruptedIOException) KeyValue(org.apache.hadoop.hbase.KeyValue) CellSetModel(org.apache.hadoop.hbase.rest.model.CellSetModel) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) Put(org.apache.hadoop.hbase.client.Put)

Example 57 with CellSetModel

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

the class RemoteHTable method doCheckAndPut.

private boolean doCheckAndPut(byte[] row, byte[] family, byte[] qualifier, byte[] value, Put put) throws IOException {
    // column to check-the-value
    put.add(new KeyValue(row, family, qualifier, value));
    CellSetModel model = buildModelFromPut(put);
    StringBuilder sb = new StringBuilder();
    sb.append('/');
    sb.append(Bytes.toString(name));
    sb.append('/');
    sb.append(toURLEncodedBytes(put.getRow()));
    sb.append("?check=put");
    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 true;
            case // NOT-MODIFIED
            304:
                return false;
            case 509:
                try {
                    Thread.sleep(sleepTime);
                } catch (final InterruptedException e) {
                    throw (InterruptedIOException) new InterruptedIOException().initCause(e);
                }
                break;
            default:
                throw new IOException("checkAndPut request failed with " + code);
        }
    }
    throw new IOException("checkAndPut request timed out");
}
Also used : InterruptedIOException(java.io.InterruptedIOException) KeyValue(org.apache.hadoop.hbase.KeyValue) CellSetModel(org.apache.hadoop.hbase.rest.model.CellSetModel) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException)

Example 58 with CellSetModel

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

the class RemoteHTable method getResults.

private Result[] getResults(String spec) throws IOException {
    for (int i = 0; i < maxRetries; i++) {
        Response response = client.get(spec, Constants.MIMETYPE_PROTOBUF);
        int code = response.getCode();
        switch(code) {
            case 200:
                CellSetModel model = new CellSetModel();
                model.getObjectFromMessage(response.getBody());
                Result[] results = buildResultFromModel(model);
                if (results.length > 0) {
                    return results;
                }
            // fall through
            case 404:
                return new Result[0];
            case 509:
                try {
                    Thread.sleep(sleepTime);
                } catch (InterruptedException e) {
                    throw (InterruptedIOException) new InterruptedIOException().initCause(e);
                }
                break;
            default:
                throw new IOException("get request returned " + code);
        }
    }
    throw new IOException("get 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) Result(org.apache.hadoop.hbase.client.Result) CheckAndMutateResult(org.apache.hadoop.hbase.client.CheckAndMutateResult)

Example 59 with CellSetModel

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

the class TestSecureRESTServer method testProxy.

public void testProxy(String extraArgs, String PRINCIPAL, File keytab, int responseCode) throws Exception {
    UserGroupInformation superuser = UserGroupInformation.loginUserFromKeytabAndReturnUGI(SERVICE_PRINCIPAL, serviceKeytab.getAbsolutePath());
    final TableName table = TableName.valueOf("publicTable");
    // Read that row as the client
    Pair<CloseableHttpClient, HttpClientContext> pair = getClient();
    CloseableHttpClient client = pair.getFirst();
    HttpClientContext context = pair.getSecond();
    HttpGet get = new HttpGet(new URL("http://localhost:" + REST_TEST.getServletPort()).toURI() + "/" + table + "/a" + extraArgs);
    get.addHeader("Accept", "application/json");
    UserGroupInformation user = UserGroupInformation.loginUserFromKeytabAndReturnUGI(PRINCIPAL, keytab.getAbsolutePath());
    String jsonResponse = user.doAs(new PrivilegedExceptionAction<String>() {

        @Override
        public String run() throws Exception {
            try (CloseableHttpResponse response = client.execute(get, context)) {
                final int statusCode = response.getStatusLine().getStatusCode();
                assertEquals(response.getStatusLine().toString(), responseCode, statusCode);
                HttpEntity entity = response.getEntity();
                return EntityUtils.toString(entity);
            }
        }
    });
    if (responseCode == HttpURLConnection.HTTP_OK) {
        ObjectMapper mapper = new JacksonJaxbJsonProvider().locateMapper(CellSetModel.class, MediaType.APPLICATION_JSON_TYPE);
        CellSetModel model = mapper.readValue(jsonResponse, CellSetModel.class);
        assertEquals(1, model.getRows().size());
        RowModel row = model.getRows().get(0);
        assertEquals("a", Bytes.toString(row.getKey()));
        assertEquals(1, row.getCells().size());
        CellModel cell = row.getCells().get(0);
        assertEquals("1", Bytes.toString(cell.getValue()));
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpEntity(org.apache.http.HttpEntity) CellSetModel(org.apache.hadoop.hbase.rest.model.CellSetModel) HttpGet(org.apache.http.client.methods.HttpGet) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) URL(java.net.URL) IOException(java.io.IOException) TableName(org.apache.hadoop.hbase.TableName) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) RowModel(org.apache.hadoop.hbase.rest.model.RowModel) CellModel(org.apache.hadoop.hbase.rest.model.CellModel) JacksonJaxbJsonProvider(org.apache.hbase.thirdparty.com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) UserGroupInformation(org.apache.hadoop.security.UserGroupInformation)

Example 60 with CellSetModel

use of org.apache.hadoop.hbase.rest.model.CellSetModel 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(Bytes.toBytes(this.columns[i]));
                }
            }
            ResultGenerator generator = ResultGenerator.fromRowSpec(this.tableResource.getName(), rowSpec, null, !params.containsKey(NOCACHE_PARAM_NAME));
            Cell value = null;
            RowModel rowModel = new RowModel(rowSpec.getRow());
            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(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