@GET @Produces({MIMETYPE_TEXT, MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF, MIMETYPE_PROTOBUF_IETF}) public Response get(final @Context UriInfo uriInfo) { if (LOG.isDebugEnabled()) { LOG.debug("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); } }
private Response update(final TableSchemaModel model, final boolean replace, final UriInfo uriInfo) { try { byte[] name = Bytes.toBytes(tableResource.getName()); HBaseAdmin admin = servlet.getAdmin(); if (replace || !admin.tableExists(name)) { return replace(name, model, uriInfo, admin); } else { return update(name, model, uriInfo, admin); } } catch (IOException e) { servlet.getMetrics().incrementFailedPutRequests(1); return Response.status(Response.Status.SERVICE_UNAVAILABLE) .type(MIMETYPE_TEXT).entity("Unavailable" + CRLF) .build(); } }
public HTableDescriptor getTableDescriptor() throws IOException { StringBuilder sb = new StringBuilder(); sb.append('/'); sb.append(Bytes.toStringBinary(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) { } break; default: throw new IOException("schema request returned " + code); } } throw new IOException("schema request timed out"); }
public TestTableSchemaModel() throws Exception { super(TableSchemaModel.class); testColumnSchemaModel = new TestColumnSchemaModel(); AS_XML = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" + "<TableSchema name=\"testTable\" IS_META=\"false\" IS_ROOT=\"false\" READONLY=\"false\">" + "<ColumnSchema name=\"testcolumn\" BLOCKSIZE=\"16384\" BLOOMFILTER=\"NONE\" " + "BLOCKCACHE=\"true\" COMPRESSION=\"GZ\" VERSIONS=\"1\" TTL=\"86400\" IN_MEMORY=\"false\"/>" + "</TableSchema>"; AS_PB = "Cgl0ZXN0VGFibGUSEAoHSVNfTUVUQRIFZmFsc2USEAoHSVNfUk9PVBIFZmFsc2USEQoIUkVBRE9O" + "TFkSBWZhbHNlGpcBCgp0ZXN0Y29sdW1uEhIKCUJMT0NLU0laRRIFMTYzODQSEwoLQkxPT01GSUxU" + "RVISBE5PTkUSEgoKQkxPQ0tDQUNIRRIEdHJ1ZRIRCgtDT01QUkVTU0lPThICR1oSDQoIVkVSU0lP" + "TlMSATESDAoDVFRMEgU4NjQwMBISCglJTl9NRU1PUlkSBWZhbHNlGICjBSABKgJHWigA"; AS_JSON = "{\"name\":\"testTable\",\"IS_META\":\"false\",\"IS_ROOT\":\"false\"," + "\"READONLY\":\"false\",\"ColumnSchema\":[{\"name\":\"testcolumn\"," + "\"BLOCKSIZE\":\"16384\",\"BLOOMFILTER\":\"NONE\",\"BLOCKCACHE\":\"true\"," + "\"COMPRESSION\":\"GZ\",\"VERSIONS\":\"1\",\"TTL\":\"86400\",\"IN_MEMORY\":\"false\"}]}"; }
@GET @Produces({MIMETYPE_TEXT, MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF, MIMETYPE_PROTOBUF_IETF}) public Response get(final @Context 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); } }
private Response update(final TableSchemaModel model, final boolean replace, final UriInfo uriInfo) { try { TableName name = TableName.valueOf(tableResource.getName()); Admin admin = servlet.getAdmin(); if (replace || !admin.tableExists(name)) { return replace(name, model, uriInfo, admin); } else { return update(name, model, uriInfo, admin); } } catch (Exception e) { servlet.getMetrics().incrementFailedPutRequests(1); return processException(e); } }
@PUT @Consumes({MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF, MIMETYPE_PROTOBUF_IETF}) public Response put(final TableSchemaModel model, final @Context UriInfo uriInfo) { if (LOG.isDebugEnabled()) { LOG.debug("PUT " + uriInfo.getAbsolutePath()); } servlet.getMetrics().incrementRequests(1); return update(model, true, uriInfo); }
@POST @Consumes({MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF, MIMETYPE_PROTOBUF_IETF}) public Response post(final TableSchemaModel model, final @Context UriInfo uriInfo) { if (LOG.isDebugEnabled()) { LOG.debug("PUT " + uriInfo.getAbsolutePath()); } servlet.getMetrics().incrementRequests(1); return update(model, false, uriInfo); }
@Override public HTableDescriptor getTableDescriptor() throws IOException { StringBuilder sb = new StringBuilder(); sb.append('/'); sb.append(Bytes.toStringBinary(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"); }
/** * 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"); }
@BeforeClass public static void setUpBeforeClass() throws Exception { conf = TEST_UTIL.getConfiguration(); TEST_UTIL.startMiniCluster(); REST_TEST_UTIL.startServletContainer(conf); client = new Client(new Cluster().add("localhost", REST_TEST_UTIL.getServletPort())); testTableSchemaModel = new TestTableSchemaModel(); context = JAXBContext.newInstance( ColumnSchemaModel.class, TableSchemaModel.class); }
/** * 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(Bytes.toStringBinary(desc.getName())); 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) { } break; default: throw new IOException("create request to " + path.toString() + " returned " + code); } } throw new IOException("create request to " + path.toString() + " timed out"); }
@BeforeClass public static void setUpBeforeClass() throws Exception { conf = TEST_UTIL.getConfiguration(); TEST_UTIL.startMiniCluster(); REST_TEST_UTIL.startServletContainer(conf); client = new Client(new Cluster().add("localhost", REST_TEST_UTIL.getServletPort())); context = JAXBContext.newInstance( ColumnSchemaModel.class, TableSchemaModel.class); }
@Test public void testTableCreateAndDeleteXML() throws IOException, JAXBException { String schemaPath = "/" + TABLE1 + "/schema"; TableSchemaModel model; Response response; HBaseAdmin admin = TEST_UTIL.getHBaseAdmin(); assertFalse(admin.tableExists(TABLE1)); // create the table model = TestTableSchemaModel.buildTestModel(TABLE1); TestTableSchemaModel.checkModel(model, TABLE1); response = client.put(schemaPath, Constants.MIMETYPE_XML, toXML(model)); 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)); 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); // test delete schema operation is forbidden in read-only mode response = client.delete(schemaPath); 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); assertEquals(response.getCode(), 200); assertFalse(admin.tableExists(TABLE1)); }
private Response update(final TableSchemaModel model, final boolean replace, final UriInfo uriInfo) { try { byte[] name = Bytes.toBytes(tableResource.getName()); HBaseAdmin admin = servlet.getAdmin(); if (replace || !admin.tableExists(name)) { return replace(name, model, uriInfo, admin); } else { return update(name, model, uriInfo, admin); } } catch (Exception e) { servlet.getMetrics().incrementFailedPutRequests(1); return processException(e); } }
/** * 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) { } break; default: throw new IOException("create request to " + path.toString() + " returned " + code); } } throw new IOException("create request to " + path.toString() + " timed out"); }
public TableSchemaModel buildTestModel(String name) { TableSchemaModel model = new TableSchemaModel(); model.setName(name); model.__setIsMeta(IS_META); model.__setIsRoot(IS_ROOT); model.__setReadOnly(READONLY); model.addColumnFamily(testColumnSchemaModel.buildTestModel()); return model; }