JingsongLi commented on a change in pull request #12010: URL: https://github.com/apache/flink/pull/12010#discussion_r422465890
########## File path: flink-formats/flink-json/src/main/java/org/apache/flink/formats/json/JsonFileSystemFormatFactory.java ########## @@ -0,0 +1,239 @@ +/* + * 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.json; + +import org.apache.flink.api.common.io.DelimitedInputFormat; +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.api.java.typeutils.GenericTypeInfo; +import org.apache.flink.core.fs.FileInputSplit; +import org.apache.flink.core.fs.Path; +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 java.io.IOException; +import java.io.OutputStream; +import java.nio.charset.StandardCharsets; +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 static org.apache.flink.table.descriptors.FormatDescriptorValidator.FORMAT; +import static org.apache.flink.table.descriptors.JsonValidator.FORMAT_FAIL_ON_MISSING_FIELD; +import static org.apache.flink.table.descriptors.JsonValidator.FORMAT_IGNORE_PARSE_ERRORS; + +/** + * Factory to build reader/writer to read/write json format file. + */ +public class JsonFileSystemFormatFactory implements FileSystemFormatFactory { + + @Override + public Map<String, String> requiredContext() { + Map<String, String> context = new HashMap<>(); + context.put(FORMAT, "json"); + return context; + } + + @Override + public List<String> supportedProperties() { + ArrayList<String> properties = new ArrayList<>(); + properties.add(FORMAT_FAIL_ON_MISSING_FIELD); + properties.add(FORMAT_IGNORE_PARSE_ERRORS); + return properties; + } + + @Override + public InputFormat<RowData, ?> createReader(ReaderContext context) { + DescriptorProperties properties = getValidatedProperties(context.getFormatProperties()); + boolean failOnMissingField = properties.getOptionalBoolean(FORMAT_FAIL_ON_MISSING_FIELD).orElse(false); + boolean ignoreParseErrors = properties.getOptionalBoolean(FORMAT_IGNORE_PARSE_ERRORS).orElse(false); + + RowType nonPartRowType = context.getRowTypeWithoutPartKeys(); + JsonRowDataDeserializationSchema deserializationSchema = new JsonRowDataDeserializationSchema( + nonPartRowType, + new GenericTypeInfo(GenericRowData.class), + failOnMissingField, + ignoreParseErrors); + + int[] jsonSelectFieldToProjectFieldMapping = context.getNonPartFieldProjectMapping(); + int[] jsonSelectFieldToJsonFieldMapping = context.getSelectFieldToNonPartFieldMapping(); + + return new JsonInputFormat( + context.getPaths(), + context.getSchema().getFieldDataTypes(), + context.getSchema().getFieldNames(), + context.getProjectFields(), + context.getPartitionKeys(), + context.getDefaultPartName(), + context.getPushedDownLimit(), + jsonSelectFieldToProjectFieldMapping, + jsonSelectFieldToJsonFieldMapping, + deserializationSchema); + } + + @Override + public Optional<Encoder<RowData>> createEncoder(WriterContext context) { + return Optional.of(new JsonRowDataEncoder(new JsonRowDataSerializationSchema(context.getNonPartRowType()))); + } + + @Override + public Optional<BulkWriter.Factory<RowData>> createBulkWriterFactory(WriterContext context) { + return Optional.empty(); + } + + @Override + public boolean supportsSchemaDerivation() { + return true; + } + + private static DescriptorProperties getValidatedProperties(Map<String, String> propertiesMap) { + final DescriptorProperties properties = new DescriptorProperties(true); + properties.putProperties(propertiesMap); + properties.validateBoolean(FORMAT_FAIL_ON_MISSING_FIELD, true); + properties.validateBoolean(FORMAT_IGNORE_PARSE_ERRORS, true); + return properties; + } + + /** + * A {@link JsonInputFormat} is responsible to read {@link RowData} records + * from json format files. + */ + public static class JsonInputFormat extends DelimitedInputFormat<RowData> { + /** + * Code of \r, used to remove \r from a line when the line ends with \r\n. + */ + private static final byte CARRIAGE_RETURN = (byte) '\r'; + + /** + * Code of \n, used to identify if \n is used as delimiter. + */ + private static final byte NEW_LINE = (byte) '\n'; + + 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[] jsonSelectFieldToProjectFieldMapping; + private final int[] jsonSelectFieldToJsonFieldMapping; + private final JsonRowDataDeserializationSchema deserializationSchema; + + private transient boolean end; + private transient long emitted; + // reuse object for per record + private transient GenericRowData rowData; + + public JsonInputFormat( + Path[] filePaths, + DataType[] fieldTypes, + String[] fieldNames, + int[] selectFields, + List<String> partitionKeys, + String defaultPartValue, + long limit, + int[] jsonSelectFieldToProjectFieldMapping, + int[] jsonSelectFieldToJsonFieldMapping, + JsonRowDataDeserializationSchema deserializationSchema) { + super.setFilePaths(filePaths); + this.fieldTypes = fieldTypes; + this.fieldNames = fieldNames; + this.selectFields = selectFields; + this.partitionKeys = partitionKeys; + this.defaultPartValue = defaultPartValue; + this.limit = limit; + this.jsonSelectFieldToProjectFieldMapping = jsonSelectFieldToProjectFieldMapping; + this.jsonSelectFieldToJsonFieldMapping = jsonSelectFieldToJsonFieldMapping; + this.deserializationSchema = deserializationSchema; + } + + @Override + public boolean supportsMultiPaths() { + return true; + } + + @Override + public void open(FileInputSplit split) throws IOException { + super.open(split); + this.end = false; + this.emitted = 0L; + this.rowData = PartitionPathUtils.fillPartitionValueForRecord(fieldNames, fieldTypes, selectFields, + partitionKeys, currentSplit.getPath(), defaultPartValue); + } + + @Override + public boolean reachedEnd() { + return emitted >= limit || end; + } + + @Override + public RowData readRecord(RowData reuse, byte[] bytes, int offset, int numBytes) throws IOException { + GenericRowData returnRecord = rowData; + // remove \r from a line when the line ends with \r\n + if (this.getDelimiter() != null && this.getDelimiter().length == 1 + && this.getDelimiter()[0] == NEW_LINE && offset + numBytes >= 1 + && bytes[offset + numBytes - 1] == CARRIAGE_RETURN){ + numBytes -= 1; + } + byte[] trimBytes = Arrays.copyOfRange(bytes, offset, offset + numBytes); + GenericRowData jsonRow = (GenericRowData) deserializationSchema.deserialize(trimBytes); + + if (jsonRow == null) { + // if the record is null, simply just skip + return null; Review comment: Should not return null only, if just return null, this source will been closed. You should override `nextRecord`. ---------------------------------------------------------------- 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: us...@infra.apache.org