Search in sources :

Example 1 with NamespacesInstanceModel

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

the class TestNamespacesInstanceResource method testNamespaceCreateAndDeleteXMLAndJSON.

@Test
public void testNamespaceCreateAndDeleteXMLAndJSON() throws IOException, JAXBException {
    String namespacePath1 = "/namespaces/" + NAMESPACE1;
    String namespacePath2 = "/namespaces/" + NAMESPACE2;
    NamespacesInstanceModel model1;
    NamespacesInstanceModel model2;
    Response response;
    // Check that namespaces don't exist via non-REST call.
    Admin admin = TEST_UTIL.getAdmin();
    assertNull(findNamespace(admin, NAMESPACE1));
    assertNull(findNamespace(admin, NAMESPACE2));
    model1 = testNamespacesInstanceModel.buildTestModel(NAMESPACE1, NAMESPACE1_PROPS);
    testNamespacesInstanceModel.checkModel(model1, NAMESPACE1, NAMESPACE1_PROPS);
    model2 = testNamespacesInstanceModel.buildTestModel(NAMESPACE2, NAMESPACE2_PROPS);
    testNamespacesInstanceModel.checkModel(model2, NAMESPACE2, NAMESPACE2_PROPS);
    // Test cannot PUT (alter) non-existent namespace.
    response = client.put(namespacePath1, Constants.MIMETYPE_XML, toXML(model1));
    assertEquals(403, response.getCode());
    String jsonString = jsonMapper.writeValueAsString(model2);
    response = client.put(namespacePath2, Constants.MIMETYPE_JSON, Bytes.toBytes(jsonString));
    assertEquals(403, response.getCode());
    // Test cannot create tables when in read only mode.
    conf.set("hbase.rest.readonly", "true");
    response = client.post(namespacePath1, Constants.MIMETYPE_XML, toXML(model1));
    assertEquals(403, response.getCode());
    jsonString = jsonMapper.writeValueAsString(model2);
    response = client.post(namespacePath2, Constants.MIMETYPE_JSON, Bytes.toBytes(jsonString));
    assertEquals(403, response.getCode());
    NamespaceDescriptor nd1 = findNamespace(admin, NAMESPACE1);
    NamespaceDescriptor nd2 = findNamespace(admin, NAMESPACE2);
    assertNull(nd1);
    assertNull(nd2);
    conf.set("hbase.rest.readonly", "false");
    // Create namespace via XML and JSON.
    response = client.post(namespacePath1, Constants.MIMETYPE_XML, toXML(model1));
    assertEquals(201, response.getCode());
    jsonString = jsonMapper.writeValueAsString(model2);
    response = client.post(namespacePath2, Constants.MIMETYPE_JSON, Bytes.toBytes(jsonString));
    assertEquals(201, response.getCode());
    // Check that created namespaces correctly.
    nd1 = findNamespace(admin, NAMESPACE1);
    nd2 = findNamespace(admin, NAMESPACE2);
    assertNotNull(nd1);
    assertNotNull(nd2);
    checkNamespaceProperties(nd1, NAMESPACE1_PROPS);
    checkNamespaceProperties(nd1, NAMESPACE1_PROPS);
    // Test cannot delete tables when in read only mode.
    conf.set("hbase.rest.readonly", "true");
    response = client.delete(namespacePath1);
    assertEquals(403, response.getCode());
    response = client.delete(namespacePath2);
    assertEquals(403, response.getCode());
    nd1 = findNamespace(admin, NAMESPACE1);
    nd2 = findNamespace(admin, NAMESPACE2);
    assertNotNull(nd1);
    assertNotNull(nd2);
    conf.set("hbase.rest.readonly", "false");
    // Delete namespaces via XML and JSON.
    response = client.delete(namespacePath1);
    assertEquals(200, response.getCode());
    response = client.delete(namespacePath2);
    assertEquals(200, response.getCode());
    nd1 = findNamespace(admin, NAMESPACE1);
    nd2 = findNamespace(admin, NAMESPACE2);
    assertNull(nd1);
    assertNull(nd2);
}
Also used : Response(org.apache.hadoop.hbase.rest.client.Response) TestNamespacesInstanceModel(org.apache.hadoop.hbase.rest.model.TestNamespacesInstanceModel) NamespacesInstanceModel(org.apache.hadoop.hbase.rest.model.NamespacesInstanceModel) NamespaceDescriptor(org.apache.hadoop.hbase.NamespaceDescriptor) Admin(org.apache.hadoop.hbase.client.Admin) Test(org.junit.Test)

