-
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 arrow #25
Draft
vargastat
wants to merge
10
commits into
develop
Choose a base branch
from
feature/ANT_2636_arrow
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
10 commits
Select commit
Hold shift + click to select a range
55229cd
set up writer and matrix type
8d23b97
set up arrow reader
556c7ce
working serializer and deserializer
5a6cff5
add timer and size logging
6846d2c
add compression codec
dfb2a37
reformat
9a9086d
refactor and some fixes
dae4079
refactor reader to use 2D array
d8fa908
clean imports
79c9ca8
fix typo on row count
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
91 changes: 91 additions & 0 deletions
91
src/main/java/com/rte_france/antares/datamanager_back/util/ArrowReader.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,91 @@ | ||
package com.rte_france.antares.datamanager_back.util; | ||
|
||
import org.apache.arrow.memory.RootAllocator; | ||
import org.apache.arrow.vector.Float8Vector; | ||
import org.apache.arrow.vector.VectorSchemaRoot; | ||
import org.apache.arrow.vector.ipc.ArrowFileReader; | ||
import org.apache.arrow.vector.types.pojo.Field; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public class ArrowReader { | ||
private static final int ROW_COUNT = 8760; | ||
|
||
public static Matrix readMatrixFromArrow(Path filePath) throws IOException { | ||
Objects.requireNonNull(filePath); | ||
|
||
try (var channel = Files.newByteChannel(filePath); | ||
var allocator = new RootAllocator(); | ||
var reader = new ArrowFileReader(channel, allocator)) { | ||
|
||
reader.loadNextBatch(); | ||
var root = reader.getVectorSchemaRoot(); | ||
var fields = root.getSchema().getFields(); | ||
|
||
var columns = new ArrayList<MatrixColumn>(); | ||
fillMatrixColumns(fields, root, columns); | ||
|
||
return new Matrix(columns); | ||
} | ||
} | ||
|
||
private static void fillMatrixColumns(List<Field> fields, VectorSchemaRoot root, ArrayList<MatrixColumn> columns) { | ||
for (var field : fields) { | ||
var vector = root.getVector(field.getName()); | ||
var values = new double[vector.getValueCount()]; | ||
for (var i = 0; i < vector.getValueCount(); i++) { | ||
switch (vector) { | ||
case Float8Vector f -> values[i] = f.get(i); | ||
default -> throw new IllegalStateException(); | ||
} | ||
} | ||
columns.add(new MatrixColumn(field.getName(), values)); | ||
} | ||
} | ||
|
||
public static Matrix readMatrixFromTxt(Path filePath) throws IOException { | ||
Objects.requireNonNull(filePath); | ||
|
||
try (var lines = Files.lines(filePath)) { | ||
var iterator = lines.iterator(); | ||
if (!iterator.hasNext()) { | ||
throw new IllegalArgumentException("File is empty"); | ||
} | ||
|
||
var firstLine = iterator.next(); | ||
var columnCount = firstLine.split("\\s+").length; | ||
var data = new double[columnCount][ROW_COUNT]; | ||
|
||
fillDataList(firstLine, iterator, data); | ||
|
||
var columns = new ArrayList<MatrixColumn>(data.length); | ||
for (int j = 0; j < data.length; j++) { | ||
columns.add(new MatrixColumn("Column" + j, data[j])); | ||
} | ||
|
||
return new Matrix(columns); | ||
} | ||
} | ||
|
||
private static void fillDataList(String firstLine, Iterator<String> iterator, double[][] data) { | ||
var rowIndex = 0; | ||
while (iterator.hasNext()) { | ||
String[] values; | ||
if (rowIndex == 0) { | ||
values = firstLine.split("\\s+"); | ||
} else { | ||
values = iterator.next().split("\\s+"); | ||
} | ||
for (var j = 0; j < values.length; j++) { | ||
data[j][rowIndex] = Double.parseDouble(values[j]); | ||
} | ||
rowIndex++; | ||
} | ||
} | ||
} | ||
104 changes: 104 additions & 0 deletions
104
src/main/java/com/rte_france/antares/datamanager_back/util/ArrowWriter.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,104 @@ | ||
package com.rte_france.antares.datamanager_back.util; | ||
|
||
import org.apache.arrow.compression.CommonsCompressionFactory; | ||
import org.apache.arrow.memory.RootAllocator; | ||
import org.apache.arrow.vector.Float8Vector; | ||
import org.apache.arrow.vector.VectorSchemaRoot; | ||
import org.apache.arrow.vector.compression.CompressionUtil; | ||
import org.apache.arrow.vector.ipc.ArrowFileWriter; | ||
import org.apache.arrow.vector.ipc.message.IpcOption; | ||
import org.apache.arrow.vector.types.FloatingPointPrecision; | ||
import org.apache.arrow.vector.types.pojo.ArrowType; | ||
import org.apache.arrow.vector.types.pojo.Field; | ||
import org.apache.arrow.vector.types.pojo.FieldType; | ||
import org.apache.arrow.vector.types.pojo.Schema; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.nio.channels.Channels; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
public class ArrowWriter { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(ArrowWriter.class); | ||
|
||
private static Field doubleField(String name) { | ||
return new Field(name, FieldType.notNullable(new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE)), null); | ||
} | ||
|
||
private static Schema createSchema(Matrix matrix) { | ||
var fields = matrix.getColumns().stream() | ||
.map(column -> doubleField(column.name())) | ||
.collect(Collectors.toList()); | ||
return new Schema(fields); | ||
} | ||
|
||
private static void populateDoubleVector(VectorSchemaRoot table, MatrixColumn column) { | ||
var vector = table.getVector(column.name()); | ||
var values = column.values(); | ||
var size = values.length; | ||
switch (vector) { | ||
case Float8Vector f8Vector -> { | ||
f8Vector.allocateNew(size); | ||
table.setRowCount(size); | ||
IntStream.range(0, size).forEach(i -> f8Vector.set(i, values[i])); | ||
} | ||
default -> throw new IllegalStateException(); | ||
} | ||
} | ||
|
||
public void write(Matrix matrix, OutputStream out) throws IOException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should check if matrix is not null before treatement |
||
Objects.requireNonNull(matrix); | ||
Objects.requireNonNull(out); | ||
|
||
var schema = createSchema(matrix); | ||
try (var allocator = new RootAllocator(); | ||
var table = VectorSchemaRoot.create(schema, allocator)) { | ||
matrix.getColumns().forEach(c -> populateDoubleVector(table, c)); | ||
|
||
var compressionFactory = new CommonsCompressionFactory(); | ||
try (var ch = Channels.newChannel(out); | ||
var writer = new ArrowFileWriter(table, null, ch, null, IpcOption.DEFAULT, compressionFactory, CompressionUtil.CodecType.ZSTD)) { | ||
writer.start(); | ||
writer.writeBatch(); | ||
writer.end(); | ||
} | ||
} | ||
} | ||
|
||
public String getDefaultFileExtension() { | ||
return "arrow"; | ||
} | ||
|
||
public static void main(String[] args) { | ||
var writer = new ArrowWriter(); | ||
try { | ||
var matrix = ArrowReader.readMatrixFromTxt(Path.of("src/main/resources/INPUT/load/load_fr_2030-2031.txt")); | ||
|
||
var startSerialization = System.nanoTime(); | ||
var arrowFilePath = Path.of("src/main/resources/test-matrix.arrow"); | ||
try (var out = Files.newOutputStream(arrowFilePath)) { | ||
writer.write(matrix, out); | ||
} | ||
var endSerialization = System.nanoTime(); | ||
var serializationTime = (endSerialization - startSerialization) / 1_000_000_000.0; | ||
var fileSize = Files.size(arrowFilePath); | ||
|
||
var startDeserialization = System.nanoTime(); | ||
var deserializedMatrix = ArrowReader.readMatrixFromArrow(arrowFilePath); | ||
var endDeserialization = System.nanoTime(); | ||
var deserializationTime = (endDeserialization - startDeserialization) / 1_000_000_000.0; | ||
|
||
LOGGER.info("Serialization time (s): {}", serializationTime); | ||
LOGGER.info("Deserialization time (s): {}", deserializationTime); | ||
LOGGER.info(".arrow file size (bytes): {}", fileSize); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/rte_france/antares/datamanager_back/util/ColumnType.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,12 @@ | ||
/** | ||
* Copyright (c) 2023, RTE (http://www.rte-france.com) | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
package com.rte_france.antares.datamanager_back.util; | ||
|
||
public enum ColumnType { | ||
INT, | ||
FLOAT | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/rte_france/antares/datamanager_back/util/Matrix.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,25 @@ | ||
/** | ||
* Copyright (c) 2023, RTE (http://www.rte-france.com) | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
package com.rte_france.antares.datamanager_back.util; | ||
|
||
import lombok.Value; | ||
|
||
import java.util.List; | ||
|
||
@Value | ||
public class Matrix { | ||
|
||
List<MatrixColumn> columns; | ||
|
||
public int getRowCount() { | ||
if (columns.isEmpty()) { | ||
return 0; | ||
} | ||
return columns.getFirst().getSize(); | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/com/rte_france/antares/datamanager_back/util/MatrixColumn.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,33 @@ | ||
/** | ||
* Copyright (c) 2025, RTE (http://www.rte-france.com) | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
package com.rte_france.antares.datamanager_back.util; | ||
|
||
import java.util.Arrays; | ||
import java.util.Objects; | ||
|
||
public record MatrixColumn(String name, double[] values) { | ||
public int getSize() { | ||
return values.length; | ||
} | ||
|
||
public MatrixColumn { | ||
Objects.requireNonNull(name); | ||
Objects.requireNonNull(values); | ||
} | ||
|
||
public MatrixColumn renamed(String newName) { | ||
return new MatrixColumn(newName, values); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "MatrixColumn{" + | ||
"name='" + name + '\'' + | ||
", values=" + Arrays.toString(values) + | ||
'}'; | ||
} | ||
} |
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.
may a 2-D array might be enough as we are dealing with primitives, take a look at this Java Arrays vs ArrayLists Guide | Medium