bda-datastore/src/main/java/gr/ntua/ece/cslab/selis/bda/datastore/beans/Recipe.java
public static String saveRecipeForSlug(
        String slug, InputStream recipeInStream, String recipeName) 
            throws IOException, SystemConnectorException  {

        // Read the binary into a byte array in memory.
        byte[] recipeBytes;
        try {
            recipeBytes = IOUtils.toByteArray(recipeInStream);

            recipeInStream.close();
        } catch (IOException e) {
            e.printStackTrace();
            throw e;
        }

        // Calculate binary's md5 hash.
        String recipeHash = DigestUtils.md5Hex(recipeBytes);

        // Find binary's storage location.
        Configuration configuration = Configuration.getInstance();

        String recipeFilename = Paths.get(
            Recipe.getStorageForSlug(slug),
            FilenameUtils.getBaseName(recipeName)+"_"+recipeHash+"."+FilenameUtils.getExtension(recipeName)
        ).toString();

        if (configuration.execEngine.getRecipeStorageType().startsWith("hdfs")) {
            // Use HDFS storage for recipes.

            HDFSConnector connector = (HDFSConnector )
                SystemConnector.getInstance().getHDFSConnector();

            org.apache.hadoop.fs.FileSystem fs = connector.getFileSystem();

            // Create HDFS file path object.
            org.apache.hadoop.fs.Path outputFilePath = 
                new org.apache.hadoop.fs.Path(recipeFilename);

            // Write to HDFS.
            org.apache.hadoop.fs.FSDataOutputStream outputStream = fs.create(
                outputFilePath
            );

            try {
                outputStream.write(recipeBytes);
            } catch (IOException e) {
                e.printStackTrace();
                throw e;
            } finally {
                outputStream.close();
            }

            // Prepend `hdfs://` before returning recipe name.
            recipeFilename = "hdfs://" + recipeFilename;
        } else {
            // Use local storage for recipes.

            // Create file path object.
            File outputFile = new File(recipeFilename);
            OutputStream outputStream = new FileOutputStream(outputFile);

            // Write to local storage.
            try {
                IOUtils.write(recipeBytes, outputStream);
            } catch (IOException e) {
                e.printStackTrace();
                throw e;
            } finally {
                outputStream.close();
            }
        }

        return recipeFilename;
    }
/**
     * Reads from `recipeInStream` a recipe binary and stores it the configured storage backend.
     *
     * Concatenates the `recipeName` with the md5 hash of the binary to generate a unique filename.
     * Then uses `Recipe.getStorageForSlug()` to retrieve the location where it stores the binary.
     *
     * Supports HDFS and local storage backends.
     *
     * TODO: Tests.
     *
     * @param slug           The SCN's slug.
     * @param recipeInStream A `InputStream` from where we read the recipe binary.
     * @param recipeName     The recipe binary's name.
     * @return               A `String` with the recipe binary's absolute path.
     * @throws IOException
     */
Reads from `recipeInStream` a recipe binary and stores it the configured storage backend.
Concatenates the `recipeName` with the md5 hash of the binary to generate a unique filename.
Then uses `Recipe.getStorageForSlug()` to retrieve the location where it stores the binary.

Supports HDFS and local storage backends.

Tests.

