forked from apache/flink
-
Notifications
You must be signed in to change notification settings - Fork 2
/
FileSystemTableSink.java
505 lines (437 loc) · 16.6 KB
/
FileSystemTableSink.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/*
* 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.api.common.typeinfo.Types;
import org.apache.flink.api.java.typeutils.TypeExtractor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.configuration.DelegatingConfiguration;
import org.apache.flink.configuration.ReadableConfig;
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.core.io.SimpleVersionedSerializer;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSink;
import org.apache.flink.streaming.api.functions.sink.DiscardingSink;
import org.apache.flink.streaming.api.functions.sink.filesystem.BucketAssigner;
import org.apache.flink.streaming.api.functions.sink.filesystem.OutputFileConfig;
import org.apache.flink.streaming.api.functions.sink.filesystem.PartFileInfo;
import org.apache.flink.streaming.api.functions.sink.filesystem.RollingPolicy;
import org.apache.flink.streaming.api.functions.sink.filesystem.StreamingFileSink;
import org.apache.flink.streaming.api.functions.sink.filesystem.StreamingFileSink.BucketsBuilder;
import org.apache.flink.streaming.api.functions.sink.filesystem.bucketassigners.SimpleVersionedStringSerializer;
import org.apache.flink.streaming.api.functions.sink.filesystem.rollingpolicies.CheckpointRollingPolicy;
import org.apache.flink.table.api.TableException;
import org.apache.flink.table.api.TableSchema;
import org.apache.flink.table.catalog.ObjectIdentifier;
import org.apache.flink.table.data.RowData;
import org.apache.flink.table.factories.FileSystemFormatFactory;
import org.apache.flink.table.filesystem.stream.StreamingFileCommitter;
import org.apache.flink.table.filesystem.stream.StreamingFileCommitter.CommitMessage;
import org.apache.flink.table.filesystem.stream.StreamingFileWriter;
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.table.utils.PartitionPathUtils;
import org.apache.flink.util.Preconditions;
import java.io.IOException;
import java.io.OutputStream;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import static org.apache.flink.table.filesystem.FileSystemOptions.SINK_PARTITION_COMMIT_POLICY_KIND;
import static org.apache.flink.table.filesystem.FileSystemOptions.SINK_ROLLING_POLICY_FILE_SIZE;
import static org.apache.flink.table.filesystem.FileSystemOptions.SINK_ROLLING_POLICY_TIME_INTERVAL;
import static org.apache.flink.table.filesystem.FileSystemTableFactory.createFormatFactory;
/**
* File system {@link TableSink}.
*/
public class FileSystemTableSink implements
AppendStreamTableSink<RowData>,
PartitionableTableSink,
OverwritableTableSink {
private final ObjectIdentifier tableIdentifier;
private final boolean isBounded;
private final TableSchema schema;
private final List<String> partitionKeys;
private final Path path;
private final String defaultPartName;
private final Map<String, String> properties;
private boolean overwrite = false;
private boolean dynamicGrouping = false;
private LinkedHashMap<String, String> staticPartitions = new LinkedHashMap<>();
/**
* Construct a file system table sink.
*
* @param isBounded whether the input of sink is bounded.
* @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 properties properties.
*/
public FileSystemTableSink(
ObjectIdentifier tableIdentifier,
boolean isBounded,
TableSchema schema,
Path path,
List<String> partitionKeys,
String defaultPartName,
Map<String, String> properties) {
this.tableIdentifier = tableIdentifier;
this.isBounded = isBounded;
this.schema = schema;
this.path = path;
this.defaultPartName = defaultPartName;
this.partitionKeys = partitionKeys;
this.properties = properties;
}
@Override
public final DataStreamSink<RowData> consumeDataStream(DataStream<RowData> dataStream) {
RowDataPartitionComputer computer = new RowDataPartitionComputer(
defaultPartName,
schema.getFieldNames(),
schema.getFieldDataTypes(),
partitionKeys.toArray(new String[0]));
EmptyMetaStoreFactory metaStoreFactory = new EmptyMetaStoreFactory(path);
OutputFileConfig outputFileConfig = OutputFileConfig.builder()
.withPartPrefix("part-" + UUID.randomUUID().toString())
.build();
if (isBounded) {
FileSystemOutputFormat.Builder<RowData> builder = new FileSystemOutputFormat.Builder<>();
builder.setPartitionComputer(computer);
builder.setDynamicGrouped(dynamicGrouping);
builder.setPartitionColumns(partitionKeys.toArray(new String[0]));
builder.setFormatFactory(createOutputFormatFactory());
builder.setMetaStoreFactory(metaStoreFactory);
builder.setOverwrite(overwrite);
builder.setStaticPartitions(staticPartitions);
builder.setTempPath(toStagingPath());
builder.setOutputFileConfig(outputFileConfig);
return dataStream.writeUsingOutputFormat(builder.build())
.setParallelism(dataStream.getParallelism());
} else {
Configuration conf = new Configuration();
properties.forEach(conf::setString);
Object writer = createWriter();
TableBucketAssigner assigner = new TableBucketAssigner(computer);
TableRollingPolicy rollingPolicy = new TableRollingPolicy(
!(writer instanceof Encoder),
conf.get(SINK_ROLLING_POLICY_FILE_SIZE).getBytes(),
conf.get(SINK_ROLLING_POLICY_TIME_INTERVAL).toMillis());
BucketsBuilder<RowData, String, ? extends BucketsBuilder<RowData, ?, ?>> bucketsBuilder;
if (writer instanceof Encoder) {
//noinspection unchecked
bucketsBuilder = StreamingFileSink.forRowFormat(
path, new ProjectionEncoder((Encoder<RowData>) writer, computer))
.withBucketAssigner(assigner)
.withOutputFileConfig(outputFileConfig)
.withRollingPolicy(rollingPolicy);
} else {
//noinspection unchecked
bucketsBuilder = StreamingFileSink.forBulkFormat(
path, new ProjectionBulkFactory((BulkWriter.Factory<RowData>) writer, computer))
.withBucketAssigner(assigner)
.withOutputFileConfig(outputFileConfig)
.withRollingPolicy(rollingPolicy);
}
return createStreamingSink(
conf,
path,
partitionKeys,
tableIdentifier,
overwrite,
dataStream,
bucketsBuilder,
metaStoreFactory);
}
}
public static DataStreamSink<RowData> createStreamingSink(
Configuration conf,
Path path,
List<String> partitionKeys,
ObjectIdentifier tableIdentifier,
boolean overwrite,
DataStream<RowData> inputStream,
BucketsBuilder<RowData, String, ? extends BucketsBuilder<RowData, ?, ?>> bucketsBuilder,
TableMetaStoreFactory msFactory) {
if (overwrite) {
throw new IllegalStateException("Streaming mode not support overwrite.");
}
StreamingFileWriter fileWriter = new StreamingFileWriter(
BucketsBuilder.DEFAULT_BUCKET_CHECK_INTERVAL, bucketsBuilder);
DataStream<CommitMessage> writerStream = inputStream.transform(
StreamingFileWriter.class.getSimpleName(),
TypeExtractor.createTypeInfo(CommitMessage.class),
fileWriter).setParallelism(inputStream.getParallelism());
DataStream<?> returnStream = writerStream;
// save committer when we don't need it.
if (partitionKeys.size() > 0 && conf.contains(SINK_PARTITION_COMMIT_POLICY_KIND)) {
StreamingFileCommitter committer = new StreamingFileCommitter(
path, tableIdentifier, partitionKeys, msFactory, conf);
returnStream = writerStream
.transform(StreamingFileCommitter.class.getSimpleName(), Types.VOID, committer)
.setParallelism(1)
.setMaxParallelism(1);
}
//noinspection unchecked
return returnStream.addSink(new DiscardingSink()).setParallelism(1);
}
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);
}
}
@SuppressWarnings("unchecked")
private OutputFormatFactory<RowData> createOutputFormatFactory() {
Object writer = createWriter();
return writer instanceof Encoder ?
path -> createEncoderOutputFormat((Encoder<RowData>) writer, path) :
path -> createBulkWriterOutputFormat((BulkWriter.Factory<RowData>) writer, path);
}
private Object createWriter() {
FileSystemFormatFactory formatFactory = createFormatFactory(properties);
Configuration conf = new Configuration();
properties.forEach(conf::setString);
FileSystemFormatFactory.WriterContext context = new FileSystemFormatFactory.WriterContext() {
@Override
public TableSchema getSchema() {
return schema;
}
@Override
public ReadableConfig getFormatOptions() {
return new DelegatingConfiguration(conf, formatFactory.factoryIdentifier() + ".");
}
@Override
public List<String> getPartitionKeys() {
return partitionKeys;
}
};
Optional<Encoder<RowData>> encoder = formatFactory.createEncoder(context);
Optional<BulkWriter.Factory<RowData>> bulk = formatFactory.createBulkWriterFactory(context);
if (encoder.isPresent()) {
return encoder.get();
} else if (bulk.isPresent()) {
return bulk.get();
} else {
throw new TableException(
formatFactory + " format should implement at least one Encoder or BulkWriter");
}
}
private static OutputFormat<RowData> createBulkWriterOutputFormat(
BulkWriter.Factory<RowData> factory,
Path path) {
return new OutputFormat<RowData>() {
private static final long serialVersionUID = 1L;
private transient BulkWriter<RowData> writer;
@Override
public void configure(Configuration parameters) {
}
@Override
public void open(int taskNumber, int numTasks) throws IOException {
this.writer = factory.create(path.getFileSystem()
.create(path, FileSystem.WriteMode.OVERWRITE));
}
@Override
public void writeRecord(RowData record) throws IOException {
writer.addElement(record);
}
@Override
public void close() throws IOException {
writer.flush();
writer.finish();
}
};
}
private static OutputFormat<RowData> createEncoderOutputFormat(
Encoder<RowData> encoder,
Path path) {
return new OutputFormat<RowData>() {
private static final long serialVersionUID = 1L;
private transient FSDataOutputStream output;
@Override
public void configure(Configuration parameters) {
}
@Override
public void open(int taskNumber, int numTasks) throws IOException {
this.output = path.getFileSystem()
.create(path, FileSystem.WriteMode.OVERWRITE);
}
@Override
public void writeRecord(RowData record) throws IOException {
encoder.encode(record, output);
}
@Override
public void close() throws IOException {
this.output.flush();
this.output.close();
}
};
}
@Override
public FileSystemTableSink configure(String[] fieldNames, TypeInformation<?>[] fieldTypes) {
return this;
}
@Override
public void setOverwrite(boolean overwrite) {
this.overwrite = overwrite;
}
@Override
public void setStaticPartition(Map<String, String> partitions) {
this.staticPartitions = toPartialLinkedPartSpec(partitions);
}
private LinkedHashMap<String, String> toPartialLinkedPartSpec(Map<String, String> part) {
LinkedHashMap<String, String> partSpec = new LinkedHashMap<>();
for (String partitionKey : partitionKeys) {
if (part.containsKey(partitionKey)) {
partSpec.put(partitionKey, part.get(partitionKey));
}
}
return partSpec;
}
@Override
public TableSchema getTableSchema() {
return schema;
}
@Override
public DataType getConsumedDataType() {
return schema.toRowDataType().bridgedTo(RowData.class);
}
@Override
public boolean configurePartitionGrouping(boolean supportsGrouping) {
this.dynamicGrouping = supportsGrouping;
return dynamicGrouping;
}
/**
* Table bucket assigner, wrap {@link PartitionComputer}.
*/
public static class TableBucketAssigner implements BucketAssigner<RowData, String> {
private final PartitionComputer<RowData> computer;
public TableBucketAssigner(PartitionComputer<RowData> computer) {
this.computer = computer;
}
@Override
public String getBucketId(RowData element, Context context) {
try {
return PartitionPathUtils.generatePartitionPath(
computer.generatePartValues(element));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public SimpleVersionedSerializer<String> getSerializer() {
return SimpleVersionedStringSerializer.INSTANCE;
}
}
/**
* Table {@link RollingPolicy}, it extends {@link CheckpointRollingPolicy} for bulk writers.
*/
public static class TableRollingPolicy extends CheckpointRollingPolicy<RowData, String> {
private final boolean rollOnCheckpoint;
private final long rollingFileSize;
private final long rollingTimeInterval;
public TableRollingPolicy(
boolean rollOnCheckpoint,
long rollingFileSize,
long rollingTimeInterval) {
this.rollOnCheckpoint = rollOnCheckpoint;
Preconditions.checkArgument(rollingFileSize > 0L);
Preconditions.checkArgument(rollingTimeInterval > 0L);
this.rollingFileSize = rollingFileSize;
this.rollingTimeInterval = rollingTimeInterval;
}
@Override
public boolean shouldRollOnCheckpoint(PartFileInfo<String> partFileState) {
try {
return rollOnCheckpoint || partFileState.getSize() > rollingFileSize;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public boolean shouldRollOnEvent(
PartFileInfo<String> partFileState,
RowData element) throws IOException {
return partFileState.getSize() > rollingFileSize;
}
@Override
public boolean shouldRollOnProcessingTime(
PartFileInfo<String> partFileState,
long currentTime) {
return currentTime - partFileState.getCreationTime() >= rollingTimeInterval;
}
}
private static class ProjectionEncoder implements Encoder<RowData> {
private final Encoder<RowData> encoder;
private final RowDataPartitionComputer computer;
private ProjectionEncoder(Encoder<RowData> encoder, RowDataPartitionComputer computer) {
this.encoder = encoder;
this.computer = computer;
}
@Override
public void encode(RowData element, OutputStream stream) throws IOException {
encoder.encode(computer.projectColumnsToWrite(element), stream);
}
}
/**
* Project row to non-partition fields.
*/
public static class ProjectionBulkFactory implements BulkWriter.Factory<RowData> {
private final BulkWriter.Factory<RowData> factory;
private final RowDataPartitionComputer computer;
public ProjectionBulkFactory(BulkWriter.Factory<RowData> factory, RowDataPartitionComputer computer) {
this.factory = factory;
this.computer = computer;
}
@Override
public BulkWriter<RowData> create(FSDataOutputStream out) throws IOException {
BulkWriter<RowData> writer = factory.create(out);
return new BulkWriter<RowData>() {
@Override
public void addElement(RowData element) throws IOException {
writer.addElement(computer.projectColumnsToWrite(element));
}
@Override
public void flush() throws IOException {
writer.flush();
}
@Override
public void finish() throws IOException {
writer.finish();
}
};
}
}
}