Example usage for org.apache.hadoop.fs InvalidRequestException InvalidRequestException

List of usage examples for org.apache.hadoop.fs InvalidRequestException InvalidRequestException

Introduction

In this page you can find the example usage for org.apache.hadoop.fs InvalidRequestException InvalidRequestException.

Prototype

public InvalidRequestException(String str) 

Source Link

Usage

From source file:org.wso2.carbon.ml.core.impl.MLModelHandler.java

License:Open Source License

/**
 * Publish a ML model to registry./*  w  w  w  . ja va 2s  . c o  m*/
 *
 * @param tenantId Unique ID of the tenant.
 * @param userName Username of the user.
 * @param modelId  Unique ID of the built ML model
 * @throws InvalidRequestException, MLModelPublisherException, MLModelHandlerException
 */
public String publishModel(int tenantId, String userName, long modelId, Format mode)
        throws InvalidRequestException, MLModelPublisherException, MLModelHandlerException,
        MLPmmlExportException {
    InputStream in = null;
    String errorMsg = "Failed to publish the model [id] " + modelId;
    RegistryOutputAdapter registryOutputAdapter = new RegistryOutputAdapter();
    String relativeRegistryPath = null;

    switch (mode) {
    case SERIALIZED:
        try {
            // read model
            MLStorage storage = databaseService.getModelStorage(modelId);
            if (storage == null) {
                throw new InvalidRequestException("Invalid model [id] " + modelId);
            }
            String storageType = storage.getType();
            String storageLocation = storage.getLocation();
            MLIOFactory ioFactory = new MLIOFactory(mlProperties);
            MLInputAdapter inputAdapter = ioFactory.getInputAdapter(storageType + MLConstants.IN_SUFFIX);
            in = inputAdapter.read(storageLocation);
            if (in == null) {
                throw new InvalidRequestException("Invalid model [id] " + modelId);
            }
            // create registry path
            MLCoreServiceValueHolder valueHolder = MLCoreServiceValueHolder.getInstance();
            String modelName = databaseService.getModel(tenantId, userName, modelId).getName();
            relativeRegistryPath = "/" + valueHolder.getModelRegistryLocation() + "/" + modelName;
            // publish to registry
            registryOutputAdapter.write(relativeRegistryPath, in);
        } catch (DatabaseHandlerException e) {
            throw new MLModelPublisherException(errorMsg, e);
        } catch (MLInputAdapterException e) {
            throw new MLModelPublisherException(errorMsg, e);
        } catch (MLOutputAdapterException e) {
            throw new MLModelPublisherException(errorMsg, e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    String msg = "Error in closing input stream while publishing model";
                    log.error(msg, e);
                }
            }
        }
        break;

    case PMML:
        MLCoreServiceValueHolder valueHolder = MLCoreServiceValueHolder.getInstance();
        try {
            String modelName = databaseService.getModel(tenantId, userName, modelId).getName();
            relativeRegistryPath = "/" + valueHolder.getModelRegistryLocation() + "/" + modelName + ".xml";

            MLModel model = retrieveModel(modelId);
            String pmmlModel = exportAsPMML(model);
            in = new ByteArrayInputStream(pmmlModel.getBytes(StandardCharsets.UTF_8));
            registryOutputAdapter.write(relativeRegistryPath, in);

        } catch (DatabaseHandlerException e) {
            throw new MLModelPublisherException(errorMsg, e);
        } catch (MLModelHandlerException e) {
            throw new MLModelHandlerException("Failed to retrieve the model [id] " + modelId, e);
        } catch (MLOutputAdapterException e) {
            throw new MLModelPublisherException(errorMsg, e);
        } catch (MLPmmlExportException e) {
            throw new MLPmmlExportException("PMML export not supported for model type");
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    String msg = "Error in closing input stream while publishing model";
                    log.error(msg, e);
                }
            }
        }
        break;

    default:
        throw new MLModelPublisherException(errorMsg);
    }

    return RegistryConstants.GOVERNANCE_REGISTRY_BASE_PATH + relativeRegistryPath;
}