-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/ant 2636 parquet avro #26
Draft
vargastat
wants to merge
6
commits into
develop
Choose a base branch
from
feature/ANT_2636_parquet_avro
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/main/java/com/rte_france/antares/datamanager_back/util/TimeSeriesReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.rte_france.antares.datamanager_back.util; | ||
|
||
import org.apache.parquet.avro.AvroParquetReader; | ||
import org.apache.parquet.io.LocalInputFile; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Objects; | ||
|
||
public class TimeSeriesReader { | ||
public static TimeSeriesMatrix readFromParquet(Path filePath) throws IOException { | ||
Objects.requireNonNull(filePath); | ||
if (Files.notExists(filePath)) { | ||
throw new IllegalArgumentException("File " + filePath + " doesn't exist"); | ||
} | ||
|
||
var inputFile = new LocalInputFile(filePath); | ||
try (var reader = AvroParquetReader.<TimeSeriesMatrix>builder(inputFile).build()) { | ||
var matrix = reader.read(); | ||
if (matrix == null) { | ||
throw new IOException("The Parquet file is empty or does not contain a TimeSeriesMatrix"); | ||
} | ||
return matrix; | ||
} | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
src/main/java/com/rte_france/antares/datamanager_back/util/TimeSeriesWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.rte_france.antares.datamanager_back.util; | ||
|
||
import org.apache.parquet.avro.AvroParquetWriter; | ||
import org.apache.parquet.hadoop.ParquetFileWriter; | ||
import org.apache.parquet.hadoop.ParquetWriter; | ||
import org.apache.parquet.hadoop.metadata.CompressionCodecName; | ||
import org.apache.parquet.io.LocalOutputFile; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Arrays; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
|
||
public class TimeSeriesWriter { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(TimeSeriesWriter.class); | ||
|
||
public static TimeSeriesMatrix readFromTxt(Path filePath) throws IOException { | ||
Objects.requireNonNull(filePath); | ||
try (var lines = Files.lines(filePath)) { | ||
var rows = lines.map(line -> { | ||
var values = line.trim().split("\\s+"); | ||
var doubles = Arrays.stream(values) | ||
.map(Double::parseDouble) | ||
.toList(); | ||
return new TimeSeriesRow(doubles); | ||
}).collect(Collectors.toList()); | ||
return new TimeSeriesMatrix(rows); | ||
} | ||
} | ||
|
||
public static void writeToParquet(TimeSeriesMatrix matrix, Path outputPath) throws IOException { | ||
Objects.requireNonNull(matrix); | ||
Objects.requireNonNull(outputPath); | ||
if (!outputPath.toString().endsWith(".parquet")) { | ||
outputPath = outputPath.resolveSibling(outputPath.getFileName() + ".parquet"); | ||
} | ||
|
||
var outputFile = new LocalOutputFile(outputPath); | ||
try (var writer = AvroParquetWriter | ||
.<TimeSeriesMatrix>builder(outputFile) | ||
.withSchema(TimeSeriesMatrix.getClassSchema()) | ||
.withCompressionCodec(CompressionCodecName.ZSTD) | ||
.withByteStreamSplitEncoding(true) | ||
.withRowGroupSize((long) ParquetWriter.DEFAULT_BLOCK_SIZE) | ||
.withPageSize(ParquetWriter.DEFAULT_PAGE_SIZE) | ||
.withWriteMode(ParquetFileWriter.Mode.OVERWRITE) | ||
.build()) { | ||
|
||
writer.write(matrix); | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
try { | ||
var matrix = TimeSeriesWriter.readFromTxt(Path.of("src/main/resources/INPUT/load/load_fr_2030-2031.txt")); | ||
|
||
var startSerialization = System.nanoTime(); | ||
var parquetFilePath = Path.of("src/main/resources/INPUT/load/output_test.parquet"); | ||
TimeSeriesWriter.writeToParquet(matrix, parquetFilePath); | ||
var endSerialization = System.nanoTime(); | ||
var serializationTime = (endSerialization - startSerialization) / 1_000_000_000.0; | ||
var fileSize = Files.size(parquetFilePath); | ||
|
||
var startDeserialization = System.nanoTime(); | ||
var deserializedMatrix = TimeSeriesReader.readFromParquet(parquetFilePath); | ||
var endDeserialization = System.nanoTime(); | ||
var deserializationTime = (endDeserialization - startDeserialization) / 1_000_000_000.0; | ||
|
||
LOGGER.info("Serialization time: {}", serializationTime); | ||
LOGGER.info("Deserialization time: {}", deserializationTime); | ||
LOGGER.info(".parquet file size (bytes): {}", fileSize); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
@namespace("com.rte_france.antares.datamanager_back.util") | ||
protocol TimeSeriesMatrixProtocol { | ||
record TimeSeriesMatrix { | ||
array<TimeSeriesRow> rows; | ||
} | ||
|
||
record TimeSeriesRow { | ||
array<double> columns; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
File.exists needs to be checked too