-
Notifications
You must be signed in to change notification settings - Fork 69
Implement a geoparquet writer #899
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
922124a
Implement a geoparquet writer
bchapuis e733d95
Implement the builder
bchapuis 5cb3cd2
Fix deprecation notices
bchapuis dcc468e
Use the builder instead of the constructor
bchapuis 3987f2c
Fix problem when writing groups
bchapuis 8fcc1a7
Address the review comments
bchapuis 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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
138 changes: 138 additions & 0 deletions
138
baremaps-geoparquet/src/main/java/org/apache/baremaps/geoparquet/GeoParquetWriteSupport.java
This file contains hidden or 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,138 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to you under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.baremaps.geoparquet; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.apache.hadoop.conf.Configuration; | ||
import org.apache.parquet.hadoop.api.WriteSupport; | ||
import org.apache.parquet.io.api.Binary; | ||
import org.apache.parquet.io.api.RecordConsumer; | ||
import org.apache.parquet.schema.*; | ||
|
||
/** | ||
* WriteSupport implementation for writing GeoParquetGroup instances to Parquet. | ||
*/ | ||
public class GeoParquetWriteSupport extends WriteSupport<GeoParquetGroup> { | ||
|
||
private Configuration configuration; | ||
private final MessageType schema; | ||
private final GeoParquetMetadata metadata; | ||
private RecordConsumer recordConsumer; | ||
private final ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
/** | ||
* Constructs a new GeoParquetWriteSupport. | ||
* | ||
* @param schema the Parquet schema | ||
* @param metadata the GeoParquet metadata | ||
*/ | ||
public GeoParquetWriteSupport(MessageType schema, GeoParquetMetadata metadata) { | ||
this.schema = schema; | ||
this.metadata = metadata; | ||
} | ||
|
||
@Override | ||
public WriteContext init(Configuration configuration) { | ||
Map<String, String> extraMetadata = new HashMap<>(); | ||
String geoMetadataJson = serializeMetadata(metadata); | ||
extraMetadata.put("geo", geoMetadataJson); | ||
return new WriteContext(schema, extraMetadata); | ||
} | ||
|
||
@Override | ||
public void prepareForWrite(RecordConsumer recordConsumer) { | ||
this.recordConsumer = recordConsumer; | ||
} | ||
|
||
@Override | ||
public void write(GeoParquetGroup group) { | ||
recordConsumer.startMessage(); | ||
writeGroup(group, schema, true); | ||
recordConsumer.endMessage(); | ||
} | ||
|
||
private void writeGroup(GeoParquetGroup group, GroupType groupType, boolean isRoot) { | ||
if (!isRoot) { | ||
recordConsumer.startGroup(); | ||
} | ||
for (int i = 0; i < groupType.getFieldCount(); i++) { | ||
Type fieldType = groupType.getType(i); | ||
String fieldName = fieldType.getName(); | ||
int repetitionCount = group.getFieldRepetitionCount(i); | ||
if (repetitionCount == 0) { | ||
continue; // Skip if no values are present | ||
} | ||
for (int j = 0; j < repetitionCount; j++) { | ||
recordConsumer.startField(fieldName, i); | ||
if (fieldType.isPrimitive()) { | ||
Object value = group.getValue(i, j); | ||
writePrimitive(value, fieldType.asPrimitiveType()); | ||
} else { | ||
GeoParquetGroup childGroup = group.getGroup(i, j); | ||
writeGroup(childGroup, fieldType.asGroupType(), false); | ||
} | ||
recordConsumer.endField(fieldName, i); | ||
} | ||
} | ||
if (!isRoot) { | ||
recordConsumer.endGroup(); | ||
} | ||
} | ||
|
||
private void writePrimitive(Object value, PrimitiveType primitiveType) { | ||
if (value == null) { | ||
// The Parquet format does not support writing null values directly. | ||
// If the field is optional and the value is null, we simply do not write it. | ||
return; | ||
} | ||
switch (primitiveType.getPrimitiveTypeName()) { | ||
case INT32: | ||
recordConsumer.addInteger((Integer) value); | ||
break; | ||
case INT64: | ||
recordConsumer.addLong((Long) value); | ||
break; | ||
case FLOAT: | ||
recordConsumer.addFloat((Float) value); | ||
break; | ||
case DOUBLE: | ||
recordConsumer.addDouble((Double) value); | ||
break; | ||
case BOOLEAN: | ||
recordConsumer.addBoolean((Boolean) value); | ||
break; | ||
case BINARY, FIXED_LEN_BYTE_ARRAY: | ||
recordConsumer.addBinary((Binary) value); | ||
break; | ||
default: | ||
throw new GeoParquetException( | ||
"Unsupported type: " + primitiveType.getPrimitiveTypeName()); | ||
} | ||
} | ||
|
||
private String serializeMetadata(GeoParquetMetadata metadata) { | ||
try { | ||
return objectMapper.writeValueAsString(metadata); | ||
} catch (JsonProcessingException e) { | ||
throw new GeoParquetException("Failed to serialize GeoParquet metadata", e); | ||
} | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
baremaps-geoparquet/src/main/java/org/apache/baremaps/geoparquet/GeoParquetWriter.java
This file contains hidden or 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,98 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to you under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.baremaps.geoparquet; | ||
|
||
import org.apache.hadoop.conf.Configuration; | ||
import org.apache.hadoop.fs.Path; | ||
import org.apache.parquet.conf.ParquetConfiguration; | ||
import org.apache.parquet.hadoop.ParquetWriter; | ||
import org.apache.parquet.hadoop.api.WriteSupport; | ||
import org.apache.parquet.schema.MessageType; | ||
|
||
/** | ||
* A writer for GeoParquet files that writes GeoParquetGroup instances to a Parquet file. | ||
*/ | ||
public class GeoParquetWriter { | ||
|
||
private GeoParquetWriter() { | ||
// Prevent instantiation | ||
} | ||
|
||
public static Builder builder(Path file) { | ||
return new Builder(file); | ||
} | ||
|
||
public static class Builder | ||
extends ParquetWriter.Builder<GeoParquetGroup, GeoParquetWriter.Builder> { | ||
|
||
private MessageType type = null; | ||
|
||
private GeoParquetMetadata metadata = null; | ||
|
||
private Builder(Path file) { | ||
super(file); | ||
} | ||
|
||
/** | ||
* Replace the message type with the specified one. | ||
* | ||
* @param type the message type | ||
* @return the builder | ||
*/ | ||
public GeoParquetWriter.Builder withType(MessageType type) { | ||
this.type = type; | ||
return this; | ||
} | ||
|
||
/** | ||
* Replace the metadata with the specified one. | ||
* | ||
* @param metadata the metadata | ||
* @return the builder | ||
*/ | ||
public GeoParquetWriter.Builder withGeoParquetMetadata(GeoParquetMetadata metadata) { | ||
this.metadata = metadata; | ||
bchapuis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return this; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
protected WriteSupport<GeoParquetGroup> getWriteSupport(Configuration conf) { | ||
// We don't need access to the hadoop configuration for now | ||
return getWriteSupport((ParquetConfiguration) null); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
protected WriteSupport<GeoParquetGroup> getWriteSupport(ParquetConfiguration conf) { | ||
return new GeoParquetWriteSupport(type, metadata); | ||
bchapuis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
protected GeoParquetWriter.Builder self() { | ||
return this; | ||
} | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.