Search in sources :

Example 66 with StringRepresentation

use of org.restlet.representation.StringRepresentation in project pinot by linkedin.

the class PinotTableRestletResource method setTablestate.

@HttpVerb("get")
@Summary("Enable, disable or drop a table")
@Tags({ "table" })
@Paths({ "/tables/{tableName}", "/table/{tableName}/" })
private StringRepresentation setTablestate(@Parameter(name = "tableName", in = "path", description = "The name of the table for which to toggle its state", required = true) String tableName, @Parameter(name = "type", in = "query", description = "Type of table, Offline or Realtime", required = false) String type, @Parameter(name = "state", in = "query", description = "The desired table state, either enable or disable", required = true) String state) throws JSONException {
    JSONArray ret = new JSONArray();
    boolean tableExists = false;
    if ((type == null || TableType.OFFLINE.name().equalsIgnoreCase(type)) && _pinotHelixResourceManager.hasOfflineTable(tableName)) {
        String offlineTableName = TableNameBuilder.OFFLINE_TABLE_NAME_BUILDER.forTable(tableName);
        JSONObject offline = new JSONObject();
        tableExists = true;
        offline.put(TABLE_NAME, offlineTableName);
        offline.put(STATE, toggleTableState(offlineTableName, state).toJSON().toString());
        ret.put(offline);
    }
    if ((type == null || TableType.REALTIME.name().equalsIgnoreCase(type)) && _pinotHelixResourceManager.hasRealtimeTable(tableName)) {
        String realTimeTableName = TableNameBuilder.REALTIME_TABLE_NAME_BUILDER.forTable(tableName);
        JSONObject realTime = new JSONObject();
        tableExists = true;
        realTime.put(TABLE_NAME, realTimeTableName);
        realTime.put(STATE, toggleTableState(realTimeTableName, state).toJSON().toString());
        ret.put(realTime);
    }
    if (tableExists) {
        return new StringRepresentation(ret.toString());
    } else {
        setStatus(Status.CLIENT_ERROR_NOT_FOUND);
        return new StringRepresentation("Error: Table " + tableName + " not found.");
    }
}
Also used : JSONObject(org.json.JSONObject) StringRepresentation(org.restlet.representation.StringRepresentation) JSONArray(org.json.JSONArray) Summary(com.linkedin.pinot.common.restlet.swagger.Summary) HttpVerb(com.linkedin.pinot.common.restlet.swagger.HttpVerb) Paths(com.linkedin.pinot.common.restlet.swagger.Paths) Tags(com.linkedin.pinot.common.restlet.swagger.Tags)

Example 67 with StringRepresentation

use of org.restlet.representation.StringRepresentation in project pinot by linkedin.

the class PinotTableRestletResource method getAllTables.

@HttpVerb("get")
@Summary("Views all tables' configuration")
@Tags({ "table" })
@Paths({ "/tables", "/tables/" })
private Representation getAllTables() throws JSONException {
    JSONObject object = new JSONObject();
    JSONArray tableArray = new JSONArray();
    Set<String> tableNames = new TreeSet<String>();
    tableNames.addAll(_pinotHelixResourceManager.getAllUniquePinotRawTableNames());
    for (String pinotTableName : tableNames) {
        tableArray.put(pinotTableName);
    }
    object.put("tables", tableArray);
    return new StringRepresentation(object.toString(2));
}
Also used : JSONObject(org.json.JSONObject) StringRepresentation(org.restlet.representation.StringRepresentation) TreeSet(java.util.TreeSet) JSONArray(org.json.JSONArray) Summary(com.linkedin.pinot.common.restlet.swagger.Summary) HttpVerb(com.linkedin.pinot.common.restlet.swagger.HttpVerb) Paths(com.linkedin.pinot.common.restlet.swagger.Paths) Tags(com.linkedin.pinot.common.restlet.swagger.Tags)

Example 68 with StringRepresentation

use of org.restlet.representation.StringRepresentation in project pinot by linkedin.

the class PinotTableRestletResource method delete.

@Override
@Delete
public Representation delete() {
    StringRepresentation presentation = null;
    final String tableName = (String) getRequest().getAttributes().get(TABLE_NAME);
    final String type = getReference().getQueryAsForm().getValues(TABLE_TYPE);
    if (!deleteTable(tableName, type)) {
        String error = new String("Error: Table " + tableName + " not found.");
        LOGGER.error(error);
        setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
        return new StringRepresentation(error);
    }
    return presentation;
}
Also used : StringRepresentation(org.restlet.representation.StringRepresentation) Delete(org.restlet.resource.Delete)

Example 69 with StringRepresentation

use of org.restlet.representation.StringRepresentation in project pinot by linkedin.

the class PinotTableRestletResource method post.

