Java Code Examples for org.apache.beam.sdk.io.Compression#writeCompressed()
The following examples show how to use
org.apache.beam.sdk.io.Compression#writeCompressed() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestUtils.java From DataflowTemplates with Apache License 2.0 | 6 votes |
/** * Helper to generate files for testing. * * @param filePath The path to the file to write. * @param lines The lines to write. * @param compression The compression type of the file. * @return The file written. * @throws IOException If an error occurs while creating or writing the file. */ public static ResourceId writeToFile( String filePath, List<String> lines, Compression compression) throws IOException { String fileContents = String.join(System.lineSeparator(), lines); ResourceId resourceId = FileSystems.matchNewResource(filePath, false); String mimeType = compression == Compression.UNCOMPRESSED ? MimeTypes.TEXT : MimeTypes.BINARY; // Write the file contents to the channel and close. try (ReadableByteChannel readChannel = Channels.newChannel(new ByteArrayInputStream(fileContents.getBytes()))) { try (WritableByteChannel writeChannel = compression.writeCompressed(FileSystems.create(resourceId, mimeType))) { ByteStreams.copy(readChannel, writeChannel); } } return resourceId; }
Example 2
Source File: CompressPayload.java From gcp-ingestion with Mozilla Public License 2.0 | 5 votes |
@VisibleForTesting static byte[] compress(byte[] payload, Compression compression) { ByteArrayOutputStream out = new ByteArrayOutputStream(); // We use a try-with-resources statement to ensure everything gets closed appropriately. try (ReadableByteChannel inChannel = Channels.newChannel(new ByteArrayInputStream(payload)); WritableByteChannel outChannel = compression.writeCompressed(Channels.newChannel(out))) { ByteStreams.copy(inChannel, outChannel); } catch (IOException e) { return payload; } return out.toByteArray(); }
Example 3
Source File: BulkCompressor.java From DataflowTemplates with Apache License 2.0 | 5 votes |
@ProcessElement public void processElement(ProcessContext context) { ResourceId inputFile = context.element().resourceId(); Compression compression = compressionValue.get(); // Add the compression extension to the output filename. Example: demo.txt -> demo.txt.gz String outputFilename = inputFile.getFilename() + compression.getSuggestedSuffix(); // Resolve the necessary resources to perform the transfer ResourceId outputDir = FileSystems.matchNewResource(destinationLocation.get(), true); ResourceId outputFile = outputDir.resolve(outputFilename, StandardResolveOptions.RESOLVE_FILE); ResourceId tempFile = outputDir.resolve("temp-" + outputFilename, StandardResolveOptions.RESOLVE_FILE); // Perform the copy of the compressed channel to the destination. try (ReadableByteChannel readerChannel = FileSystems.open(inputFile)) { try (WritableByteChannel writerChannel = compression.writeCompressed(FileSystems.create(tempFile, MimeTypes.BINARY))) { // Execute the copy to the temporary file ByteStreams.copy(readerChannel, writerChannel); } // Rename the temporary file to the output file FileSystems.rename(ImmutableList.of(tempFile), ImmutableList.of(outputFile)); // Output the path to the uncompressed file context.output(outputFile.toString()); } catch (IOException e) { LOG.error("Error occurred during compression of {}", inputFile.toString(), e); context.output(DEADLETTER_TAG, KV.of(inputFile.toString(), e.getMessage())); } }