@param slug           The SCN's slug.
@param recipeInStream A `InputStream` from where we read the recipe binary.
@param recipeName     The recipe binary's name.
@return               A `String` with the recipe binary's absolute path.
Reads from ` recipeInStream ` a recipe binary and stores it the configured storage backend . Concatenates the ` recipeName ` with the md5 hash of the binary to generate a unique filename . Then uses ` Recipe . getStorageForSlug () ` to retrieve the location where it stores the binary . Supports HDFS and local storage backends . Tests . @param slug The SCN ' s slug . @param recipeInStream A ` InputStream ` from where we read the recipe binary . @param recipeName The recipe binary ' s name . @return A ` String ` with the recipe binary ' s absolute path .
public static String saveRecipeForSlug(
        String slug, InputStream recipeInStream, String recipeName) 
            throws IOException, SystemConnectorException  {
        byte[] recipeBytes;
        try {
            recipeBytes = IOUtils.toByteArray(recipeInStream);
            recipeInStream.close();
        } catch (IOException e) {
            e.printStackTrace();
            throw e;
        }
        String recipeHash = DigestUtils.md5Hex(recipeBytes);
        Configuration configuration = Configuration.getInstance();
        String recipeFilename = Paths.get(
            Recipe.getStorageForSlug(slug),
            FilenameUtils.getBaseName(recipeName)+"_"+recipeHash+"."+FilenameUtils.getExtension(recipeName)
        ).toString();
        if (configuration.execEngine.getRecipeStorageType().startsWith("hdfs")) {
            HDFSConnector connector = (HDFSConnector )
                SystemConnector.getInstance().getHDFSConnector();
            org.apache.hadoop.fs.FileSystem fs = connector.getFileSystem();
            org.apache.hadoop.fs.Path outputFilePath = 
                new org.apache.hadoop.fs.Path(recipeFilename);
            org.apache.hadoop.fs.FSDataOutputStream outputStream = fs.create(
                outputFilePath
            );
            try {
                outputStream.write(recipeBytes);
            } catch (IOException e) {
                e.printStackTrace();
                throw e;
            } finally {
                outputStream.close();
            }
            recipeFilename = "hdfs://" + recipeFilename;
        } else {
            File outputFile = new File(recipeFilename);
            OutputStream outputStream = new FileOutputStream(outputFile);
            try {
                IOUtils.write(recipeBytes, outputStream);
            } catch (IOException e) {
                e.printStackTrace();
                throw e;
            } finally {
                outputStream.close();
            }
        }
        return recipeFilename;
    }
public static String saveRecipeForSlug ( String slug , InputStream recipeInStream , String recipeName ) throws IOException , SystemConnectorException { byte [ ] recipeBytes ; try { recipeBytes = IOUtils . toByteArray ( recipeInStream ) ; recipeInStream . close ( ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; throw e ; } String recipeHash = DigestUtils . md5Hex ( recipeBytes ) ; Configuration configuration = Configuration . getInstance ( ) ; String recipeFilename = Paths . get ( Recipe . getStorageForSlug ( slug ) , FilenameUtils . getBaseName ( recipeName ) + "_" + recipeHash + "." + FilenameUtils . getExtension ( recipeName ) ) . toString ( ) ; if ( configuration . execEngine . getRecipeStorageType ( ) . startsWith ( "hdfs" ) ) { HDFSConnector connector = ( HDFSConnector ) SystemConnector . getInstance ( ) . getHDFSConnector ( ) ; org . apache . hadoop . fs . FileSystem fs = connector . getFileSystem ( ) ; org . apache . hadoop . fs . Path outputFilePath = new org . apache . hadoop . fs . Path ( recipeFilename ) ; org . apache . hadoop . fs . FSDataOutputStream outputStream = fs . create ( outputFilePath ) ; try { outputStream . write ( recipeBytes ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; throw e ; } finally { outputStream . close ( ) ; } recipeFilename = "hdfs://" + recipeFilename ; } else { File outputFile = new File ( recipeFilename ) ; OutputStream outputStream = new FileOutputStream ( outputFile ) ; try { IOUtils . write ( recipeBytes , outputStream ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; throw e ; } finally { outputStream . close ( ) ; } } return recipeFilename ; }
Reads from `recipeInStream` a recipe binary and stores it the configured storage backend.
Reads from ` recipeInStream ` a recipe binary and stores it the configured storage backend .
// Read the binary into a byte array in memory. // Calculate binary's md5 hash. // Find binary's storage location. // Use HDFS storage for recipes. // Create HDFS file path object. // Write to HDFS. // Prepend `hdfs://` before returning recipe name. // Use local storage for recipes. // Create file path object. // Write to local storage.