Example 2 with NamespacesInstanceModel

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

the class NamespacesInstanceResource method get.

/**
   * Build a response for GET namespace description or GET list of namespace tables.
   * @param context servlet context
   * @param uriInfo (JAX-RS context variable) request URL
   * @return A response containing NamespacesInstanceModel for a namespace descriptions and
   * TableListModel for a list of namespace tables.
   */
@GET
@Produces({ MIMETYPE_TEXT, MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF, MIMETYPE_PROTOBUF_IETF })
public Response get(@Context final ServletContext context, @Context final UriInfo uriInfo) {
    if (LOG.isTraceEnabled()) {
        LOG.trace("GET " + uriInfo.getAbsolutePath());
    }
    servlet.getMetrics().incrementRequests(1);
    // Respond to list of namespace tables requests.
    if (queryTables) {
        TableListModel tableModel = new TableListModel();
        try {
            HTableDescriptor[] tables = servlet.getAdmin().listTableDescriptorsByNamespace(namespace);
            for (int i = 0; i < tables.length; i++) {
                tableModel.add(new TableModel(tables[i].getTableName().getQualifierAsString()));
            }
            servlet.getMetrics().incrementSucessfulGetRequests(1);
            return Response.ok(tableModel).build();
        } catch (IOException e) {
            servlet.getMetrics().incrementFailedGetRequests(1);
            throw new RuntimeException("Cannot retrieve table list for '" + namespace + "'.");
        }
    }
    // Respond to namespace description requests.
    try {
        NamespacesInstanceModel rowModel = new NamespacesInstanceModel(servlet.getAdmin(), namespace);
        servlet.getMetrics().incrementSucessfulGetRequests(1);
        return Response.ok(rowModel).build();
    } catch (IOException e) {
        servlet.getMetrics().incrementFailedGetRequests(1);
        throw new RuntimeException("Cannot retrieve info for '" + namespace + "'.");
    }
}
Also used : TableListModel(org.apache.hadoop.hbase.rest.model.TableListModel) NamespacesInstanceModel(org.apache.hadoop.hbase.rest.model.NamespacesInstanceModel) IOException(java.io.IOException) TableModel(org.apache.hadoop.hbase.rest.model.TableModel) HTableDescriptor(org.apache.hadoop.hbase.HTableDescriptor) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 3 with NamespacesInstanceModel

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

the class TestNamespacesInstanceResource method testInvalidNamespacePostsAndPuts.

