Search in sources :

Example 1 with OpenFileOptions

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

the class BasicNonByteBufferOperations method read.

private boolean read(FileSystem alluxioClient) throws IOException, AlluxioException {
    OpenFileOptions options = OpenFileOptions.defaults().setReadType(mReadType);
    boolean pass = true;
    long startTimeMs = CommonUtils.getCurrentMs();
    try (DataInputStream input = new DataInputStream(alluxioClient.openFile(mFilePath, options))) {
        int length = input.readInt();
        for (int i = 0; i < length; i++) {
            if (input.readInt() != i) {
                pass = false;
                break;
            }
        }
    }
    LOG.info(FormatUtils.formatTimeTakenMs(startTimeMs, "readFile file " + mFilePath));
    return pass;
}
Also used : OpenFileOptions(alluxio.client.file.options.OpenFileOptions) DataInputStream(java.io.DataInputStream)

Example 2 with OpenFileOptions

use of alluxio.client.file.options.OpenFileOptions 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 3 with OpenFileOptions

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

the class LoadCommand method load.

/**
   * Loads a file or directory in Alluxio space, makes it resident in memory.
   *
   * @param filePath The {@link AlluxioURI} path to load into Alluxio memory
   * @throws AlluxioException when Alluxio exception occurs
   * @throws IOException when non-Alluxio exception occurs
   */
private void load(AlluxioURI filePath) throws AlluxioException, IOException {
    URIStatus status = mFileSystem.getStatus(filePath);
    if (status.isFolder()) {
        List<URIStatus> statuses = mFileSystem.listStatus(filePath);
        for (URIStatus uriStatus : statuses) {
            AlluxioURI newPath = new AlluxioURI(uriStatus.getPath());
            load(newPath);
        }
    } else {
        if (status.getInMemoryPercentage() == 100) {
            // The file has already been fully loaded into Alluxio memory.
            return;
        }
        Closer closer = Closer.create();
        try {
            OpenFileOptions options = OpenFileOptions.defaults().setReadType(ReadType.CACHE_PROMOTE);
            FileInStream in = closer.register(mFileSystem.openFile(filePath, options));
            byte[] buf = new byte[8 * Constants.MB];
            while (in.read(buf) != -1) {
            }
        } catch (Exception e) {
            throw closer.rethrow(e);
        } finally {
            closer.close();
        }
    }
    System.out.println(filePath + " loaded");
}
Also used : Closer(com.google.common.io.Closer) FileInStream(alluxio.client.file.FileInStream) OpenFileOptions(alluxio.client.file.options.OpenFileOptions) URIStatus(alluxio.client.file.URIStatus) AlluxioException(alluxio.exception.AlluxioException) IOException(java.io.IOException) AlluxioURI(alluxio.AlluxioURI)

Example 4 with OpenFileOptions

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

the class CatCommand method runCommand.

@Override
protected void runCommand(AlluxioURI path, CommandLine cl) throws AlluxioException, IOException {
    URIStatus status = mFileSystem.getStatus(path);
    if (status.isFolder()) {
        throw new FileDoesNotExistException(ExceptionMessage.PATH_MUST_BE_FILE.getMessage(path));
    }
    OpenFileOptions options = OpenFileOptions.defaults().setReadType(ReadType.NO_CACHE);
    byte[] buf = new byte[512];
    try (FileInStream is = mFileSystem.openFile(path, options)) {
        int read = is.read(buf);
        while (read != -1) {
            System.out.write(buf, 0, read);
            read = is.read(buf);
        }
    }
}
Also used : FileDoesNotExistException(alluxio.exception.FileDoesNotExistException) FileInStream(alluxio.client.file.FileInStream) OpenFileOptions(alluxio.client.file.options.OpenFileOptions) URIStatus(alluxio.client.file.URIStatus)

Example 5 with OpenFileOptions

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

the class TailCommand method runCommand.

@Override
protected void runCommand(AlluxioURI path, CommandLine cl) throws AlluxioException, IOException {
    URIStatus status = mFileSystem.getStatus(path);
    int numOfBytes = Constants.KB;
    if (cl.hasOption('c')) {
        numOfBytes = Integer.parseInt(cl.getOptionValue('c'));
        Preconditions.checkArgument(numOfBytes > 0, "specified bytes must be > 0");
    }
    if (status.isFolder()) {
        throw new IOException(ExceptionMessage.PATH_MUST_BE_FILE.getMessage(path));
    }
    OpenFileOptions options = OpenFileOptions.defaults().setReadType(ReadType.NO_CACHE);
    try (FileInStream is = mFileSystem.openFile(path, options)) {
        byte[] buf = new byte[numOfBytes];
        long bytesToRead;
        if (status.getLength() > numOfBytes) {
            bytesToRead = numOfBytes;
        } else {
            bytesToRead = status.getLength();
        }
        is.skip(status.getLength() - bytesToRead);
        int read = is.read(buf);
        if (read != -1) {
            System.out.write(buf, 0, read);
        }
    }
}
Also used : FileInStream(alluxio.client.file.FileInStream) OpenFileOptions(alluxio.client.file.options.OpenFileOptions) IOException(java.io.IOException) URIStatus(alluxio.client.file.URIStatus)

Aggregations

OpenFileOptions (alluxio.client.file.options.OpenFileOptions)12 FileInStream (alluxio.client.file.FileInStream)9 URIStatus (alluxio.client.file.URIStatus)5 AlluxioURI (alluxio.AlluxioURI)4 Closer (com.google.common.io.Closer)4 IOException (java.io.IOException)3 Test (org.junit.Test)3 FileSystem (alluxio.client.file.FileSystem)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 FileOutStream (alluxio.client.file.FileOutStream)1 CreateFileOptions (alluxio.client.file.options.CreateFileOptions)1 AlluxioException (alluxio.exception.AlluxioException)1 FileDoesNotExistException (alluxio.exception.FileDoesNotExistException)1 FileBlockInfo (alluxio.wire.FileBlockInfo)1 FileInfo (alluxio.wire.FileInfo)1 DataInputStream (java.io.DataInputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 ArrayList (java.util.ArrayList)1 ServletOutputStream (javax.servlet.ServletOutputStream)1