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;
}
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);
}
}
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");
}
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);
}
}
}
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);
}
}
}
Aggregations