Search in sources :

Example 1 with TableSchemaModel

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

the class RemoteAdmin method createTable.

/**
   * Creates a new table.
   * @param desc table descriptor for table
   * @throws IOException if a remote or network exception occurs
   */
public void createTable(HTableDescriptor desc) throws IOException {
    TableSchemaModel model = new TableSchemaModel(desc);
    StringBuilder path = new StringBuilder();
    path.append('/');
    if (accessToken != null) {
        path.append(accessToken);
        path.append('/');
    }
    path.append(desc.getTableName());
    path.append('/');
    path.append("schema");
    int code = 0;
    for (int i = 0; i < maxRetries; i++) {
        Response response = client.put(path.toString(), Constants.MIMETYPE_PROTOBUF, model.createProtobufOutput());
        code = response.getCode();
        switch(code) {
            case 201:
                return;
            case 509:
                try {
                    Thread.sleep(sleepTime);
                } catch (InterruptedException e) {
                    throw (InterruptedIOException) new InterruptedIOException().initCause(e);
                }
                break;
            default:
                throw new IOException("create request to " + path.toString() + " returned " + code);
        }
    }
    throw new IOException("create request to " + path.toString() + " timed out");
}
Also used : InterruptedIOException(java.io.InterruptedIOException) TableSchemaModel(org.apache.hadoop.hbase.rest.model.TableSchemaModel) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException)

Example 2 with TableSchemaModel

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

the class TestSchemaResource method testTableCreateAndDeletePB.

@Test
public void testTableCreateAndDeletePB() throws IOException, JAXBException {
    String schemaPath = "/" + TABLE2 + "/schema";
    TableSchemaModel model;
    Response response;
    Admin admin = TEST_UTIL.getAdmin();
    assertFalse(admin.tableExists(TableName.valueOf(TABLE2)));
    // create the table
    model = testTableSchemaModel.buildTestModel(TABLE2);
    testTableSchemaModel.checkModel(model, TABLE2);
    if (csrfEnabled) {
        // test put operation is forbidden without custom header
        response = client.put(schemaPath, Constants.MIMETYPE_PROTOBUF, model.createProtobufOutput());
        assertEquals(response.getCode(), 400);
    }
    response = client.put(schemaPath, Constants.MIMETYPE_PROTOBUF, model.createProtobufOutput(), extraHdr);
    assertEquals(response.getCode(), 201);
    // recall the same put operation but in read-only mode
    conf.set("hbase.rest.readonly", "true");
    response = client.put(schemaPath, Constants.MIMETYPE_PROTOBUF, model.createProtobufOutput(), extraHdr);
    assertNotNull(extraHdr);
    assertEquals(response.getCode(), 403);
    // retrieve the schema and validate it
    response = client.get(schemaPath, Constants.MIMETYPE_PROTOBUF);
    assertEquals(response.getCode(), 200);
    assertEquals(Constants.MIMETYPE_PROTOBUF, response.getHeader("content-type"));
    model = new TableSchemaModel();
    model.getObjectFromMessage(response.getBody());
    testTableSchemaModel.checkModel(model, TABLE2);
    // retrieve the schema and validate it with alternate pbuf type
    response = client.get(schemaPath, Constants.MIMETYPE_PROTOBUF_IETF);
    assertEquals(response.getCode(), 200);
    assertEquals(Constants.MIMETYPE_PROTOBUF_IETF, response.getHeader("content-type"));
    model = new TableSchemaModel();
    model.getObjectFromMessage(response.getBody());
    testTableSchemaModel.checkModel(model, TABLE2);
    if (csrfEnabled) {
        // test delete schema operation is forbidden without custom header
        response = client.delete(schemaPath);
        assertEquals(400, response.getCode());
    }
    // test delete schema operation is forbidden in read-only mode
    response = client.delete(schemaPath, extraHdr);
    assertEquals(response.getCode(), 403);
    // return read-only setting back to default
    conf.set("hbase.rest.readonly", "false");
    // delete the table and make sure HBase concurs
    response = client.delete(schemaPath, extraHdr);
    assertEquals(response.getCode(), 200);
    assertFalse(admin.tableExists(TableName.valueOf(TABLE2)));
}
Also used : Response(org.apache.hadoop.hbase.rest.client.Response) TestTableSchemaModel(org.apache.hadoop.hbase.rest.model.TestTableSchemaModel) TableSchemaModel(org.apache.hadoop.hbase.rest.model.TableSchemaModel) Admin(org.apache.hadoop.hbase.client.Admin) Test(org.junit.Test)

Example 3 with TableSchemaModel

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

the class SchemaResource method get.

@GET
@Produces({ MIMETYPE_TEXT, 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);
    try {
        ResponseBuilder response = Response.ok(new TableSchemaModel(getTableSchema()));
        response.cacheControl(cacheControl);
        servlet.getMetrics().incrementSucessfulGetRequests(1);
        return response.build();
    } catch (Exception e) {
        servlet.getMetrics().incrementFailedGetRequests(1);
        return processException(e);
    }
}
Also used : TableSchemaModel(org.apache.hadoop.hbase.rest.model.TableSchemaModel) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) TableNotFoundException(org.apache.hadoop.hbase.TableNotFoundException) TableNotEnabledException(org.apache.hadoop.hbase.TableNotEnabledException) TableExistsException(org.apache.hadoop.hbase.TableExistsException) IOException(java.io.IOException) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 4 with TableSchemaModel

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

