use of org.apache.flink.core.fs.FSDataOutputStream in project flink by apache.
the class SavepointStoreTest method testUnexpectedSavepoint.
/**
* Tests loading with unexpected magic number.
*/
@Test
public void testUnexpectedSavepoint() throws Exception {
// Random file
Path filePath = new Path(tmp.getRoot().getPath(), UUID.randomUUID().toString());
FSDataOutputStream fdos = FileSystem.get(filePath.toUri()).create(filePath, false);
DataOutputStream dos = new DataOutputStream(fdos);
for (int i = 0; i < 10; i++) {
dos.writeLong(ThreadLocalRandom.current().nextLong());
}
try {
SavepointStore.loadSavepoint(filePath.toString(), Thread.currentThread().getContextClassLoader());
fail("Did not throw expected Exception");
} catch (RuntimeException e) {
assertTrue(e.getMessage().contains("Flink 1.0") && e.getMessage().contains("Unexpected magic number"));
}
}
use of org.apache.flink.core.fs.FSDataOutputStream in project flink by apache.
the class DistCp method main.
public static void main(String[] args) throws Exception {
// set up the execution environment
final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
ParameterTool params = ParameterTool.fromArgs(args);
if (!params.has("input") || !params.has("output")) {
System.err.println("Usage: --input <path> --output <path> [--parallelism <n>]");
return;
}
final Path sourcePath = new Path(params.get("input"));
final Path targetPath = new Path(params.get("output"));
if (!isLocal(env) && !(isOnDistributedFS(sourcePath) && isOnDistributedFS(targetPath))) {
System.out.println("In a distributed mode only HDFS input/output paths are supported");
return;
}
final int parallelism = params.getInt("parallelism", 10);
if (parallelism <= 0) {
System.err.println("Parallelism should be greater than 0");
return;
}
// make parameters available in the web interface
env.getConfig().setGlobalJobParameters(params);
env.setParallelism(parallelism);
long startTime = System.currentTimeMillis();
LOGGER.info("Initializing copy tasks");
List<FileCopyTask> tasks = getCopyTasks(sourcePath);
LOGGER.info("Copy task initialization took " + (System.currentTimeMillis() - startTime) + "ms");
DataSet<FileCopyTask> inputTasks = new DataSource<>(env, new FileCopyTaskInputFormat(tasks), new GenericTypeInfo<>(FileCopyTask.class), "fileCopyTasks");
FlatMapOperator<FileCopyTask, Object> res = inputTasks.flatMap(new RichFlatMapFunction<FileCopyTask, Object>() {
private static final long serialVersionUID = 1109254230243989929L;
private LongCounter fileCounter;
private LongCounter bytesCounter;
@Override
public void open(Configuration parameters) throws Exception {
bytesCounter = getRuntimeContext().getLongCounter(BYTES_COPIED_CNT_NAME);
fileCounter = getRuntimeContext().getLongCounter(FILES_COPIED_CNT_NAME);
}
@Override
public void flatMap(FileCopyTask task, Collector<Object> out) throws Exception {
LOGGER.info("Processing task: " + task);
Path outPath = new Path(targetPath, task.getRelativePath());
FileSystem targetFs = targetPath.getFileSystem();
// creating parent folders in case of a local FS
if (!targetFs.isDistributedFS()) {
// dealing with cases like file:///tmp or just /tmp
File outFile = outPath.toUri().isAbsolute() ? new File(outPath.toUri()) : new File(outPath.toString());
File parentFile = outFile.getParentFile();
if (!parentFile.mkdirs() && !parentFile.exists()) {
throw new RuntimeException("Cannot create local file system directories: " + parentFile);
}
}
FSDataOutputStream outputStream = null;
FSDataInputStream inputStream = null;
try {
outputStream = targetFs.create(outPath, FileSystem.WriteMode.OVERWRITE);
inputStream = task.getPath().getFileSystem().open(task.getPath());
int bytes = IOUtils.copy(inputStream, outputStream);
bytesCounter.add(bytes);
} finally {
IOUtils.closeQuietly(inputStream);
IOUtils.closeQuietly(outputStream);
}
fileCounter.add(1L);
}
});
// no data sinks are needed, therefore just printing an empty result
res.print();
Map<String, Object> accumulators = env.getLastJobExecutionResult().getAllAccumulatorResults();
LOGGER.info("== COUNTERS ==");
for (Map.Entry<String, Object> e : accumulators.entrySet()) {
LOGGER.info(e.getKey() + ": " + e.getValue());
}
}
use of org.apache.flink.core.fs.FSDataOutputStream in project flink by apache.
the class FileUtilsTest method generateTestFile.
/**
* Generates a random content file.
*
* @param outputFile the path of the output file
* @param length the size of content to generate
* @return MD5 of the output file
* @throws IOException
* @throws NoSuchAlgorithmException
*/
private static String generateTestFile(String outputFile, int length) throws IOException, NoSuchAlgorithmException {
Path outputFilePath = new Path(outputFile);
final FileSystem fileSystem = outputFilePath.getFileSystem();
try (final FSDataOutputStream fsDataOutputStream = fileSystem.create(outputFilePath, FileSystem.WriteMode.OVERWRITE)) {
return writeRandomContent(fsDataOutputStream, length);
}
}
use of org.apache.flink.core.fs.FSDataOutputStream in project flink by apache.
the class AbstractHadoopFileSystemITTest method testSimpleFileWriteAndRead.
@Test
public void testSimpleFileWriteAndRead() throws Exception {
final String testLine = "Hello Upload!";
final Path path = new Path(basePath, "test.txt");
try {
try (FSDataOutputStream out = fs.create(path, FileSystem.WriteMode.OVERWRITE);
OutputStreamWriter writer = new OutputStreamWriter(out, StandardCharsets.UTF_8)) {
writer.write(testLine);
}
// just in case, wait for the path to exist
checkPathExistence(path, true, consistencyToleranceNS);
try (FSDataInputStream in = fs.open(path);
InputStreamReader ir = new InputStreamReader(in, StandardCharsets.UTF_8);
BufferedReader reader = new BufferedReader(ir)) {
String line = reader.readLine();
assertEquals(testLine, line);
}
} finally {
fs.delete(path, false);
}
checkPathExistence(path, false, consistencyToleranceNS);
}
use of org.apache.flink.core.fs.FSDataOutputStream in project flink by apache.
the class StateChangeFsUploader method wrap.
private OutputStreamWithPos wrap(FSDataOutputStream fsStream) throws IOException {
StreamCompressionDecorator instance = compression ? SnappyStreamCompressionDecorator.INSTANCE : UncompressedStreamCompressionDecorator.INSTANCE;
OutputStream compressed = compression ? instance.decorateWithCompression(fsStream) : fsStream;
return new OutputStreamWithPos(new BufferedOutputStream(compressed, bufferSize));
}
Aggregations