@Test
public void testInvalidNamespacePostsAndPuts() throws IOException, JAXBException {
    String namespacePath1 = "/namespaces/" + NAMESPACE1;
    String namespacePath2 = "/namespaces/" + NAMESPACE2;
    String namespacePath3 = "/namespaces/" + NAMESPACE3;
    NamespacesInstanceModel model1;
    NamespacesInstanceModel model2;
    NamespacesInstanceModel model3;
    Response response;
    // Check that namespaces don't exist via non-REST call.
    Admin admin = TEST_UTIL.getAdmin();
    assertNull(findNamespace(admin, NAMESPACE1));
    assertNull(findNamespace(admin, NAMESPACE2));
    assertNull(findNamespace(admin, NAMESPACE3));
    model1 = testNamespacesInstanceModel.buildTestModel(NAMESPACE1, NAMESPACE1_PROPS);
    testNamespacesInstanceModel.checkModel(model1, NAMESPACE1, NAMESPACE1_PROPS);
    model2 = testNamespacesInstanceModel.buildTestModel(NAMESPACE2, NAMESPACE2_PROPS);
    testNamespacesInstanceModel.checkModel(model2, NAMESPACE2, NAMESPACE2_PROPS);
    model3 = testNamespacesInstanceModel.buildTestModel(NAMESPACE3, NAMESPACE3_PROPS);
    testNamespacesInstanceModel.checkModel(model3, NAMESPACE3, NAMESPACE3_PROPS);
    // Try REST post and puts with invalid content.
    response = client.post(namespacePath1, Constants.MIMETYPE_JSON, toXML(model1));
    assertEquals(400, response.getCode());
    String jsonString = jsonMapper.writeValueAsString(model2);
    response = client.put(namespacePath2, Constants.MIMETYPE_XML, Bytes.toBytes(jsonString));
    assertEquals(400, response.getCode());
    response = client.post(namespacePath3, Constants.MIMETYPE_PROTOBUF, toXML(model1));
    assertEquals(500, response.getCode());
    NamespaceDescriptor nd1 = findNamespace(admin, NAMESPACE1);
    NamespaceDescriptor nd2 = findNamespace(admin, NAMESPACE2);
    NamespaceDescriptor nd3 = findNamespace(admin, NAMESPACE3);
    assertNull(nd1);
    assertNull(nd2);
    assertNull(nd3);
}
Also used : Response(org.apache.hadoop.hbase.rest.client.Response) TestNamespacesInstanceModel(org.apache.hadoop.hbase.rest.model.TestNamespacesInstanceModel) NamespacesInstanceModel(org.apache.hadoop.hbase.rest.model.NamespacesInstanceModel) NamespaceDescriptor(org.apache.hadoop.hbase.NamespaceDescriptor) Admin(org.apache.hadoop.hbase.client.Admin) Test(org.junit.Test)

Example 4 with NamespacesInstanceModel

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

the class TestNamespacesInstanceResource method testGetNamespaceTablesAndCannotDeleteNamespace.