the class RemoteHTable method getTableDescriptor.

@Override
public HTableDescriptor getTableDescriptor() throws IOException {
    StringBuilder sb = new StringBuilder();
    sb.append('/');
    sb.append(Bytes.toString(name));
    sb.append('/');
    sb.append("schema");
    for (int i = 0; i < maxRetries; i++) {
        Response response = client.get(sb.toString(), Constants.MIMETYPE_PROTOBUF);
        int code = response.getCode();
        switch(code) {
            case 200:
                TableSchemaModel schema = new TableSchemaModel();
                schema.getObjectFromMessage(response.getBody());
                return schema.getTableDescriptor();
            case 509:
                try {
                    Thread.sleep(sleepTime);
                } catch (InterruptedException e) {
                    throw (InterruptedIOException) new InterruptedIOException().initCause(e);
                }
                break;
            default:
                throw new IOException("schema request returned " + code);
        }
    }
    throw new IOException("schema request timed out");
}
Also used : InterruptedIOException(java.io.InterruptedIOException) TableSchemaModel(org.apache.hadoop.hbase.rest.model.TableSchemaModel) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException)

Example 5 with TableSchemaModel

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

the class TestSchemaResource method testTableCreateAndDeleteXML.

@Test
public void testTableCreateAndDeleteXML() throws IOException, JAXBException {
    String schemaPath = "/" + TABLE1 + "/schema";
    TableSchemaModel model;
    Response response;
    Admin admin = TEST_UTIL.getAdmin();
    assertFalse(admin.tableExists(TableName.valueOf(TABLE1)));
    // create the table
    model = testTableSchemaModel.buildTestModel(TABLE1);
    testTableSchemaModel.checkModel(model, TABLE1);
    if (csrfEnabled) {
        // test put operation is forbidden without custom header
        response = client.put(schemaPath, Constants.MIMETYPE_XML, toXML(model));
        assertEquals(response.getCode(), 400);
    }
    response = client.put(schemaPath, Constants.MIMETYPE_XML, toXML(model), extraHdr);
    assertEquals(response.getCode(), 201);
    // recall the same put operation but in read-only mode
    conf.set("hbase.rest.readonly", "true");
    response = client.put(schemaPath, Constants.MIMETYPE_XML, toXML(model), extraHdr);
    assertEquals(response.getCode(), 403);
    // retrieve the schema and validate it
    response = client.get(schemaPath, Constants.MIMETYPE_XML);
    assertEquals(response.getCode(), 200);
    assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type"));
    model = fromXML(response.getBody());
    testTableSchemaModel.checkModel(model, TABLE1);
    // with json retrieve the schema and validate it
    response = client.get(schemaPath, Constants.MIMETYPE_JSON);
    assertEquals(response.getCode(), 200);
    assertEquals(Constants.MIMETYPE_JSON, response.getHeader("content-type"));
    model = testTableSchemaModel.fromJSON(Bytes.toString(response.getBody()));
    testTableSchemaModel.checkModel(model, TABLE1);
    if (csrfEnabled) {
        // test delete schema operation is forbidden without custom header
        response = client.delete(schemaPath);
        assertEquals(400, response.getCode());
    }
    // test delete schema operation is forbidden in read-only mode
    response = client.delete(schemaPath, extraHdr);
    assertEquals(response.getCode(), 403);
    // return read-only setting back to default
    conf.set("hbase.rest.readonly", "false");
    // delete the table and make sure HBase concurs
    response = client.delete(schemaPath, extraHdr);
    assertEquals(response.getCode(), 200);
    assertFalse(admin.tableExists(TableName.valueOf(TABLE1)));
}
Also used : Response(org.apache.hadoop.hbase.rest.client.Response) TestTableSchemaModel(org.apache.hadoop.hbase.rest.model.TestTableSchemaModel) TableSchemaModel(org.apache.hadoop.hbase.rest.model.TableSchemaModel) Admin(org.apache.hadoop.hbase.client.Admin) Test(org.junit.Test)

Aggregations

TableSchemaModel (org.apache.hadoop.hbase.rest.model.TableSchemaModel)5 IOException (java.io.IOException)3 InterruptedIOException (java.io.InterruptedIOException)2 Admin (org.apache.hadoop.hbase.client.Admin)2 Response (org.apache.hadoop.hbase.rest.client.Response)2 TestTableSchemaModel (org.apache.hadoop.hbase.rest.model.TestTableSchemaModel)2 Test (org.junit.Test)2 GET (javax.ws.rs.GET)1 Produces (javax.ws.rs.Produces)1 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)1 TableExistsException (org.apache.hadoop.hbase.TableExistsException)1 TableNotEnabledException (org.apache.hadoop.hbase.TableNotEnabledException)1 TableNotFoundException (org.apache.hadoop.hbase.TableNotFoundException)1