Search in sources :

Example 11 with CreateFileOptions

use of alluxio.client.file.options.CreateFileOptions in project alluxio by Alluxio.

the class MultiMount method main.

/**
   * Entry point for the {@link MultiMount} program.
   *
   * @param args command-line arguments
   */
public static void main(String[] args) {
    if (args.length != 1) {
        System.err.println("Usage: ./bin/alluxio runClass alluxio.examples.MultiMount <HDFS_URL>");
        System.exit(-1);
    }
    AlluxioURI mntPath = new AlluxioURI("/mnt");
    AlluxioURI s3Mount = new AlluxioURI("/mnt/s3");
    AlluxioURI inputPath = new AlluxioURI("/mnt/s3/hello.txt");
    AlluxioURI s3Path = new AlluxioURI("s3n://alluxio-demo/");
    AlluxioURI hdfsMount = new AlluxioURI("/mnt/hdfs");
    AlluxioURI outputPath = new AlluxioURI("/mnt/hdfs/hello.txt");
    AlluxioURI hdfsPath = new AlluxioURI(args[0]);
    FileSystem fileSystem = FileSystem.Factory.get();
    try {
        // Make sure mount directory exists.
        if (!fileSystem.exists(mntPath)) {
            System.out.print("creating " + mntPath + " ... ");
            fileSystem.createDirectory(mntPath);
            System.out.println("done");
        }
        // Make sure the S3 mount point does not exist.
        if (fileSystem.exists(s3Mount)) {
            System.out.print("unmounting " + s3Mount + " ... ");
            fileSystem.unmount(s3Mount);
            System.out.println("done");
        }
        // Make sure the HDFS mount point does not exist.
        if (fileSystem.exists(hdfsMount)) {
            System.out.print("unmounting " + hdfsMount + " ... ");
            fileSystem.unmount(hdfsMount);
            System.out.println("done");
        }
        // Mount S3.
        System.out.print("mounting " + s3Path + " to " + s3Mount + " ... ");
        fileSystem.mount(s3Mount, s3Path);
        System.out.println("done");
        // Mount HDFS.
        System.out.print("mounting " + hdfsPath + " to " + hdfsMount + " ... ");
        fileSystem.mount(hdfsMount, hdfsPath);
        System.out.println("done");
        // Make sure output file does not exist.
        if (fileSystem.exists(outputPath)) {
            System.out.print("deleting " + outputPath + " ... ");
            fileSystem.delete(outputPath);
            System.out.println("done");
        }
        // Open the input stream.
        System.out.print("opening " + inputPath + " ... ");
        FileInStream is = fileSystem.openFile(inputPath);
        System.out.println("done");
        // Open the output stream, setting the write type to make sure result is persisted.
        System.out.print("opening " + outputPath + " ... ");
        CreateFileOptions options = CreateFileOptions.defaults().setWriteType(WriteType.CACHE_THROUGH);
        FileOutStream os = fileSystem.createFile(outputPath, options);
        System.out.println("done");
        // Copy the data
        System.out.print("transferring data from " + inputPath + " to " + outputPath + " ... ");
        IOUtils.copy(is, os);
        System.out.println("done");
        // Close the input stream.
        System.out.print("closing " + inputPath + " ... ");
        is.close();
        System.out.println("done");
        // Close the output stream.
        System.out.print("closing " + outputPath + " ... ");
        os.close();
        System.out.println("done");
    } catch (Exception e) {
        System.out.println("fail");
        e.printStackTrace();
    } finally {
        // Make sure the S3 mount point is removed.
        try {
            if (fileSystem.exists(s3Mount)) {
                System.out.print("unmounting " + s3Mount + " ... ");
                fileSystem.unmount(s3Mount);
                System.out.println("done");
            }
        } catch (Exception e) {
            System.out.println("fail");
            e.printStackTrace();
        }
        // Make sure the HDFS mount point is removed.
        try {
            if (fileSystem.exists(hdfsMount)) {
                System.out.print("unmounting " + hdfsMount + " ... ");
                fileSystem.unmount(hdfsMount);
                System.out.println("done");
            }
        } catch (Exception e) {
            System.out.println("fail");
            e.printStackTrace();
        }
    }
}
Also used : CreateFileOptions(alluxio.client.file.options.CreateFileOptions) FileSystem(alluxio.client.file.FileSystem) FileInStream(alluxio.client.file.FileInStream) FileOutStream(alluxio.client.file.FileOutStream) AlluxioURI(alluxio.AlluxioURI)

Example 12 with CreateFileOptions

use of alluxio.client.file.options.CreateFileOptions in project alluxio by Alluxio.

the class CpCommand method copyFile.

/**
   * Copies a file in the Alluxio filesystem.
   *
   * @param srcPath the source {@link AlluxioURI} (has to be a file)
   * @param dstPath the destination path in the Alluxio filesystem
   * @throws AlluxioException when Alluxio exception occurs
   * @throws IOException when non-Alluxio exception occurs
   */
private void copyFile(AlluxioURI srcPath, AlluxioURI dstPath) throws AlluxioException, IOException {
    try (Closer closer = Closer.create()) {
        OpenFileOptions openFileOptions = OpenFileOptions.defaults().setReadType(ReadType.NO_CACHE);
        FileInStream is = closer.register(mFileSystem.openFile(srcPath, openFileOptions));
        CreateFileOptions createFileOptions = CreateFileOptions.defaults();
        FileOutStream os = closer.register(mFileSystem.createFile(dstPath, createFileOptions));
        IOUtils.copy(is, os);
        System.out.println("Copied " + srcPath + " to " + dstPath);
    }
}
Also used : Closer(com.google.common.io.Closer) CreateFileOptions(alluxio.client.file.options.CreateFileOptions) FileInStream(alluxio.client.file.FileInStream) FileOutStream(alluxio.client.file.FileOutStream) OpenFileOptions(alluxio.client.file.options.OpenFileOptions)

