-
Notifications
You must be signed in to change notification settings - Fork 1
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 writesupport #27
Draft
vargastat
wants to merge
15
commits into
develop
Choose a base branch
from
feature/ANT_2636_parquet_writesupport
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 12 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
2ea5c15
set up writer and matrix type
077e733
set up arrow reader
2412d0a
working serializer and deserializer
cd8adb0
add timer and size logging
41deb1b
add compression codec
8a85e63
start setting up pom for parquet writesupport implem
cbb1cf9
finalize writesupport implem
2eba142
use constansts for defaults
3b3c360
add reading from parquet to matrix method
98a6626
add ReadSupport and GroupConverter, finish implem
0f409c3
remove commented code
7acf570
reformat
25ad27a
refactor readMatrix method and some fixes
ca1c89a
create new project with studies endpoint
melazaar 88dc7ac
refacto create project
melazaar 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
15 changes: 15 additions & 0 deletions
15
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,15 @@ | ||
/** | ||
* 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; | ||
|
||
/** | ||
* @author Sylvain Leclerc <sylvain.leclerc@rte-france.com> | ||
*/ | ||
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) + | ||
'}'; | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
src/main/java/com/rte_france/antares/datamanager_back/util/MatrixGroupConverter.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,73 @@ | ||
package com.rte_france.antares.datamanager_back.util; | ||
|
||
import org.apache.parquet.io.api.Converter; | ||
import org.apache.parquet.io.api.GroupConverter; | ||
import org.apache.parquet.io.api.PrimitiveConverter; | ||
import org.apache.parquet.schema.GroupType; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public class MatrixGroupConverter extends GroupConverter { | ||
private final List<MatrixColumn> columns = new ArrayList<>(); | ||
private final List<String> columnNames; | ||
private final int rowCount; | ||
|
||
public MatrixGroupConverter(GroupType schema, int rowCount) { | ||
Objects.requireNonNull(schema); | ||
this.rowCount = Objects.checkIndex(rowCount, 8761); | ||
this.columnNames = new ArrayList<>(); | ||
for (var field : schema.getFields()) { | ||
columnNames.add(field.getName()); | ||
} | ||
} | ||
|
||
@Override | ||
public Converter getConverter(int fieldIndex) { | ||
return new PrimitiveConverter() { | ||
private final double[] values = new double[rowCount]; | ||
private int currentIndex = 0; | ||
|
||
@Override | ||
public void addDouble(double value) { | ||
values[currentIndex++] = value; | ||
} | ||
|
||
@Override | ||
public void addFloat(float value) { | ||
values[currentIndex++] = value; | ||
} | ||
|
||
@Override | ||
public void addInt(int value) { | ||
values[currentIndex++] = value; | ||
} | ||
|
||
@Override | ||
public void addLong(long value) { | ||
values[currentIndex++] = value; | ||
} | ||
|
||
@Override | ||
public void addBoolean(boolean value) { | ||
values[currentIndex++] = value ? 1.0 : 0.0; | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public void start() {} | ||
|
||
@Override | ||
public void end() { | ||
for (var name : columnNames) { | ||
double[] values = new double[rowCount]; | ||
columns.add(new MatrixColumn(name, values)); | ||
} | ||
} | ||
|
||
public Matrix getMatrix() { | ||
return new Matrix(columns); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/com/rte_france/antares/datamanager_back/util/MatrixReadSupport.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,46 @@ | ||
package com.rte_france.antares.datamanager_back.util; | ||
|
||
import org.apache.parquet.hadoop.api.InitContext; | ||
import org.apache.parquet.hadoop.api.ReadSupport; | ||
import org.apache.parquet.io.api.GroupConverter; | ||
import org.apache.parquet.io.api.RecordMaterializer; | ||
import org.apache.parquet.schema.MessageType; | ||
|
||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
public class MatrixReadSupport extends ReadSupport<Matrix> { | ||
private final int rowCount; | ||
|
||
public MatrixReadSupport(int rowCount) { | ||
Objects.checkIndex(rowCount, 8761); | ||
this.rowCount = rowCount; | ||
} | ||
|
||
@Override | ||
public ReadContext init(InitContext context) { | ||
var schema = context.getFileSchema(); | ||
return new ReadContext(schema); | ||
} | ||
|
||
@Override | ||
public RecordMaterializer<Matrix> prepareForRead( | ||
org.apache.hadoop.conf.Configuration configuration, | ||
Map<String, String> keyValueMetaData, | ||
MessageType fileSchema, | ||
ReadContext readContext) { | ||
return new RecordMaterializer<Matrix>() { | ||
private final MatrixGroupConverter converter = new MatrixGroupConverter(fileSchema, rowCount); | ||
|
||
@Override | ||
public Matrix getCurrentRecord() { | ||
return converter.getMatrix(); | ||
} | ||
|
||
@Override | ||
public GroupConverter getRootConverter() { | ||
return converter; | ||
} | ||
}; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/rte_france/antares/datamanager_back/util/MatrixRow.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,10 @@ | ||
package com.rte_france.antares.datamanager_back.util; | ||
|
||
import lombok.Value; | ||
|
||
@Value | ||
class MatrixRow { | ||
Matrix matrix; | ||
int row; | ||
} | ||
|
78 changes: 78 additions & 0 deletions
78
src/main/java/com/rte_france/antares/datamanager_back/util/MatrixWriteSupport.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,78 @@ | ||
/** | ||
* 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 org.apache.parquet.hadoop.api.WriteSupport; | ||
import org.apache.parquet.io.api.RecordConsumer; | ||
import org.apache.parquet.schema.MessageType; | ||
import org.apache.parquet.schema.PrimitiveType; | ||
import org.apache.parquet.schema.Type; | ||
import org.apache.parquet.schema.Types; | ||
|
||
import java.util.Collections; | ||
import java.util.stream.Collectors; | ||
|
||
class MatrixWriteSupport extends WriteSupport<MatrixRow> { | ||
|
||
private final Matrix matrix; | ||
private RecordConsumer consumer; | ||
|
||
MatrixWriteSupport(Matrix matrix) { | ||
this.matrix = matrix; | ||
} | ||
|
||
@Override | ||
public WriteContext init(org.apache.hadoop.conf.Configuration configuration) { | ||
var colTypes = matrix.getColumns().stream() | ||
.map(c -> doubleType(c.name())) | ||
.collect(Collectors.toList()); | ||
|
||
var schema = new MessageType("matrix", colTypes); | ||
return new WriteContext(schema, Collections.emptyMap()); | ||
} | ||
|
||
private static Type type(ColumnType type, String name) { | ||
return switch (type) { | ||
case INT -> intType(name); | ||
case FLOAT -> floatType(name); | ||
default -> throw new IllegalArgumentException("Unknown column type " + type); | ||
}; | ||
} | ||
|
||
private static Type floatType(String name) { | ||
return Types.primitive(PrimitiveType.PrimitiveTypeName.FLOAT, Type.Repetition.REQUIRED) | ||
.named(name); | ||
} | ||
|
||
private static Type intType(String name) { | ||
return Types.primitive(PrimitiveType.PrimitiveTypeName.INT32, Type.Repetition.REQUIRED) | ||
.named(name); | ||
} | ||
|
||
private static Type doubleType(String name) { | ||
return Types.primitive(PrimitiveType.PrimitiveTypeName.DOUBLE, Type.Repetition.REQUIRED) | ||
.named(name); | ||
} | ||
|
||
@Override | ||
public void prepareForWrite(RecordConsumer recordConsumer) { | ||
this.consumer = recordConsumer; | ||
} | ||
|
||
@Override | ||
public void write(MatrixRow matrixRow) { | ||
consumer.startMessage(); | ||
var columnIndex = 0; | ||
for (var c : matrixRow.getMatrix().getColumns()) { | ||
consumer.startField(c.name(), columnIndex); | ||
consumer.addDouble(c.values()[matrixRow.getRow()]); | ||
consumer.endField(c.name(), columnIndex); | ||
columnIndex++; | ||
} | ||
consumer.endMessage(); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
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.
not sure if we need checkIndex method here to check rowCount? if rowCount is 8761 index of rows will be from 0 to 8760