@Test
public void testGetNamespaceTablesAndCannotDeleteNamespace() throws IOException, JAXBException {
    Admin admin = TEST_UTIL.getAdmin();
    String nsName = "TestNamespacesInstanceResource5";
    Response response;
    // Create namespace via admin.
    NamespaceDescriptor.Builder nsBuilder = NamespaceDescriptor.create(nsName);
    NamespaceDescriptor nsd = nsBuilder.build();
    nsd.setConfiguration("key1", "value1");
    admin.createNamespace(nsd);
    // Create two tables via admin.
    HColumnDescriptor colDesc = new HColumnDescriptor("cf1");
    TableName tn1 = TableName.valueOf(nsName + ":table1");
    HTableDescriptor table = new HTableDescriptor(tn1);
    table.addFamily(colDesc);
    admin.createTable(table);
    TableName tn2 = TableName.valueOf(nsName + ":table2");
    table = new HTableDescriptor(tn2);
    table.addFamily(colDesc);
    admin.createTable(table);
    Map<String, String> nsProperties = new HashMap<>();
    nsProperties.put("key1", "value1");
    List<String> nsTables = Arrays.asList("table1", "table2");
    // Check get namespace properties as XML, JSON and Protobuf.
    String namespacePath = "/namespaces/" + nsName;
    response = client.get(namespacePath);
    assertEquals(200, response.getCode());
    response = client.get(namespacePath, Constants.MIMETYPE_XML);
    assertEquals(200, response.getCode());
    NamespacesInstanceModel model = fromXML(response.getBody());
    checkNamespaceProperties(model.getProperties(), nsProperties);
    response = client.get(namespacePath, Constants.MIMETYPE_JSON);
    assertEquals(200, response.getCode());
    model = jsonMapper.readValue(response.getBody(), NamespacesInstanceModel.class);
    checkNamespaceProperties(model.getProperties(), nsProperties);
    response = client.get(namespacePath, Constants.MIMETYPE_PROTOBUF);
    assertEquals(200, response.getCode());
    model.getObjectFromMessage(response.getBody());
    checkNamespaceProperties(model.getProperties(), nsProperties);
    // Check get namespace tables as XML, JSON and Protobuf.
    namespacePath = "/namespaces/" + nsName + "/tables";
    response = client.get(namespacePath);
    assertEquals(200, response.getCode());
    response = client.get(namespacePath, Constants.MIMETYPE_XML);
    assertEquals(200, response.getCode());
    TableListModel tablemodel = fromXML(response.getBody());
    checkNamespaceTables(tablemodel.getTables(), nsTables);
    response = client.get(namespacePath, Constants.MIMETYPE_JSON);
    assertEquals(200, response.getCode());
    tablemodel = jsonMapper.readValue(response.getBody(), TableListModel.class);
    checkNamespaceTables(tablemodel.getTables(), nsTables);
    response = client.get(namespacePath, Constants.MIMETYPE_PROTOBUF);
    assertEquals(200, response.getCode());
    tablemodel.setTables(new ArrayList<>());
    tablemodel.getObjectFromMessage(response.getBody());
    checkNamespaceTables(tablemodel.getTables(), nsTables);
    // Check cannot delete namespace via REST because it contains tables.
    response = client.delete(namespacePath);
    namespacePath = "/namespaces/" + nsName;
    assertEquals(503, response.getCode());
}
Also used : TableListModel(org.apache.hadoop.hbase.rest.model.TableListModel) HColumnDescriptor(org.apache.hadoop.hbase.HColumnDescriptor) HashMap(java.util.HashMap) Admin(org.apache.hadoop.hbase.client.Admin) HTableDescriptor(org.apache.hadoop.hbase.HTableDescriptor) Response(org.apache.hadoop.hbase.rest.client.Response) TableName(org.apache.hadoop.hbase.TableName) TestNamespacesInstanceModel(org.apache.hadoop.hbase.rest.model.TestNamespacesInstanceModel) NamespacesInstanceModel(org.apache.hadoop.hbase.rest.model.NamespacesInstanceModel) NamespaceDescriptor(org.apache.hadoop.hbase.NamespaceDescriptor) Test(org.junit.Test)

Example 5 with NamespacesInstanceModel

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

the class TestNamespacesInstanceResource method testNamespaceCreateAndDeletePBAndNoBody.

