You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2020/05/16 03:28:38 UTC

[GitHub] [flink] leonardBang commented on a change in pull request #12162: [FLINK-17718][avro] Integrate avro to file system connector

leonardBang commented on a change in pull request #12162:
URL: https://github.com/apache/flink/pull/12162#discussion_r426111526



##########
File path: flink-formats/flink-avro/src/main/java/org/apache/flink/formats/avro/AvroFileSystemFormatFactory.java
##########
@@ -0,0 +1,267 @@
+/*
+ * 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.avro;
+
+import org.apache.flink.api.common.io.InputFormat;
+import org.apache.flink.api.common.serialization.BulkWriter;
+import org.apache.flink.api.common.serialization.Encoder;
+import org.apache.flink.core.fs.FSDataOutputStream;
+import org.apache.flink.core.fs.FileInputSplit;
+import org.apache.flink.core.fs.Path;
+import org.apache.flink.formats.avro.typeutils.AvroSchemaConverter;
+import org.apache.flink.table.data.GenericRowData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.descriptors.DescriptorProperties;
+import org.apache.flink.table.factories.FileSystemFormatFactory;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.flink.table.utils.PartitionPathUtils;
+
+import org.apache.avro.Schema;
+import org.apache.avro.file.CodecFactory;
+import org.apache.avro.file.DataFileConstants;
+import org.apache.avro.file.DataFileWriter;
+import org.apache.avro.generic.GenericData;
+import org.apache.avro.generic.GenericDatumWriter;
+import org.apache.avro.generic.GenericRecord;
+import org.apache.avro.generic.IndexedRecord;
+import org.apache.avro.io.DatumWriter;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.stream.Collectors;
+
+import static org.apache.flink.table.descriptors.FormatDescriptorValidator.FORMAT;
+
+/**
+ * Avro format factory for file system.
+ */
+public class AvroFileSystemFormatFactory implements FileSystemFormatFactory {
+
+	public static final String AVRO_OUTPUT_CODEC = "format." + DataFileConstants.CODEC;
+
+	@Override
+	public boolean supportsSchemaDerivation() {
+		return true;
+	}
+
+	@Override
+	public List<String> supportedProperties() {
+		List<String> options = new ArrayList<>();
+		options.add(AVRO_OUTPUT_CODEC);
+		return options;
+	}
+
+	@Override
+	public Map<String, String> requiredContext() {
+		Map<String, String> context = new HashMap<>();
+		context.put(FORMAT, "avro");
+		return context;
+	}
+
+	@Override
+	public InputFormat<RowData, ?> createReader(ReaderContext context) {
+		DescriptorProperties properties = new DescriptorProperties();
+		properties.putProperties(context.getFormatProperties());
+
+		String[] fieldNames = context.getSchema().getFieldNames();
+		List<String> projectFields = Arrays.stream(context.getProjectFields())
+			.mapToObj(idx -> fieldNames[idx])
+			.collect(Collectors.toList());
+		List<String> csvFields = Arrays.stream(fieldNames)
+			.filter(field -> !context.getPartitionKeys().contains(field))
+			.collect(Collectors.toList());
+
+		int[] selectFieldToProjectField = context.getFormatProjectFields().stream()
+			.mapToInt(projectFields::indexOf)
+			.toArray();
+		int[] selectFieldToFormatField = context.getFormatProjectFields().stream()
+			.mapToInt(csvFields::indexOf)
+			.toArray();
+
+		//noinspection unchecked
+		return new RowDataAvroInputFormat(
+				context.getPaths(),
+				context.getFormatRowType(),
+				context.getSchema().getFieldDataTypes(),
+				context.getSchema().getFieldNames(),
+				context.getProjectFields(),
+				context.getPartitionKeys(),
+				context.getDefaultPartName(),
+				context.getPushedDownLimit(),
+				selectFieldToProjectField,
+				selectFieldToFormatField);
+	}
+
+	@Override
+	public Optional<Encoder<RowData>> createEncoder(WriterContext context) {
+		return Optional.empty();
+	}
+
+	@Override
+	public Optional<BulkWriter.Factory<RowData>> createBulkWriterFactory(WriterContext context) {
+		return Optional.of(new RowDataAvroWriterFactory(
+				context.getFormatRowType(),
+				context.getFormatProperties().get(AVRO_OUTPUT_CODEC)));
+	}
+
+	/**
+	 * InputFormat that reads avro record into {@link RowData}.
+	 *
+	 * <p>This extends {@link AvroInputFormat}, but {@link RowData} is not a avro record,
+	 * so now we remove generic information.
+	 * TODO {@link AvroInputFormat} support type conversion.
+	 */
+	private static class RowDataAvroInputFormat extends AvroInputFormat {
+
+		private static final long serialVersionUID = 1L;
+
+		private final DataType[] fieldTypes;
+		private final String[] fieldNames;
+		private final int[] selectFields;
+		private final List<String> partitionKeys;
+		private final String defaultPartValue;
+		private final long limit;
+		private final int[] selectFieldToProjectField;
+		private final int[] selectFieldToFormatField;
+		private final RowType formatRowType;
+
+		private transient long emitted;
+		// reuse object for per record
+		private transient GenericRowData rowData;
+		private transient Schema schema;
+		private transient IndexedRecord record;
+
+		public RowDataAvroInputFormat(
+				Path[] filePaths,
+				RowType formatRowType,
+				DataType[] fieldTypes,
+				String[] fieldNames,
+				int[] selectFields,
+				List<String> partitionKeys,
+				String defaultPartValue,
+				long limit,
+				int[] selectFieldToProjectField,
+				int[] selectFieldToFormatField) {
+			super(filePaths[0], GenericRecord.class);
+			super.setFilePaths(filePaths);
+			this.formatRowType = formatRowType;
+			this.fieldTypes = fieldTypes;
+			this.fieldNames = fieldNames;
+			this.partitionKeys = partitionKeys;
+			this.defaultPartValue = defaultPartValue;
+			this.selectFields = selectFields;
+			this.limit = limit;
+			this.emitted = 0;
+			this.selectFieldToProjectField = selectFieldToProjectField;
+			this.selectFieldToFormatField = selectFieldToFormatField;
+		}
+
+		@Override
+		public void open(FileInputSplit split) throws IOException {
+			super.open(split);
+			schema = AvroSchemaConverter.convertToSchema(formatRowType);
+			record = new GenericData.Record(schema);
+			rowData = PartitionPathUtils.fillPartitionValueForRecord(
+					fieldNames,
+					fieldTypes,
+					selectFields,
+					partitionKeys,
+					currentSplit.getPath(),
+					defaultPartValue);
+		}
+
+		@Override
+		public boolean reachedEnd() throws IOException {
+			return emitted >= limit || super.reachedEnd();
+		}
+
+		@Override
+		public Object nextRecord(Object reuse) throws IOException {
+			@SuppressWarnings("unchecked")
+			IndexedRecord r = (IndexedRecord) super.nextRecord(record);

Review comment:
       We need deal the null here:
   ```
   if (r == null) {
    return null;
   }
   ```

##########
File path: flink-formats/flink-avro/src/main/java/org/apache/flink/formats/avro/AvroRowDataDeserializationSchema.java
##########
@@ -168,7 +168,7 @@ public int hashCode() {
 
 	// --------------------------------------------------------------------------------------------
 
-	private RowData convertAvroRecordToRowData(Schema schema, RowType rowType, IndexedRecord record) {
+	public static GenericRowData convertAvroRecordToRowData(Schema schema, RowType rowType, IndexedRecord record) {

Review comment:
       package privilege is enough here
   ```suggestion
   	static GenericRowData convertAvroRecordToRowData(Schema schema, RowType rowType, IndexedRecord record) {
   ```

##########
File path: flink-formats/flink-avro/src/test/java/org/apache/flink/formats/avro/AvroFilesystemITCase.java
##########
@@ -0,0 +1,57 @@
+/*
+ * 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.avro;
+
+import org.apache.flink.table.planner.runtime.batch.sql.BatchFileSystemITCaseBase;
+
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+
+/**
+ * ITCase to test csv format for {@link AvroFileSystemFormatFactory} in batch mode.

Review comment:
       please also add a ITCase `AvroFsStreamingSinkITCase` for stream 

##########
File path: flink-formats/flink-avro/src/test/java/org/apache/flink/formats/avro/AvroFilesystemITCase.java
##########
@@ -0,0 +1,57 @@
+/*
+ * 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.avro;
+
+import org.apache.flink.table.planner.runtime.batch.sql.BatchFileSystemITCaseBase;
+
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+
+/**
+ * ITCase to test csv format for {@link AvroFileSystemFormatFactory} in batch mode.

Review comment:
       ```suggestion
    * ITCase to test avro format for {@link AvroFileSystemFormatFactory} in batch mode.
   ```




----------------------------------------------------------------
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.

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