forked from apache/iceberg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Flink: Add Avro value reader, writer implementations (apache#1153)
- Loading branch information
Showing
8 changed files
with
809 additions
and
38 deletions.
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
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
72 changes: 72 additions & 0 deletions
72
flink/src/main/java/org/apache/iceberg/flink/data/FlinkAvroReader.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,72 @@ | ||
/* | ||
* 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.iceberg.flink.data; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import org.apache.avro.Schema; | ||
import org.apache.flink.types.Row; | ||
import org.apache.iceberg.avro.ValueReader; | ||
import org.apache.iceberg.avro.ValueReaders; | ||
import org.apache.iceberg.data.avro.DataReader; | ||
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; | ||
import org.apache.iceberg.types.Types; | ||
|
||
public class FlinkAvroReader extends DataReader<Row> { | ||
|
||
public FlinkAvroReader(org.apache.iceberg.Schema expectedSchema, Schema readSchema) { | ||
super(expectedSchema, readSchema, ImmutableMap.of()); | ||
} | ||
|
||
@Override | ||
protected ValueReader<?> createStructReader(Types.StructType struct, | ||
List<ValueReader<?>> fields, | ||
Map<Integer, ?> idToConstant) { | ||
return new RowReader(fields, struct, idToConstant); | ||
} | ||
|
||
private static class RowReader extends ValueReaders.StructReader<Row> { | ||
private final Types.StructType structType; | ||
|
||
private RowReader(List<ValueReader<?>> readers, Types.StructType struct, Map<Integer, ?> idToConstant) { | ||
super(readers, struct, idToConstant); | ||
this.structType = struct; | ||
} | ||
|
||
@Override | ||
protected Row reuseOrCreate(Object reuse) { | ||
if (reuse instanceof Row) { | ||
return (Row) reuse; | ||
} else { | ||
return new Row(structType.fields().size()); | ||
} | ||
} | ||
|
||
@Override | ||
protected Object get(Row row, int pos) { | ||
return row.getField(pos); | ||
} | ||
|
||
@Override | ||
protected void set(Row row, int pos, Object value) { | ||
row.setField(pos, value); | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
flink/src/main/java/org/apache/iceberg/flink/data/FlinkAvroWriter.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,49 @@ | ||
/* | ||
* 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.iceberg.flink.data; | ||
|
||
import java.util.List; | ||
import org.apache.avro.Schema; | ||
import org.apache.flink.types.Row; | ||
import org.apache.iceberg.avro.ValueWriter; | ||
import org.apache.iceberg.avro.ValueWriters; | ||
import org.apache.iceberg.data.avro.DataWriter; | ||
|
||
public class FlinkAvroWriter extends DataWriter<Row> { | ||
public FlinkAvroWriter(Schema schema) { | ||
super(schema); | ||
} | ||
|
||
@Override | ||
protected ValueWriter<?> createStructWriter(List<ValueWriter<?>> fields) { | ||
return new RowWriter(fields); | ||
} | ||
|
||
private static class RowWriter extends ValueWriters.StructWriter<Row> { | ||
private RowWriter(List<ValueWriter<?>> writers) { | ||
super(writers); | ||
} | ||
|
||
@Override | ||
protected Object get(Row struct, int pos) { | ||
return struct.getField(pos); | ||
} | ||
} | ||
} |
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
73 changes: 73 additions & 0 deletions
73
flink/src/test/java/org/apache/iceberg/flink/data/TestFlinkAvroReaderWriter.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 @@ | ||
/* | ||
* 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.iceberg.flink.data; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.Iterator; | ||
import org.apache.flink.types.Row; | ||
import org.apache.iceberg.Files; | ||
import org.apache.iceberg.Schema; | ||
import org.apache.iceberg.avro.Avro; | ||
import org.apache.iceberg.io.CloseableIterable; | ||
import org.apache.iceberg.io.FileAppender; | ||
import org.junit.Assert; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.TemporaryFolder; | ||
|
||
import static org.apache.iceberg.flink.data.RandomData.COMPLEX_SCHEMA; | ||
|
||
public class TestFlinkAvroReaderWriter { | ||
private static final int NUM_RECORDS = 20_000; | ||
|
||
@Rule | ||
public TemporaryFolder temp = new TemporaryFolder(); | ||
|
||
private void testCorrectness(Schema schema, int numRecords, Iterable<Row> iterable) throws IOException { | ||
File testFile = temp.newFile(); | ||
Assert.assertTrue("Delete should succeed", testFile.delete()); | ||
|
||
try (FileAppender<Row> writer = Avro.write(Files.localOutput(testFile)) | ||
.schema(schema) | ||
.createWriterFunc(FlinkAvroWriter::new) | ||
.build()) { | ||
writer.addAll(iterable); | ||
} | ||
|
||
try (CloseableIterable<Row> reader = Avro.read(Files.localInput(testFile)) | ||
.project(schema) | ||
.createReaderFunc(FlinkAvroReader::new) | ||
.build()) { | ||
Iterator<Row> expected = iterable.iterator(); | ||
Iterator<Row> rows = reader.iterator(); | ||
for (int i = 0; i < numRecords; i += 1) { | ||
Assert.assertTrue("Should have expected number of rows", rows.hasNext()); | ||
Assert.assertEquals(expected.next(), rows.next()); | ||
} | ||
Assert.assertFalse("Should not have extra rows", rows.hasNext()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testNormalData() throws IOException { | ||
testCorrectness(COMPLEX_SCHEMA, NUM_RECORDS, RandomData.generate(COMPLEX_SCHEMA, NUM_RECORDS, 19982)); | ||
} | ||
} |
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
Oops, something went wrong.