leonardBang commented on a change in pull request #11574: 
[FLINK-16859][table-runtime] Introduce FileSystemTableFactory
URL: https://github.com/apache/flink/pull/11574#discussion_r402044180
 
 

 ##########
 File path: 
flink-table/flink-table-runtime-blink/src/main/java/org/apache/flink/table/filesystem/FileSystemTableSink.java
 ##########
 @@ -0,0 +1,271 @@
+/*
+ * 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.table.filesystem;
+
+import org.apache.flink.api.common.io.OutputFormat;
+import org.apache.flink.api.common.serialization.BulkWriter;
+import org.apache.flink.api.common.serialization.Encoder;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.core.fs.FSDataOutputStream;
+import org.apache.flink.core.fs.FileSystem;
+import org.apache.flink.core.fs.Path;
+import org.apache.flink.streaming.api.datastream.DataStream;
+import org.apache.flink.streaming.api.datastream.DataStreamSink;
+import org.apache.flink.table.api.TableException;
+import org.apache.flink.table.api.TableSchema;
+import org.apache.flink.table.dataformat.BaseRow;
+import org.apache.flink.table.sinks.AppendStreamTableSink;
+import org.apache.flink.table.sinks.OverwritableTableSink;
+import org.apache.flink.table.sinks.PartitionableTableSink;
+import org.apache.flink.table.sinks.TableSink;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.util.Preconditions;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+
+/**
+ * File system {@link TableSink}.
+ */
+public class FileSystemTableSink implements
+               AppendStreamTableSink<BaseRow>,
+               PartitionableTableSink,
+               OverwritableTableSink {
+
+       private final TableSchema schema;
+       private final List<String> partitionKeys;
+       private final Path path;
+       private final String defaultPartName;
+       private final FileSystemFormatFactory formatFactory;
+
+       private boolean overwrite = false;
+       private boolean dynamicGrouping = false;
+       private LinkedHashMap<String, String> staticPartitions = new 
LinkedHashMap<>();
+
+       /**
+        * Construct a file system table sink.
+        *
+        * @param schema schema of the table.
+        * @param path directory path of the file system table.
+        * @param partitionKeys partition keys of the table.
+        * @param defaultPartName The default partition name in case the 
dynamic partition column value
+        *                        is null/empty string.
+        * @param formatFactory format factory to create reader.
+        */
+       public FileSystemTableSink(
+                       TableSchema schema,
+                       Path path,
+                       List<String> partitionKeys,
+                       String defaultPartName,
+                       FileSystemFormatFactory formatFactory) {
+               this.schema = schema;
+               this.path = path;
+               this.defaultPartName = defaultPartName;
+               this.formatFactory = formatFactory;
+               this.partitionKeys = partitionKeys;
+       }
+
+       @Override
+       public final DataStreamSink<BaseRow> 
consumeDataStream(DataStream<BaseRow> dataStream) {
+               RowDataPartitionComputer computer = new 
RowDataPartitionComputer(
+                               defaultPartName,
+                               schema.getFieldNames(),
+                               schema.getFieldDataTypes(),
+                               partitionKeys.toArray(new String[0]));
+
+               FileSystemOutputFormat.Builder<BaseRow> builder = new 
FileSystemOutputFormat.Builder<>();
+               builder.setPartitionComputer(computer);
+               builder.setDynamicGrouped(dynamicGrouping);
+               builder.setPartitionColumns(partitionKeys.toArray(new 
String[0]));
+               builder.setFormatFactory(createOutputFormatFactory(
+                               formatFactory, this::getNonPartitionTypes));
+               builder.setMetaStoreFactory(createTableMetaStoreFactory(path));
+               builder.setOverwrite(overwrite);
+               builder.setStaticPartitions(staticPartitions);
+               builder.setTempPath(toStagingPath());
+               return dataStream.writeUsingOutputFormat(builder.build())
+                               .setParallelism(dataStream.getParallelism());
+       }
+
+       private DataType[] getNonPartitionTypes() {
+               return Arrays.stream(schema.getFieldNames())
+                               .filter(name -> !partitionKeys.contains(name))
+                               .map(name -> 
schema.getFieldDataType(name).get())
+                               .toArray(DataType[]::new);
+       }
+
+       private Path toStagingPath() {
+               Path stagingDir = new Path(path, ".staging_" + 
System.currentTimeMillis());
+               try {
+                       FileSystem fs = stagingDir.getFileSystem();
+                       Preconditions.checkState(
+                                       fs.exists(stagingDir) || 
fs.mkdirs(stagingDir),
+                                       "Failed to create staging dir " + 
stagingDir);
+                       return stagingDir;
+               } catch (IOException e) {
+                       throw new RuntimeException(e);
+               }
+       }
+
+       private static OutputFormatFactory<BaseRow> createOutputFormatFactory(
+                       FileSystemFormatFactory formatFactory,
+                       FileSystemFormatFactory.WriterContext context) {
+               Optional<Encoder<BaseRow>> encoder = 
formatFactory.createEncoder(context);
+               Optional<BulkWriter.Factory<BaseRow>> bulk = 
formatFactory.createBulkWriterFactory(context);
+
+               if (!encoder.isPresent() && !bulk.isPresent()) {
+                       throw new TableException(
+                                       formatFactory + " format should 
implement at least one Encoder or BulkWriter");
+               }
+               return encoder
+                               .<OutputFormatFactory<BaseRow>>map(en -> path 
-> createEncoderOutputFormat(en, path))
+                               .orElseGet(() -> path -> 
createBulkWriterOutputFormat(bulk.get(), path));
+       }
+
+       private static OutputFormat<BaseRow> createBulkWriterOutputFormat(
+                       BulkWriter.Factory<BaseRow> factory,
+                       Path path) {
+               return new OutputFormat<BaseRow>() {
+
 
 Review comment:
   Add a serialVersionUID?

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


With regards,
Apache Git Services

Reply via email to