@Override
@Post("json")
public Representation post(Representation entity) {
    AbstractTableConfig config = null;
    try {
        String jsonRequest = entity.getText();
        config = AbstractTableConfig.init(jsonRequest);
        try {
            addTable(config);
        } catch (Exception e) {
            LOGGER.error("Caught exception while adding table", e);
            ControllerRestApplication.getControllerMetrics().addMeteredGlobalValue(ControllerMeter.CONTROLLER_TABLE_ADD_ERROR, 1L);
            setStatus(Status.SERVER_ERROR_INTERNAL);
            return new StringRepresentation("Failed: " + e.getMessage());
        }
        return new StringRepresentation("Success");
    } catch (Exception e) {
        LOGGER.error("error reading/serializing requestJSON", e);
        setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
        return new StringRepresentation("Failed: " + e.getMessage());
    }
}
Also used : StringRepresentation(org.restlet.representation.StringRepresentation) AbstractTableConfig(com.linkedin.pinot.common.config.AbstractTableConfig) JsonProcessingException(org.codehaus.jackson.JsonProcessingException) JsonMappingException(org.codehaus.jackson.map.JsonMappingException) JSONException(org.json.JSONException) IOException(java.io.IOException) JsonParseException(org.codehaus.jackson.JsonParseException) Post(org.restlet.resource.Post)

Example 70 with StringRepresentation

use of org.restlet.representation.StringRepresentation in project pinot by linkedin.

the class PinotTableSchema method getTableSchema.

@HttpVerb("get")
@Summary("Gets schema for a table")
@Tags({ "schema", "table" })
@Paths({ "/tables/{tableName}/schema", "/tables/{tableName}/schema/" })
private Representation getTableSchema(@Parameter(name = "tableName", in = "path", description = "Table name for which to get the schema", required = true) String tableName) {
    if (_pinotHelixResourceManager.hasRealtimeTable(tableName)) {
        try {
            AbstractTableConfig config = _pinotHelixResourceManager.getTableConfig(tableName, TableType.REALTIME);
            return new StringRepresentation(_pinotHelixResourceManager.getSchema(config.getValidationConfig().getSchemaName()).getJSONSchema().toString());
        } catch (Exception e) {
            LOGGER.error("Caught exception while fetching schema for a realtime table : {} ", tableName, e);
            ControllerRestApplication.getControllerMetrics().addMeteredGlobalValue(ControllerMeter.CONTROLLER_TABLE_SCHEMA_GET_ERROR, 1L);
            setStatus(Status.SERVER_ERROR_INTERNAL);
            return PinotSegmentUploadRestletResource.exceptionToStringRepresentation(e);
        }
    } else {
        AbstractTableConfig config;
        try {
            config = _pinotHelixResourceManager.getTableConfig(tableName, TableType.OFFLINE);
            String schemaName = config.getValidationConfig().getSchemaName();
            Schema schema = null;
            if (schemaName != null && !schemaName.isEmpty()) {
                schema = _pinotHelixResourceManager.getSchema(schemaName);
            }
            if (schema == null) {
                setStatus(Status.CLIENT_ERROR_NOT_FOUND);
                StringRepresentation repr = new StringRepresentation("{\"error\": \"Schema " + schemaName + " not found\"");
                repr.setMediaType(MediaType.APPLICATION_JSON);
                return repr;
            }
            return new StringRepresentation(schema.getJSONSchema().toString());
        } catch (Exception e) {
            LOGGER.error("Caught exception while fetching schema for a offline table : {} ", tableName, e);
            ControllerRestApplication.getControllerMetrics().addMeteredGlobalValue(ControllerMeter.CONTROLLER_TABLE_SCHEMA_GET_ERROR, 1L);
            setStatus(Status.SERVER_ERROR_INTERNAL);
            return PinotSegmentUploadRestletResource.exceptionToStringRepresentation(e);
        }
    }
}
Also used : StringRepresentation(org.restlet.representation.StringRepresentation) Schema(com.linkedin.pinot.common.data.Schema) AbstractTableConfig(com.linkedin.pinot.common.config.AbstractTableConfig) IOException(java.io.IOException) Summary(com.linkedin.pinot.common.restlet.swagger.Summary) HttpVerb(com.linkedin.pinot.common.restlet.swagger.HttpVerb) Paths(com.linkedin.pinot.common.restlet.swagger.Paths) Tags(com.linkedin.pinot.common.restlet.swagger.Tags)

Aggregations

StringRepresentation (org.restlet.representation.StringRepresentation)130 HttpVerb (com.linkedin.pinot.common.restlet.swagger.HttpVerb)30 Paths (com.linkedin.pinot.common.restlet.swagger.Paths)30 Summary (com.linkedin.pinot.common.restlet.swagger.Summary)30 IOException (java.io.IOException)30 Tags (com.linkedin.pinot.common.restlet.swagger.Tags)29 ZkClient (org.apache.helix.manager.zk.ZkClient)29 JSONObject (org.json.JSONObject)23 JsonMappingException (org.codehaus.jackson.map.JsonMappingException)19 ZNRecord (org.apache.helix.ZNRecord)17 JsonGenerationException (org.codehaus.jackson.JsonGenerationException)17 JSONException (org.json.JSONException)17 Representation (org.restlet.representation.Representation)15 Responses (com.linkedin.pinot.common.restlet.swagger.Responses)14 HelixException (org.apache.helix.HelixException)12 JSONArray (org.json.JSONArray)12 Builder (org.apache.helix.PropertyKey.Builder)11 Get (org.restlet.resource.Get)10 Post (org.restlet.resource.Post)9 ResourceException (org.restlet.resource.ResourceException)9