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



##########
File path: 
flink-formats/flink-json/src/main/java/org/apache/flink/formats/json/JsonFileSystemFormatFactory.java
##########
@@ -0,0 +1,263 @@
+/*
+ * 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.api.TableSchema;
+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.LogicalType;
+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 java.util.stream.Collectors;
+
+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);
+
+               // deal partition fields
+               TableSchema tableSchema = context.getSchema();
+               List<DataType> fieldTypes = 
Arrays.asList(tableSchema.getFieldDataTypes());
+               List<String> fieldNames = 
Arrays.asList(tableSchema.getFieldNames());
+               String[] nonPartFieldNames = fieldNames.stream()
+                       .filter(name -> 
!context.getPartitionKeys().contains(name)).toArray(String[]::new);
+               DataType[] nonPartFieldTypes = 
Arrays.asList(nonPartFieldNames).stream()
+                       .map(name -> 
fieldTypes.get(fieldNames.indexOf(name))).toArray(DataType[]::new);
+               RowType nonPartRowType = RowType.of(
+                       Arrays.asList(nonPartFieldTypes).stream()
+                               .map(DataType::getLogicalType)
+                               .toArray(LogicalType[]::new),
+                       nonPartFieldNames);
+
+               // deal project fields
+               int[] selectFields = context.getProjectFields();
+               List<String> partitionKeys = context.getPartitionKeys();
+               List<String> selectFieldNames = Arrays.stream(selectFields)
+                       .mapToObj(fieldNames::get)
+                       .collect(Collectors.toList());
+               List<String> jsonSelectFieldNames = selectFieldNames.stream()

Review comment:
       nice tips




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


Reply via email to