@Test
public void testNamespaceCreateAndDeletePBAndNoBody() throws IOException, JAXBException {
    String namespacePath3 = "/namespaces/" + NAMESPACE3;
    String namespacePath4 = "/namespaces/" + NAMESPACE4;
    NamespacesInstanceModel model3;
    NamespacesInstanceModel model4;
    Response response;
    // Check that namespaces don't exist via non-REST call.
    Admin admin = TEST_UTIL.getAdmin();
    assertNull(findNamespace(admin, NAMESPACE3));
    assertNull(findNamespace(admin, NAMESPACE4));
    model3 = testNamespacesInstanceModel.buildTestModel(NAMESPACE3, NAMESPACE3_PROPS);
    testNamespacesInstanceModel.checkModel(model3, NAMESPACE3, NAMESPACE3_PROPS);
    model4 = testNamespacesInstanceModel.buildTestModel(NAMESPACE4, NAMESPACE4_PROPS);
    testNamespacesInstanceModel.checkModel(model4, NAMESPACE4, NAMESPACE4_PROPS);
    // Test cannot PUT (alter) non-existent namespace.
    response = client.put(namespacePath3, Constants.MIMETYPE_BINARY, new byte[] {});
    assertEquals(403, response.getCode());
    response = client.put(namespacePath4, Constants.MIMETYPE_PROTOBUF, model4.createProtobufOutput());
    assertEquals(403, response.getCode());
    // Test cannot create tables when in read only mode.
    conf.set("hbase.rest.readonly", "true");
    response = client.post(namespacePath3, Constants.MIMETYPE_BINARY, new byte[] {});
    assertEquals(403, response.getCode());
    response = client.put(namespacePath4, Constants.MIMETYPE_PROTOBUF, model4.createProtobufOutput());
    assertEquals(403, response.getCode());
    NamespaceDescriptor nd3 = findNamespace(admin, NAMESPACE3);
    NamespaceDescriptor nd4 = findNamespace(admin, NAMESPACE4);
    assertNull(nd3);
    assertNull(nd4);
    conf.set("hbase.rest.readonly", "false");
    // Create namespace via no body and protobuf.
    response = client.post(namespacePath3, Constants.MIMETYPE_BINARY, new byte[] {});
    assertEquals(201, response.getCode());
    response = client.post(namespacePath4, Constants.MIMETYPE_PROTOBUF, model4.createProtobufOutput());
    assertEquals(201, response.getCode());
    // Check that created namespaces correctly.
    nd3 = findNamespace(admin, NAMESPACE3);
    nd4 = findNamespace(admin, NAMESPACE4);
    assertNotNull(nd3);
    assertNotNull(nd4);
    checkNamespaceProperties(nd3, new HashMap<>());
    checkNamespaceProperties(nd4, NAMESPACE4_PROPS);
    // Check cannot post tables that already exist.
    response = client.post(namespacePath3, Constants.MIMETYPE_BINARY, new byte[] {});
    assertEquals(403, response.getCode());
    response = client.post(namespacePath4, Constants.MIMETYPE_PROTOBUF, model4.createProtobufOutput());
    assertEquals(403, response.getCode());
    // Check cannot post tables when in read only mode.
    conf.set("hbase.rest.readonly", "true");
    response = client.delete(namespacePath3);
    assertEquals(403, response.getCode());
    response = client.delete(namespacePath4);
    assertEquals(403, response.getCode());
    nd3 = findNamespace(admin, NAMESPACE3);
    nd4 = findNamespace(admin, NAMESPACE4);
    assertNotNull(nd3);
    assertNotNull(nd4);
    conf.set("hbase.rest.readonly", "false");
    // Delete namespaces via XML and JSON.
    response = client.delete(namespacePath3);
    assertEquals(200, response.getCode());
    response = client.delete(namespacePath4);
    assertEquals(200, response.getCode());
    nd3 = findNamespace(admin, NAMESPACE3);
    nd4 = findNamespace(admin, NAMESPACE4);
    assertNull(nd3);
    assertNull(nd4);
}
Also used : Response(org.apache.hadoop.hbase.rest.client.Response) TestNamespacesInstanceModel(org.apache.hadoop.hbase.rest.model.TestNamespacesInstanceModel) NamespacesInstanceModel(org.apache.hadoop.hbase.rest.model.NamespacesInstanceModel) NamespaceDescriptor(org.apache.hadoop.hbase.NamespaceDescriptor) Admin(org.apache.hadoop.hbase.client.Admin) Test(org.junit.Test)

Aggregations

NamespacesInstanceModel (org.apache.hadoop.hbase.rest.model.NamespacesInstanceModel)5 NamespaceDescriptor (org.apache.hadoop.hbase.NamespaceDescriptor)4 Admin (org.apache.hadoop.hbase.client.Admin)4 Response (org.apache.hadoop.hbase.rest.client.Response)4 TestNamespacesInstanceModel (org.apache.hadoop.hbase.rest.model.TestNamespacesInstanceModel)4 Test (org.junit.Test)4 HTableDescriptor (org.apache.hadoop.hbase.HTableDescriptor)2 TableListModel (org.apache.hadoop.hbase.rest.model.TableListModel)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 GET (javax.ws.rs.GET)1 Produces (javax.ws.rs.Produces)1 HColumnDescriptor (org.apache.hadoop.hbase.HColumnDescriptor)1 TableName (org.apache.hadoop.hbase.TableName)1 TableModel (org.apache.hadoop.hbase.rest.model.TableModel)1