JingGe commented on a change in pull request #17501:
URL: https://github.com/apache/flink/pull/17501#discussion_r737418008



##########
File path: 
flink-formats/flink-parquet/src/test/java/org/apache/flink/formats/parquet/avro/AvroParquetRecordFormatTest.java
##########
@@ -0,0 +1,211 @@
+/*
+ * 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.flink.formats.parquet.avro;
+
+import org.apache.flink.api.common.serialization.BulkWriter;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.connector.file.src.reader.StreamFormat;
+import org.apache.flink.connector.file.src.util.CheckpointedPosition;
+import org.apache.flink.core.fs.FileSystem;
+import org.apache.flink.core.fs.Path;
+import org.apache.flink.formats.parquet.ParquetWriterFactory;
+
+import org.apache.avro.Schema;
+import org.apache.avro.generic.GenericData;
+import org.apache.avro.generic.GenericRecord;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Objects;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+/**
+ * Unit test for {@link AvroParquetRecordFormat} and {@link
+ * org.apache.flink.connector.file.src.reader.RecordFormat}.
+ */
+class AvroParquetRecordFormatTest {
+
+    private static final String USER_PARQUET_FILE = "user.parquet";
+
+    private static Path path;
+    private static Schema schema;
+    private static List<GenericRecord> records = new ArrayList<>(3);
+
+    @TempDir static java.nio.file.Path temporaryFolder;
+
+    /**
+     * Create a parquet file in the {@code TEMPORARY_FOLDER} directory.
+     *
+     * @throws IOException if new file can not be created.
+     */
+    @BeforeAll
+    static void setup() throws IOException {
+        schema =
+                new Schema.Parser()
+                        .parse(
+                                "{\"type\": \"record\", "
+                                        + "\"name\": \"User\", "
+                                        + "\"fields\": [\n"
+                                        + "        {\"name\": \"name\", 
\"type\": \"string\" },\n"
+                                        + "        {\"name\": 
\"favoriteNumber\",  \"type\": [\"int\", \"null\"] },\n"
+                                        + "        {\"name\": 
\"favoriteColor\", \"type\": [\"string\", \"null\"] }\n"
+                                        + "    ]\n"
+                                        + "    }");
+
+        records.add(createUser("Peter", 1, "red"));
+        records.add(createUser("Tom", 2, "yellow"));
+        records.add(createUser("Jack", 3, "green"));
+
+        path = new Path(temporaryFolder.resolve(USER_PARQUET_FILE).toUri());
+
+        ParquetWriterFactory<GenericRecord> writerFactory =
+                ParquetAvroWriters.forGenericRecord(schema);
+        BulkWriter<GenericRecord> writer =
+                writerFactory.create(
+                        path.getFileSystem().create(path, 
FileSystem.WriteMode.OVERWRITE));
+
+        for (GenericRecord record : records) {
+            writer.addElement(record);
+        }
+
+        writer.flush();
+        writer.finish();
+    }
+
+    @Test
+    void testCreateReader() throws IOException {
+        StreamFormat.Reader<GenericRecord> reader =
+                new AvroParquetRecordFormat(schema)
+                        .createReader(
+                                new Configuration(),
+                                path,
+                                0,
+                                
path.getFileSystem().getFileStatus(path).getLen());
+        for (GenericRecord record : records) {
+            assertUserEquals(Objects.requireNonNull(reader.read()), record);
+        }
+    }
+
+    /** Expect exception since splitting is not supported now. */
+    @Test
+    void testCreateReaderWithSplitting() {
+        assertThrows(
+                IllegalArgumentException.class,
+                () ->
+                        new AvroParquetRecordFormat(schema)
+                                .createReader(new Configuration(), path, 5, 
5));
+    }
+
+    @Test
+    void testCreateReaderWithNullPath() {
+        assertThrows(
+                NullPointerException.class,
+                () ->
+                        new AvroParquetRecordFormat(schema)
+                                .createReader(new Configuration(), (Path) 
null, 0, 0));
+    }
+
+    @Test
+    void testRestoreReaderWithNoOffset() {
+        assertThrows(
+                IllegalArgumentException.class,
+                () ->
+                        new AvroParquetRecordFormat(schema)
+                                .restoreReader(
+                                        new Configuration(),
+                                        path,
+                                        CheckpointedPosition.NO_OFFSET,
+                                        0,
+                                        
path.getFileSystem().getFileStatus(path).getLen()));
+    }
+
+    @Test
+    void testRestoreReader() throws IOException {

Review comment:
       source better, thanks.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@flink.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to