Search in sources :

Example 6 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 7 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)

Example 8 with OpenFileOptions

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

the class WebInterfaceDownloadServlet method downloadFile.

/**
   * This function prepares for downloading a file.
   *
   * @param path the path of the file to download
   * @param request the {@link HttpServletRequest} object
   * @param response the {@link HttpServletResponse} object
   * @throws FileDoesNotExistException if the file does not exist
   * @throws IOException if an I/O error occurs
   * @throws InvalidPathException if an invalid path is encountered
   * @throws AlluxioException if an unexpected Alluxio exception is thrown
   */
private void downloadFile(AlluxioURI path, HttpServletRequest request, HttpServletResponse response) throws FileDoesNotExistException, IOException, InvalidPathException, AlluxioException {
    FileSystem alluxioClient = FileSystem.Factory.get();
    URIStatus status = alluxioClient.getStatus(path);
    long len = status.getLength();
    String fileName = path.getName();
    response.setContentType("application/octet-stream");
    if (len <= Integer.MAX_VALUE) {
        response.setContentLength((int) len);
    } else {
        response.addHeader("Content-Length", Long.toString(len));
    }
    response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
    FileInStream is = null;
    ServletOutputStream out = null;
    try {
        OpenFileOptions options = OpenFileOptions.defaults().setReadType(ReadType.NO_CACHE);
        is = alluxioClient.openFile(path, options);
        out = response.getOutputStream();
        ByteStreams.copy(is, out);
    } finally {
        if (out != null) {
            out.flush();
            out.close();
        }
        if (is != null) {
            is.close();
        }
    }
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) FileSystem(alluxio.client.file.FileSystem) FileInStream(alluxio.client.file.FileInStream) OpenFileOptions(alluxio.client.file.options.OpenFileOptions) URIStatus(alluxio.client.file.URIStatus)

Example 9 with OpenFileOptions

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

the class WebInterfaceBrowseServlet method displayFile.

/**
   * This function displays 5KB of a file from a specific offset if it is in ASCII format.
   *
   * @param path the path of the file to display
   * @param request the {@link HttpServletRequest} object
   * @param offset where the file starts to display
   * @throws FileDoesNotExistException if the file does not exist
   * @throws IOException if an I/O error occurs
   * @throws InvalidPathException if an invalid path is encountered
   * @throws AlluxioException if an unexpected Alluxio exception is thrown
   */
private void displayFile(AlluxioURI path, HttpServletRequest request, long offset) throws FileDoesNotExistException, InvalidPathException, IOException, AlluxioException {
    FileSystem fs = FileSystem.Factory.get();
    String fileData;
    URIStatus status = fs.getStatus(path);
    if (status.isCompleted()) {
        OpenFileOptions options = OpenFileOptions.defaults().setReadType(ReadType.NO_CACHE);
        try (FileInStream is = fs.openFile(path, options)) {
            int len = (int) Math.min(5 * Constants.KB, status.getLength() - offset);
            byte[] data = new byte[len];
            long skipped = is.skip(offset);
            if (skipped < 0) {
                // nothing was skipped
                fileData = "Unable to traverse to offset; is file empty?";
            } else if (skipped < offset) {
                // couldn't skip all the way to offset
                fileData = "Unable to traverse to offset; is offset larger than the file?";
            } else {
                // read may not read up to len, so only convert what was read
                int read = is.read(data, 0, len);
                if (read < 0) {
                    // stream couldn't read anything, skip went to EOF?
                    fileData = "Unable to read file";
                } else {
                    fileData = WebUtils.convertByteArrayToStringWithoutEscape(data, 0, read);
                }
            }
        }
    } else {
        fileData = "The requested file is not complete yet.";
    }
    List<UIFileBlockInfo> uiBlockInfo = new ArrayList<>();
    for (FileBlockInfo fileBlockInfo : mMaster.getFileSystemMaster().getFileBlockInfoList(path)) {
        uiBlockInfo.add(new UIFileBlockInfo(fileBlockInfo));
    }
    request.setAttribute("fileBlocks", uiBlockInfo);
    request.setAttribute("fileData", fileData);
    request.setAttribute("highestTierAlias", mMaster.getBlockMaster().getGlobalStorageTierAssoc().getAlias(0));
}
Also used : FileSystem(alluxio.client.file.FileSystem) FileInStream(alluxio.client.file.FileInStream) ArrayList(java.util.ArrayList) OpenFileOptions(alluxio.client.file.options.OpenFileOptions) URIStatus(alluxio.client.file.URIStatus) FileBlockInfo(alluxio.wire.FileBlockInfo)

Example 10 with OpenFileOptions

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

the class CpCommand method copyFileToLocal.

/**
   * Copies a file specified by argv from the filesystem to the local filesystem. This is the
   * utility function.
   *
   * @param srcPath The source {@link AlluxioURI} (has to be a file)
   * @param dstPath The {@link AlluxioURI} of the destination in the local filesystem
   * @throws AlluxioException when Alluxio exception occurs
   * @throws IOException when non-Alluxio exception occurs
   */
private void copyFileToLocal(AlluxioURI srcPath, AlluxioURI dstPath) throws AlluxioException, IOException {
    File dstFile = new File(dstPath.getPath());
    String randomSuffix = String.format(".%s_copyToLocal_", RandomStringUtils.randomAlphanumeric(8));
    File tmpDst = new File(dstFile.getAbsolutePath() + randomSuffix);
    try (Closer closer = Closer.create()) {
        OpenFileOptions options = OpenFileOptions.defaults().setReadType(ReadType.NO_CACHE);
        FileInStream is = closer.register(mFileSystem.openFile(srcPath, options));
        FileOutputStream out = closer.register(new FileOutputStream(tmpDst));
        byte[] buf = new byte[64 * Constants.MB];
        int t = is.read(buf);
        while (t != -1) {
            out.write(buf, 0, t);
            t = is.read(buf);
        }
        if (!tmpDst.renameTo(dstFile)) {
            throw new IOException("Failed to rename " + tmpDst.getPath() + " to destination " + dstPath);
        }
        System.out.println("Copied " + srcPath + " to " + dstPath);
    } finally {
        tmpDst.delete();
    }
}
Also used : Closer(com.google.common.io.Closer) FileOutputStream(java.io.FileOutputStream) FileInStream(alluxio.client.file.FileInStream) OpenFileOptions(alluxio.client.file.options.OpenFileOptions) IOException(java.io.IOException) File(java.io.File)

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