Example 13 with CreateFileOptions

use of alluxio.client.file.options.CreateFileOptions in project alluxio by Alluxio.

the class BufferedBlockInStreamIntegrationTest method beforeClass.

@BeforeClass
public static final void beforeClass() throws Exception {
    sFileSystem = sLocalAlluxioClusterResource.get().getClient();
    sWriteBoth = CreateFileOptions.defaults().setWriteType(WriteType.CACHE_THROUGH);
    sWriteAlluxio = CreateFileOptions.defaults().setWriteType(WriteType.MUST_CACHE);
    sWriteUnderStore = CreateFileOptions.defaults().setWriteType(WriteType.THROUGH);
    sTestPath = PathUtils.uniqPath();
    // Create files of varying size and write type to later read from
    for (int k = MIN_LEN; k <= MAX_LEN; k += DELTA) {
        for (CreateFileOptions op : getOptionSet()) {
            AlluxioURI path = new AlluxioURI(sTestPath + "/file_" + k + "_" + op.hashCode());
            FileSystemTestUtils.createByteFile(sFileSystem, path, op, k);
        }
    }
}
Also used : CreateFileOptions(alluxio.client.file.options.CreateFileOptions) AlluxioURI(alluxio.AlluxioURI) BeforeClass(org.junit.BeforeClass)

Example 14 with CreateFileOptions

use of alluxio.client.file.options.CreateFileOptions in project alluxio by Alluxio.

the class BufferedBlockInStreamIntegrationTest method readTest3.

/**
   * Tests {@link alluxio.client.block.BufferedBlockInStream#read(byte[], int, int)}.
   */
@Test
public void readTest3() throws IOException, AlluxioException {
    for (int k = MIN_LEN; k <= MAX_LEN; k += DELTA) {
        for (CreateFileOptions op : getOptionSet()) {
            AlluxioURI path = new AlluxioURI(sTestPath + "/file_" + k + "_" + op.hashCode());
            FileInStream is = sFileSystem.openFile(path, FileSystemTestUtils.toOpenFileOptions(op));
            byte[] ret = new byte[k / 2];
            Assert.assertEquals(k / 2, is.read(ret, 0, k / 2));
            Assert.assertTrue(BufferUtils.equalIncreasingByteArray(k / 2, ret));
            is.close();
            is = sFileSystem.openFile(path, FileSystemTestUtils.toOpenFileOptions(op));
            ret = new byte[k];
            Assert.assertEquals(k, is.read(ret, 0, k));
            Assert.assertTrue(BufferUtils.equalIncreasingByteArray(k, ret));
            is.close();
        }
    }
}
Also used : CreateFileOptions(alluxio.client.file.options.CreateFileOptions) FileInStream(alluxio.client.file.FileInStream) AlluxioURI(alluxio.AlluxioURI) Test(org.junit.Test)

Example 15 with CreateFileOptions

use of alluxio.client.file.options.CreateFileOptions in project alluxio by Alluxio.

the class FileInStreamIntegrationTest method readTest2.

/**
   * Tests {@link FileInStream#read(byte[])}.
   */
@Test
public void readTest2() throws Exception {
    for (int k = MIN_LEN; k <= MAX_LEN; k += DELTA) {
        for (CreateFileOptions op : getOptionSet()) {
            String filename = sTestPath + "/file_" + k + "_" + op.hashCode();
            AlluxioURI uri = new AlluxioURI(filename);
            FileInStream is = sFileSystem.openFile(uri, FileSystemTestUtils.toOpenFileOptions(op));
            byte[] ret = new byte[k];
            Assert.assertEquals(k, is.read(ret));
            Assert.assertTrue(BufferUtils.equalIncreasingByteArray(k, ret));
            is.close();
            is = sFileSystem.openFile(uri, FileSystemTestUtils.toOpenFileOptions(op));
            ret = new byte[k];
            Assert.assertEquals(k, is.read(ret));
            Assert.assertTrue(BufferUtils.equalIncreasingByteArray(k, ret));
            is.close();
        }
    }
}
Also used : CreateFileOptions(alluxio.client.file.options.CreateFileOptions) FileInStream(alluxio.client.file.FileInStream) AlluxioURI(alluxio.AlluxioURI) Test(org.junit.Test)

Aggregations

CreateFileOptions (alluxio.client.file.options.CreateFileOptions)54 AlluxioURI (alluxio.AlluxioURI)48 Test (org.junit.Test)42 FileInStream (alluxio.client.file.FileInStream)22 FileOutStream (alluxio.client.file.FileOutStream)11 URIStatus (alluxio.client.file.URIStatus)6 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 CreateDirectoryOptions (alluxio.client.file.options.CreateDirectoryOptions)4 BeforeClass (org.junit.BeforeClass)3 Mode (alluxio.security.authorization.Mode)2 UnderFileSystem (alluxio.underfs.UnderFileSystem)2 Before (org.junit.Before)2 Matchers.anyString (org.mockito.Matchers.anyString)2 FileSystem (alluxio.client.file.FileSystem)1 DeleteOptions (alluxio.client.file.options.DeleteOptions)1 OpenFileOptions (alluxio.client.file.options.OpenFileOptions)1 SetAttributeOptions (alluxio.client.file.options.SetAttributeOptions)1 LineageFileSystem (alluxio.client.lineage.LineageFileSystem)1 LineageMasterClient (alluxio.client.lineage.LineageMasterClient)1 AccessControlException (alluxio.exception.AccessControlException)1